lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceTest.java
/**
     * Enter large (>100) number of clients so there are several sync messages, disconnect transport
     * in the middle and verify channel is re-syncing presence messages after transport reconnect
     *
     * Tests RTP3
     */
Enter large (>100) number of clients so there are several sync messages, disconnect transport
in the middle and verify channel is re-syncing presence messages after transport reconnect

Tests RTP3
Enter large ( > 100 ) number of clients so there are several sync messages disconnect transport in the middle and verify channel is re - syncing presence messages after transport reconnect Tests RTP3
@Ignore("FIXME: fix exception")
    @Test
    public void reattach_resume_broken_sync() {
        AblyRealtime clientAbly1 = null;
        AblyRealtime clientAbly2 = null;
        TestChannel testChannel = new TestChannel();
        int clientCount = 150; /* Should be greater than 100 to break sync into several messages */
        try {
            /* subscribe for presence events in the anonymous connection */
            new PresenceWaiter(testChannel.realtimeChannel);
            /* set up a connection with specific clientId */
            ClientOptions client1Opts = new ClientOptions(testVars.keys[0].keyStr);
            fillInOptions(client1Opts);
            client1Opts.tokenDetails = wildcardToken;
            clientAbly1 = new AblyRealtime(client1Opts);
            /* wait until connected */
            (new ConnectionWaiter(clientAbly1.connection)).waitFor(ConnectionState.connected);
            assertEquals("Verify connected state reached", clientAbly1.connection.state, ConnectionState.connected);
            /* get channel and attach */
            Channel client1Channel = clientAbly1.channels.get(testChannel.channelName);
            client1Channel.attach();
            (new ChannelWaiter(client1Channel)).waitFor(ChannelState.attached);
            assertEquals("Verify attached state reached", client1Channel.state, ChannelState.attached);
            /* let client1 enter the channel for multiple clients and wait for the success callback */
            CompletionSet enterComplete = new CompletionSet();
            for(int i = 0; i < clientCount; i++) {
                client1Channel.presence.enterClient("client" + i, "Test data (attach_enter_multiple) " + i, enterComplete.add());
            }
            enterComplete.waitFor();
            assertTrue("Verify enter callback called on completion", enterComplete.pending.isEmpty());
            assertTrue("Verify no enter errors", enterComplete.errors.isEmpty());
            /* set up a second connection with different clientId */
            final MockWebsocketFactory mockTransport20 = new MockWebsocketFactory();
            DebugOptions client2Opts = new DebugOptions(testVars.keys[0].keyStr);
            fillInOptions(client2Opts);
            client2Opts.transportFactory = mockTransport20;
            client2Opts.tokenDetails = token2;
            client2Opts.clientId = testClientId2;
            client2Opts.autoConnect = false;
            mockTransport20.allowSend();
            clientAbly2 = new AblyRealtime(client2Opts);
            /* wait until connected */
            ConnectionWaiter connectionWaiter = new ConnectionWaiter(clientAbly2.connection);
            clientAbly2.connection.connect();
            connectionWaiter.waitFor(ConnectionState.connected);
            /* get channel */
            final Channel client2Channel = clientAbly2.channels.get(testChannel.channelName);
            final ConnectionManager connectionManager = clientAbly2.connection.connectionManager;
            final boolean[] disconnectedTransport = new boolean[]{false};
            final int[] presenceCount = new int[]{0};
            client2Channel.attach(new CompletionListener() {
                @Override
                public void onSuccess() {
                    try {
                        client2Channel.presence.subscribe(new Presence.PresenceListener() {
                            @Override
                            public void onPresenceMessage(PresenceMessage message) {
                                if (!disconnectedTransport[0]) {
                                    mockTransport20.lastCreatedTransport.close();
                                    connectionManager.onTransportUnavailable(mockTransport20.lastCreatedTransport, new ErrorInfo("Mock", 50000));
                                }
                                disconnectedTransport[0] = true;
                                presenceCount[0]++;
                            }
                        });
                    }
                    catch (AblyException e) {
                    }
                }
                @Override
                public void onError(ErrorInfo reason) {
                }
            });
            ChannelWaiter channelWaiter = new ChannelWaiter(client2Channel);
            channelWaiter.waitFor(ChannelState.attached);
            /* Wait for reconnect */
            connectionWaiter.waitFor(ConnectionState.connected, 2);
            client2Channel.presence.unsubscribe();
            /* Verify that channel received sync and all 150 presence messages are received */
            try {
                Thread.sleep(500);
                assertEquals("Verify number of received presence messages", client2Channel.presence.get(true).length, clientCount);
            } catch (InterruptedException e) {}
        } catch(AblyException e) {
            e.printStackTrace();
            fail("Unexpected exception running test: " + e.getMessage());
        } finally {
            if(clientAbly1 != null)
                clientAbly1.close();
            if(clientAbly2 != null)
                clientAbly2.close();
            testChannel.dispose();
        }
    }
