lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceHistoryTest.java
/**
     * Connect twice to the service, each using the default (binary) protocol.
     * Publish messages on one connection to a given channel; while in progress,
     * attach the second connection to the same channel and verify a message
     * history up to the point of attachment can be obtained.
     */
Connect twice to the service, each using the default (binary) protocol.
Publish messages on one connection to a given channel; while in progress,
attach the second connection to the same channel and verify a message
history up to the point of attachment can be obtained.
Connect twice to the service each using the default ( binary ) protocol . Publish messages on one connection to a given channel ; while in progress attach the second connection to the same channel and verify a message history up to the point of attachment can be obtained .
@Test
    public void presencehistory_until_attach() {
        AblyRealtime txAbly = null, rxAbly = null;
        try {
            ClientOptions txOpts = createOptions();
            txOpts.token = token.token;
            txOpts.clientId = testClientId;
            txAbly = new AblyRealtime(txOpts);
            DebugOptions rxOpts = new DebugOptions(testVars.keys[0].keyStr);
            fillInOptions(rxOpts);
            RawProtocolMonitor rawPresenceWaiter = RawProtocolMonitor.createReceiver(Action.presence);
            rxOpts.protocolListener = rawPresenceWaiter;
            rxAbly = new AblyRealtime(rxOpts);
            String channelName = "persisted:presencehistory_until_attach_" + testParams.name;
            /* create a channel */
            final Channel txChannel = txAbly.channels.get(channelName);
            final Channel rxChannel = rxAbly.channels.get(channelName);
            /* attach sender */
            txChannel.attach();
            (new ChannelWaiter(txChannel)).waitFor(ChannelState.attached);
            assertEquals("Verify attached state reached", txChannel.state, ChannelState.attached);
            /* publish messages to the channel */
            CompletionSet msgComplete = new CompletionSet();
            int messageCount = 25;
            for (int i = 0; i < messageCount; i++) {
                txChannel.presence.enter(String.valueOf(i), msgComplete.add());
            }
            msgComplete.waitFor();
            /* get the channel history from the attachSerial when we get the attach indication */
            rxChannel.attach();
            new ChannelWaiter(rxChannel).waitFor(ChannelState.attached);
            assertEquals("Verify attached state reached", rxChannel.state, ChannelState.attached);
            assertNotNull("Verify attachSerial provided", rxChannel.properties.attachSerial);
            /* get the history for this channel */
            PaginatedResult<PresenceMessage> messages = rxChannel.presence.history(new Param[] { new Param("untilAttach", "true") });
            assertNotNull("Expected non-null messages", messages);
            assertTrue("Expected at least one message", messages.items().length >= 1);
            /* verify that the history and received messages meet */
            for (int i = 0; i < messageCount; i++) {
                /* 0 --> "24"
                 * 1 --> "23"
                 * ...
                 * 24 --> "0"
                 */
                String actual = (String) messages.items()[messageCount - 1 - i].data;
                String expected = String.valueOf(i);
                assertThat(actual, is(equalTo(expected)));
            }
        } catch (AblyException e) {
            e.printStackTrace();
            fail("presencehistory_from_attach: Unexpected exception instantiating library");
        } finally {
            if(txAbly != null)
                txAbly.close();
            if(rxAbly != null)
                rxAbly.close();
        }
    }
@ Test public void presencehistory_until_attach ( ) { AblyRealtime txAbly = null , rxAbly = null ; try { ClientOptions txOpts = createOptions ( ) ; txOpts . token = token . token ; txOpts . clientId = testClientId ; txAbly = new AblyRealtime ( txOpts ) ; DebugOptions rxOpts = new DebugOptions ( testVars . keys [ 0 ] . keyStr ) ; fillInOptions ( rxOpts ) ; RawProtocolMonitor rawPresenceWaiter = RawProtocolMonitor . createReceiver ( Action . presence ) ; rxOpts . protocolListener = rawPresenceWaiter ; rxAbly = new AblyRealtime ( rxOpts ) ; String channelName = "persisted:presencehistory_until_attach_" + testParams . name ; /* create a channel */ final Channel txChannel = txAbly . channels . get ( channelName ) ; final Channel rxChannel = rxAbly . channels . get ( channelName ) ; /* attach sender */ txChannel . attach ( ) ; ( new ChannelWaiter ( txChannel ) ) . waitFor ( ChannelState . attached ) ; assertEquals ( "Verify attached state reached" , txChannel . state , ChannelState . attached ) ; /* publish messages to the channel */ CompletionSet msgComplete = new CompletionSet ( ) ; int messageCount = 25 ; for ( int i = 0 ; i < messageCount ; i ++ ) { txChannel . presence . enter ( String . valueOf ( i ) , msgComplete . add ( ) ) ; } msgComplete . waitFor ( ) ; /* get the channel history from the attachSerial when we get the attach indication */ rxChannel . attach ( ) ; new ChannelWaiter ( rxChannel ) . waitFor ( ChannelState . attached ) ; assertEquals ( "Verify attached state reached" , rxChannel . state , ChannelState . attached ) ; assertNotNull ( "Verify attachSerial provided" , rxChannel . properties . attachSerial ) ; /* get the history for this channel */ PaginatedResult < PresenceMessage > messages = rxChannel . presence . history ( new Param [ ] { new Param ( "untilAttach" , "true" ) } ) ; assertNotNull ( "Expected non-null messages" , messages ) ; assertTrue ( "Expected at least one message" , messages . items ( ) . length >= 1 ) ; /* verify that the history and received messages meet */ for ( int i = 0 ; i < messageCount ; i ++ ) { /* 0 --> "24"
                 * 1 --> "23"
                 * ...
                 * 24 --> "0"
                 */ String actual = ( String ) messages . items ( ) [ messageCount - 1 - i ] . data ; String expected = String . valueOf ( i ) ; assertThat ( actual , is ( equalTo ( expected ) ) ) ; } } catch ( AblyException e ) { e . printStackTrace ( ) ; fail ( "presencehistory_from_attach: Unexpected exception instantiating library" ) ; } finally { if ( txAbly != null ) txAbly . close ( ) ; if ( rxAbly != null ) rxAbly . close ( ) ; } }
Connect twice to the service, each using the default (binary) protocol.
Connect twice to the service each using the default ( binary ) protocol .