lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceTest.java
/*
     * Verify presence data is received and encoded/decoded correctly
     * Tests RTP8e, RTP6a
     */
Verify presence data is received and encoded/decoded correctly
Tests RTP8e, RTP6a
Verify presence data is received and encoded / decoded correctly Tests RTP8e RTP6a
@Ignore("FIXME: flaky test")
    @Test
    public void presence_encoding() throws AblyException, InterruptedException {
        AblyRealtime ably1 = null, ably2 = null;
        try {
            /* Set up two connections: one for entering, one for listening */
            final String channelName = "presence_encoding" + testParams.name;
            ClientOptions opts = createOptions(testVars.keys[0].keyStr);
            ably1 = new AblyRealtime(opts);
            ably2 = new AblyRealtime(opts);
            Channel channel1 = ably1.channels.get(channelName);
            Channel channel2 = ably2.channels.get(channelName);
            channel2.attach();
            new ChannelWaiter(channel2).waitFor(ChannelState.attached);
            final ArrayList<Object> receivedPresenceData = new ArrayList<>();
            channel2.presence.subscribe(new Presence.PresenceListener() {
                @Override
                public void onPresenceMessage(PresenceMessage message) {
                    synchronized (receivedPresenceData) {
                        receivedPresenceData.add(message.data);
                        receivedPresenceData.notify();
                    }
                }
            });
            String testStringData = "123";
            byte[] testByteData = new byte[] {1, 2, 3};
            JsonElement testJsonData = new JsonParser().parse("{\"var1\":\"val1\", \"var2\": \"val2\"}");
            channel1.presence.enterClient("1", testStringData);
            channel1.presence.enterClient("2", testByteData);
            channel1.presence.enterClient("3", testJsonData);
            synchronized (receivedPresenceData) {
                while (receivedPresenceData.size() < 3)
                    receivedPresenceData.wait();
            }
            assertEquals("Verify number of received presence messages", receivedPresenceData.size(), 3);
            assertEquals("Verify string data", receivedPresenceData.get(0), testStringData);
            assertTrue("Verify byte[] data",
                    receivedPresenceData.get(1) instanceof byte[] &&
                    Arrays.equals((byte[])receivedPresenceData.get(1), testByteData));
            assertEquals("Verify JSON data", receivedPresenceData.get(2), testJsonData);
            /* use data from ENTER message */
            channel1.presence.leaveClient("1");
            /* use different data */
            channel1.presence.leaveClient("2", "leave");
            synchronized (receivedPresenceData) {
                while (receivedPresenceData.size() < 5)
                    receivedPresenceData.wait();
            }
            assertEquals("Verify string data for enter message is used in leave message", receivedPresenceData.get(3), testStringData);
            assertEquals("Verify overridden leave data", receivedPresenceData.get(4), "leave");
        } finally {
            if (ably1 != null)
                ably1.close();
            if (ably2 != null)
                ably2.close();
        }
    }
@ Ignore ( "FIXME: flaky test" ) @ Test public void presence_encoding ( ) throws AblyException , InterruptedException { AblyRealtime ably1 = null , ably2 = null ; try { /* Set up two connections: one for entering, one for listening */ final String channelName = "presence_encoding" + testParams . name ; ClientOptions opts = createOptions ( testVars . keys [ 0 ] . keyStr ) ; ably1 = new AblyRealtime ( opts ) ; ably2 = new AblyRealtime ( opts ) ; Channel channel1 = ably1 . channels . get ( channelName ) ; Channel channel2 = ably2 . channels . get ( channelName ) ; channel2 . attach ( ) ; new ChannelWaiter ( channel2 ) . waitFor ( ChannelState . attached ) ; final ArrayList < Object > receivedPresenceData = new ArrayList < > ( ) ; channel2 . presence . subscribe ( new Presence . PresenceListener ( ) { @ Override public void onPresenceMessage ( PresenceMessage message ) { synchronized ( receivedPresenceData ) { receivedPresenceData . add ( message . data ) ; receivedPresenceData . notify ( ) ; } } } ) ; String testStringData = "123" ; byte [ ] testByteData = new byte [ ] { 1 , 2 , 3 } ; JsonElement testJsonData = new JsonParser ( ) . parse ( "{\"var1\":\"val1\", \"var2\": \"val2\"}" ) ; channel1 . presence . enterClient ( "1" , testStringData ) ; channel1 . presence . enterClient ( "2" , testByteData ) ; channel1 . presence . enterClient ( "3" , testJsonData ) ; synchronized ( receivedPresenceData ) { while ( receivedPresenceData . size ( ) < 3 ) receivedPresenceData . wait ( ) ; } assertEquals ( "Verify number of received presence messages" , receivedPresenceData . size ( ) , 3 ) ; assertEquals ( "Verify string data" , receivedPresenceData . get ( 0 ) , testStringData ) ; assertTrue ( "Verify byte[] data" , receivedPresenceData . get ( 1 ) instanceof byte [ ] && Arrays . equals ( ( byte [ ] ) receivedPresenceData . get ( 1 ) , testByteData ) ) ; assertEquals ( "Verify JSON data" , receivedPresenceData . get ( 2 ) , testJsonData ) ; /* use data from ENTER message */ channel1 . presence . leaveClient ( "1" ) ; /* use different data */ channel1 . presence . leaveClient ( "2" , "leave" ) ; synchronized ( receivedPresenceData ) { while ( receivedPresenceData . size ( ) < 5 ) receivedPresenceData . wait ( ) ; } assertEquals ( "Verify string data for enter message is used in leave message" , receivedPresenceData . get ( 3 ) , testStringData ) ; assertEquals ( "Verify overridden leave data" , receivedPresenceData . get ( 4 ) , "leave" ) ; } finally { if ( ably1 != null ) ably1 . close ( ) ; if ( ably2 != null ) ably2 . close ( ) ; } }
Verify presence data is received and encoded/decoded correctly
 Tests RTP8e, RTP6a
Verify presence data is received and encoded / decoded correctly Tests RTP8e RTP6a