@ Ignore ( "FIXME: fix exception" ) @ Test public void reattach_resume_broken_sync ( ) { AblyRealtime clientAbly1 = null ; AblyRealtime clientAbly2 = null ; TestChannel testChannel = new TestChannel ( ) ; int clientCount = 150 ; /* Should be greater than 100 to break sync into several messages */ try { /* subscribe for presence events in the anonymous connection */ new PresenceWaiter ( testChannel . realtimeChannel ) ; /* set up a connection with specific clientId */ ClientOptions client1Opts = new ClientOptions ( testVars . keys [ 0 ] . keyStr ) ; fillInOptions ( client1Opts ) ; client1Opts . tokenDetails = wildcardToken ; clientAbly1 = new AblyRealtime ( client1Opts ) ; /* wait until connected */ ( new ConnectionWaiter ( clientAbly1 . connection ) ) . waitFor ( ConnectionState . connected ) ; assertEquals ( "Verify connected state reached" , clientAbly1 . connection . state , ConnectionState . connected ) ; /* get channel and attach */ Channel client1Channel = clientAbly1 . channels . get ( testChannel . channelName ) ; client1Channel . attach ( ) ; ( new ChannelWaiter ( client1Channel ) ) . waitFor ( ChannelState . attached ) ; assertEquals ( "Verify attached state reached" , client1Channel . state , ChannelState . attached ) ; /* let client1 enter the channel for multiple clients and wait for the success callback */ CompletionSet enterComplete = new CompletionSet ( ) ; for ( int i = 0 ; i < clientCount ; i ++ ) { client1Channel . presence . enterClient ( "client" + i , "Test data (attach_enter_multiple) " + i , enterComplete . add ( ) ) ; } enterComplete . waitFor ( ) ; assertTrue ( "Verify enter callback called on completion" , enterComplete . pending . isEmpty ( ) ) ; assertTrue ( "Verify no enter errors" , enterComplete . errors . isEmpty ( ) ) ; /* set up a second connection with different clientId */ final MockWebsocketFactory mockTransport20 = new MockWebsocketFactory ( ) ; DebugOptions client2Opts = new DebugOptions ( testVars . keys [ 0 ] . keyStr ) ; fillInOptions ( client2Opts ) ; client2Opts . transportFactory = mockTransport20 ; client2Opts . tokenDetails = token2 ; client2Opts . clientId = testClientId2 ; client2Opts . autoConnect = false ; mockTransport20 . allowSend ( ) ; clientAbly2 = new AblyRealtime ( client2Opts ) ; /* wait until connected */ ConnectionWaiter connectionWaiter = new ConnectionWaiter ( clientAbly2 . connection ) ; clientAbly2 . connection . connect ( ) ; connectionWaiter . waitFor ( ConnectionState . connected ) ; /* get channel */ final Channel client2Channel = clientAbly2 . channels . get ( testChannel . channelName ) ; final ConnectionManager connectionManager = clientAbly2 . connection . connectionManager ; final boolean [ ] disconnectedTransport = new boolean [ ] { false } ; final int [ ] presenceCount = new int [ ] { 0 } ; client2Channel . attach ( new CompletionListener ( ) { @ Override public void onSuccess ( ) { try { client2Channel . presence . subscribe ( new Presence . PresenceListener ( ) { @ Override public void onPresenceMessage ( PresenceMessage message ) { if ( ! disconnectedTransport [ 0 ] ) { mockTransport20 . lastCreatedTransport . close ( ) ; connectionManager . onTransportUnavailable ( mockTransport20 . lastCreatedTransport , new ErrorInfo ( "Mock" , 50000 ) ) ; } disconnectedTransport [ 0 ] = true ; presenceCount [ 0 ] ++ ; } } ) ; } catch ( AblyException e ) { } } @ Override public void onError ( ErrorInfo reason ) { } } ) ; ChannelWaiter channelWaiter = new ChannelWaiter ( client2Channel ) ; channelWaiter . waitFor ( ChannelState . attached ) ; /* Wait for reconnect */ connectionWaiter . waitFor ( ConnectionState . connected , 2 ) ; client2Channel . presence . unsubscribe ( ) ; /* Verify that channel received sync and all 150 presence messages are received */ try { Thread . sleep ( 500 ) ; assertEquals ( "Verify number of received presence messages" , client2Channel . presence . get ( true ) . length , clientCount ) ; } catch ( InterruptedException e ) { } } catch ( AblyException e ) { e . printStackTrace ( ) ; fail ( "Unexpected exception running test: " + e . getMessage ( ) ) ; } finally { if ( clientAbly1 != null ) clientAbly1 . close ( ) ; if ( clientAbly2 != null ) clientAbly2 . close ( ) ; testChannel . dispose ( ) ; } }
Enter large (>100) number of clients so there are several sync messages, disconnect transport
 in the middle and verify channel is re-syncing presence messages after transport reconnect
Enter large ( > 100 ) number of clients so there are several sync messages disconnect transport in the middle and verify channel is re - syncing presence messages after transport reconnect