lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceTest.java
/**
     * <p>
     * Validates a client can observe presence messages of other client,
     * when they entered to the same channel and observing client subscribed
     * to a single action.
     * </p>
     *
     * @throws AblyException
     */

Validates a client can observe presence messages of other client,
when they entered to the same channel and observing client subscribed
to a single action.


Validates a client can observe presence messages of other client when they entered to the same channel and observing client subscribed to a single action .
@Test
    public void realtime_presence_subscribe_single() throws AblyException {
        /* Ably instance that will emit presence events */
        AblyRealtime ably1 = null;
        /* Ably instance that will receive presence events */
        AblyRealtime ably2 = null;
        String channelName = "test.presence.subscribe.single." + System.currentTimeMillis();
        PresenceMessage.Action action = Action.enter;
        try {
            ClientOptions option1 = createOptions(testVars.keys[0].keyStr);
            option1.clientId = "emitter client";
            ClientOptions option2 = createOptions(testVars.keys[0].keyStr);
            option2.clientId = "receiver client";
            ably1 = new AblyRealtime(option1);
            ably2 = new AblyRealtime(option2);
            Channel channel1 = ably1.channels.get(channelName);
            channel1.attach();
            (new ChannelWaiter(channel1)).waitFor(ChannelState.attached);
            Channel channel2 = ably2.channels.get(channelName);
            channel2.attach();
            (new ChannelWaiter(channel2)).waitFor(ChannelState.attached);
            ArrayList<PresenceMessage> receivedMessageStack = new ArrayList<>();
            channel2.presence.subscribe(action, new Presence.PresenceListener() {
                List<PresenceMessage> messageStack;
                @Override
                public void onPresenceMessage(PresenceMessage message) {
                    messageStack.add(message);
                }
                public Presence.PresenceListener setMessageStack(List<PresenceMessage> messageStack) {
                    this.messageStack = messageStack;
                    return this;
                }
            }.setMessageStack(receivedMessageStack));
            Helpers.PresenceWaiter waiter = new Helpers.PresenceWaiter(channel2);
            /* Start emitting presence with ably client 1 (emitter) */
            channel1.presence.enter("Hello, #2!", null);
            channel1.presence.updatePresence(new PresenceMessage(Action.update, ably1.options.clientId), null);
            channel1.presence.update("Lorem Ipsum", null);
            channel1.presence.leave(null);
            /* Wait until receiver client (ably2) observes {@code Action.leave}
             * is emitted from emitter client (ably1)
             */
            waiter.waitFor(ably1.options.clientId, Action.leave);
            /* Validate that,
             *- we received specific actions
             */
            assertThat(receivedMessageStack, is(not(empty())));
            for (PresenceMessage message : receivedMessageStack) {
                assertThat(message.action, is(equalTo(action)));
            }
        } finally {
            if (ably1 != null) ably1.close();
            if (ably2 != null) ably2.close();
        }
    }
@ Test public void realtime_presence_subscribe_single ( ) throws AblyException { /* Ably instance that will emit presence events */ AblyRealtime ably1 = null ; /* Ably instance that will receive presence events */ AblyRealtime ably2 = null ; String channelName = "test.presence.subscribe.single." + System . currentTimeMillis ( ) ; PresenceMessage . Action action = Action . enter ; try { ClientOptions option1 = createOptions ( testVars . keys [ 0 ] . keyStr ) ; option1 . clientId = "emitter client" ; ClientOptions option2 = createOptions ( testVars . keys [ 0 ] . keyStr ) ; option2 . clientId = "receiver client" ; ably1 = new AblyRealtime ( option1 ) ; ably2 = new AblyRealtime ( option2 ) ; Channel channel1 = ably1 . channels . get ( channelName ) ; channel1 . attach ( ) ; ( new ChannelWaiter ( channel1 ) ) . waitFor ( ChannelState . attached ) ; Channel channel2 = ably2 . channels . get ( channelName ) ; channel2 . attach ( ) ; ( new ChannelWaiter ( channel2 ) ) . waitFor ( ChannelState . attached ) ; ArrayList < PresenceMessage > receivedMessageStack = new ArrayList < > ( ) ; channel2 . presence . subscribe ( action , new Presence . PresenceListener ( ) { List < PresenceMessage > messageStack ; @ Override public void onPresenceMessage ( PresenceMessage message ) { messageStack . add ( message ) ; } public Presence . PresenceListener setMessageStack ( List < PresenceMessage > messageStack ) { this . messageStack = messageStack ; return this ; } } . setMessageStack ( receivedMessageStack ) ) ; Helpers . PresenceWaiter waiter = new Helpers . PresenceWaiter ( channel2 ) ; /* Start emitting presence with ably client 1 (emitter) */ channel1 . presence . enter ( "Hello, #2!" , null ) ; channel1 . presence . updatePresence ( new PresenceMessage ( Action . update , ably1 . options . clientId ) , null ) ; channel1 . presence . update ( "Lorem Ipsum" , null ) ; channel1 . presence . leave ( null ) ; /* Wait until receiver client (ably2) observes {@code Action.leave}
             * is emitted from emitter client (ably1)
             */ waiter . waitFor ( ably1 . options . clientId , Action . leave ) ; /* Validate that,
             *- we received specific actions
             */ assertThat ( receivedMessageStack , is ( not ( empty ( ) ) ) ) ; for ( PresenceMessage message : receivedMessageStack ) { assertThat ( message . action , is ( equalTo ( action ) ) ) ; } } finally { if ( ably1 != null ) ably1 . close ( ) ; if ( ably2 != null ) ably2 . close ( ) ; } }
<p>
 Validates a client can observe presence messages of other client,
 when they entered to the same channel and observing client subscribed
 to a single action.
<p > Validates a client can observe presence messages of other client when they entered to the same channel and observing client subscribed to a single action .