lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceTest.java
/**
     * Verify protocol messages sent on Presence.enter() follow specs if sent from correct state and
     * the call fails if sent from DETACHED state
     *
     * Tests RTP8c, RTP8g
     */
Verify protocol messages sent on Presence.enter() follow specs if sent from correct state and
the call fails if sent from DETACHED state


Verify protocol messages sent on Presence . enter () follow specs if sent from correct state and the call fails if sent from DETACHED state
@Test
    public void protocol_enter_message_format() throws AblyException, InterruptedException {
        AblyRealtime ably = null;
        try {
            final ArrayList<PresenceMessage> sentPresence = new ArrayList<>();
            /* Allow send but record all the presence messages for later analysis */
            final MockWebsocketFactory mockTransport = new MockWebsocketFactory();
            mockTransport.allowSend(new MockWebsocketFactory.MessageFilter() {
                @Override
                public boolean matches(ProtocolMessage message) {
                    if (message.action == ProtocolMessage.Action.presence && message.presence != null) {
                        synchronized (sentPresence) {
                            Collections.addAll(sentPresence, message.presence);
                            sentPresence.notify();
                        }
                    }
                    return true;
                }
            });
            DebugOptions opts = new DebugOptions(testVars.keys[0].keyStr);
            fillInOptions(opts);
            opts.clientId = testClientId1;
            opts.transportFactory = mockTransport;
            ably = new AblyRealtime(opts);
            Channel channel = ably.channels.get("protocol_enter_message_format_" + testParams.name);
            /* using testClientId1 */
            channel.presence.enter(null, null);
            synchronized (sentPresence) {
                while (sentPresence.size() < 1)
                    sentPresence.wait();
            }
            assertEquals("Verify number of presence messages sent", sentPresence.size(), 1);
            assertTrue("Verify presence messages follows spec",
                    sentPresence.get(0).action == Action.enter &&
                            sentPresence.get(0).clientId == null
            );
            channel.detach();
            new ChannelWaiter(channel).waitFor(ChannelState.detached);
            try {
                channel.presence.enter(null, null);
                fail("Presence.enter() shouldn't succeed in detached state");
            } catch (AblyException e) {
                assertEquals("Verify exception error code", e.errorInfo.code, 91001 /* unable to enter presence channel (invalid channel state) */);
            }
        } finally {
            if (ably != null)
                ably.close();
        }
    }
@ Test public void protocol_enter_message_format ( ) throws AblyException , InterruptedException { AblyRealtime ably = null ; try { final ArrayList < PresenceMessage > sentPresence = new ArrayList < > ( ) ; /* Allow send but record all the presence messages for later analysis */ final MockWebsocketFactory mockTransport = new MockWebsocketFactory ( ) ; mockTransport . allowSend ( new MockWebsocketFactory . MessageFilter ( ) { @ Override public boolean matches ( ProtocolMessage message ) { if ( message . action == ProtocolMessage . Action . presence && message . presence != null ) { synchronized ( sentPresence ) { Collections . addAll ( sentPresence , message . presence ) ; sentPresence . notify ( ) ; } } return true ; } } ) ; DebugOptions opts = new DebugOptions ( testVars . keys [ 0 ] . keyStr ) ; fillInOptions ( opts ) ; opts . clientId = testClientId1 ; opts . transportFactory = mockTransport ; ably = new AblyRealtime ( opts ) ; Channel channel = ably . channels . get ( "protocol_enter_message_format_" + testParams . name ) ; /* using testClientId1 */ channel . presence . enter ( null , null ) ; synchronized ( sentPresence ) { while ( sentPresence . size ( ) < 1 ) sentPresence . wait ( ) ; } assertEquals ( "Verify number of presence messages sent" , sentPresence . size ( ) , 1 ) ; assertTrue ( "Verify presence messages follows spec" , sentPresence . get ( 0 ) . action == Action . enter && sentPresence . get ( 0 ) . clientId == null ) ; channel . detach ( ) ; new ChannelWaiter ( channel ) . waitFor ( ChannelState . detached ) ; try { channel . presence . enter ( null , null ) ; fail ( "Presence.enter() shouldn't succeed in detached state" ) ; } catch ( AblyException e ) { assertEquals ( "Verify exception error code" , e . errorInfo . code , 91001 /* unable to enter presence channel (invalid channel state) */ ) ; } } finally { if ( ably != null ) ably . close ( ) ; } }
Verify protocol messages sent on Presence.enter() follow specs if sent from correct state and
 the call fails if sent from DETACHED state
Verify protocol messages sent on Presence . enter () follow specs if sent from correct state and the call fails if sent from DETACHED state