@@ -1624,6 +1624,66 @@ public void onPresenceMessage(PresenceMessage message) {
16241624 }
16251625 }
16261626
1627+ /**
1628+ * <p>
1629+ * Validates a client can subscribe to presence without implicit channel attach
1630+ * Refer Spec TB4, RTP6d, RTP6e
1631+ * </p>
1632+ * @throws AblyException
1633+ */
1634+ @ Test
1635+ public void presence_subscribe_without_implicit_attach () {
1636+ String ablyChannel = "subscribe_" + testParams .name ;
1637+ AblyRealtime ably = null ;
1638+ try {
1639+ ClientOptions option1 = createOptions (testVars .keys [0 ].keyStr );
1640+ option1 .clientId = "client1" ;
1641+ ably = new AblyRealtime (option1 );
1642+
1643+ /* create a channel and set attachOnSubscribe to false */
1644+ final Channel channel = ably .channels .get (ablyChannel );
1645+ ChannelOptions chOpts = new ChannelOptions ();
1646+ chOpts .attachOnSubscribe = false ;
1647+ channel .setOptions (chOpts );
1648+
1649+ List <Object > receivedPresenceMsg = new ArrayList <>();
1650+ CompletionWaiter completionWaiter = new CompletionWaiter ();
1651+
1652+ /* Check for all subscriptions without ATTACHING state */
1653+ channel .presence .subscribe (m -> receivedPresenceMsg .add (true ), completionWaiter );
1654+ assertEquals (completionWaiter .successCount , 1 );
1655+ assertEquals (channel .state , ChannelState .initialized );
1656+
1657+ channel .presence .subscribe (Action .enter , m -> receivedPresenceMsg .add (true ), completionWaiter );
1658+ assertEquals (completionWaiter .successCount , 2 );
1659+ assertEquals (channel .state , ChannelState .initialized );
1660+
1661+ channel .presence .subscribe (EnumSet .of (Action .enter , Action .leave ),m -> receivedPresenceMsg .add (true ));
1662+ assertEquals (channel .state , ChannelState .initialized );
1663+
1664+ channel .attach ();
1665+ (new ChannelWaiter (channel )).waitFor (ChannelState .attached );
1666+
1667+ channel .presence .enter ("enter client1" , null );
1668+ Exception conditionError = new Helpers .ConditionalWaiter ().
1669+ wait (() -> receivedPresenceMsg .size () == 3 , 5000 );
1670+ assertNull (conditionError );
1671+
1672+ receivedPresenceMsg .clear ();
1673+ channel .presence .leave (null );
1674+ conditionError = new Helpers .ConditionalWaiter ().
1675+ wait (() -> receivedPresenceMsg .size () == 2 , 5000 );
1676+ assertNull (conditionError );
1677+
1678+ } catch (AblyException e ) {
1679+ e .printStackTrace ();
1680+ fail ("init0: Unexpected exception instantiating library" );
1681+ } finally {
1682+ if (ably != null )
1683+ ably .close ();
1684+ }
1685+ }
1686+
16271687 /**
16281688 * <p>
16291689 * Validates a client sending multiple presence updates when the channel is in the attaching
0 commit comments