lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceHistoryTest.java
/**
     * Publish events and check expected history based on time slice (forwards)
     */
Publish events and check expected history based on time slice (forwards)
Publish events and check expected history based on time slice ( forwards )
@Test
    public void presencehistory_time_f() {
        AblyRealtime ably = null;
        try {
            /* first, publish some messages */
            long intervalStart = 0, intervalEnd = 0;
            ClientOptions rtOpts = createOptions();
            rtOpts.token = token.token;
            rtOpts.clientId = testClientId;
            ably = new AblyRealtime(rtOpts);
            String channelName = "persisted:presencehistory_time_f_" + testParams.name;
            /* create a channel */
            final Channel channel = ably.channels.get(channelName);
            /* attach */
            channel.attach();
            (new ChannelWaiter(channel)).waitFor(ChannelState.attached);
            assertEquals("Verify attached state reached", channel.state, ChannelState.attached);
            /* send batches of messages with short inter-message delay */
            CompletionSet msgComplete = new CompletionSet();
            for(int i = 0; i < 20; i++) {
                channel.presence.enter(String.valueOf(i), msgComplete.add());
                Thread.sleep(100L);
            }
            Thread.sleep(1000L);
            intervalStart = timeOffset + System.currentTimeMillis();
            for(int i = 20; i < 40; i++) {
                channel.presence.enter(String.valueOf(i), msgComplete.add());
                Thread.sleep(100L);
            }
            intervalEnd = timeOffset + System.currentTimeMillis() - 1;
            Thread.sleep(1000L);
            for(int i = 40; i < 60; i++) {
                channel.presence.enter(String.valueOf(i), msgComplete.add());
                Thread.sleep(100L);
            }
            /* get the history for this channel */
            PaginatedResult<PresenceMessage> messages = channel.presence.history(new Param[] {
                new Param("direction", "forwards"),
                new Param("start", String.valueOf(intervalStart - 500)),
                new Param("end", String.valueOf(intervalEnd + 500))
            });
            assertNotNull("Expected non-null messages", messages);
            assertEquals("Expected 20 messages", messages.items().length, 20);
            /* verify message order */
            for(int i = 20; i < 40; i++)
                assertEquals("Expect correct message data", messages.items()[i - 20].data, String.valueOf(i));
        } catch (AblyException e) {
            e.printStackTrace();
            fail("presencehistory_time_f: Unexpected exception");
        } catch (InterruptedException e) {
            e.printStackTrace();
            fail("presencehistory_time_f: Unexpected exception");
        } finally {
            if(ably != null)
                ably.close();
        }
    }
@ Test public void presencehistory_time_f ( ) { AblyRealtime ably = null ; try { /* first, publish some messages */ long intervalStart = 0 , intervalEnd = 0 ; ClientOptions rtOpts = createOptions ( ) ; rtOpts . token = token . token ; rtOpts . clientId = testClientId ; ably = new AblyRealtime ( rtOpts ) ; String channelName = "persisted:presencehistory_time_f_" + testParams . name ; /* create a channel */ final Channel channel = ably . channels . get ( channelName ) ; /* attach */ channel . attach ( ) ; ( new ChannelWaiter ( channel ) ) . waitFor ( ChannelState . attached ) ; assertEquals ( "Verify attached state reached" , channel . state , ChannelState . attached ) ; /* send batches of messages with short inter-message delay */ CompletionSet msgComplete = new CompletionSet ( ) ; for ( int i = 0 ; i < 20 ; i ++ ) { channel . presence . enter ( String . valueOf ( i ) , msgComplete . add ( ) ) ; Thread . sleep ( 100L ) ; } Thread . sleep ( 1000L ) ; intervalStart = timeOffset + System . currentTimeMillis ( ) ; for ( int i = 20 ; i < 40 ; i ++ ) { channel . presence . enter ( String . valueOf ( i ) , msgComplete . add ( ) ) ; Thread . sleep ( 100L ) ; } intervalEnd = timeOffset + System . currentTimeMillis ( ) - 1 ; Thread . sleep ( 1000L ) ; for ( int i = 40 ; i < 60 ; i ++ ) { channel . presence . enter ( String . valueOf ( i ) , msgComplete . add ( ) ) ; Thread . sleep ( 100L ) ; } /* get the history for this channel */ PaginatedResult < PresenceMessage > messages = channel . presence . history ( new Param [ ] { new Param ( "direction" , "forwards" ) , new Param ( "start" , String . valueOf ( intervalStart - 500 ) ) , new Param ( "end" , String . valueOf ( intervalEnd + 500 ) ) } ) ; assertNotNull ( "Expected non-null messages" , messages ) ; assertEquals ( "Expected 20 messages" , messages . items ( ) . length , 20 ) ; /* verify message order */ for ( int i = 20 ; i < 40 ; i ++ ) assertEquals ( "Expect correct message data" , messages . items ( ) [ i - 20 ] . data , String . valueOf ( i ) ) ; } catch ( AblyException e ) { e . printStackTrace ( ) ; fail ( "presencehistory_time_f: Unexpected exception" ) ; } catch ( InterruptedException e ) { e . printStackTrace ( ) ; fail ( "presencehistory_time_f: Unexpected exception" ) ; } finally { if ( ably != null ) ably . close ( ) ; } }
Publish events and check expected history based on time slice (forwards)
Publish events and check expected history based on time slice ( forwards )