src/it/java/com/stijndepestel/geminio/CaptureReplayIT.java
/**
     * Capture/Replay integration test.
     */
Capture/Replay integration test.
Capture / Replay integration test .
@Test
    public void captureReplayIT() {
        final TestEvent[] eventsToThrow = new TestEvent[CaptureReplayIT.NUMBER_OF_EVENTS_TO_THROW];
        for (int i = 0; i < eventsToThrow.length; i++) {
            eventsToThrow[i] = new TestEvent();
        }
        final ArrayList<TestEvent> catchedEvents = new ArrayList<>(
                CaptureReplayIT.NUMBER_OF_EVENTS_TO_THROW);
        final Consumer<TestEvent> eventCatcher = event -> {
            final long deviation = System.currentTimeMillis()
                    - this.previousTimestamp
                    - CaptureReplayIT.INTERVAL_BETWEEN_EVENTS;
            Assert.assertTrue("Error margin to big: " + deviation,
                    deviation < CaptureReplayIT.ACCEPTABLE_ERROR_MARGIN);
            catchedEvents.add(event);
            this.previousTimestamp = System.currentTimeMillis();
        };
        final Capture<TestEvent> capture = new Capture<>(TestHelper::serialize,
                json -> this.persistedJSON = json.toString());
        final Replay<TestEvent> replay = new Replay<>(TestHelper::deserialize,
                () -> new JSONObject(this.persistedJSON), eventCatcher);
        capture.startCapture();
        final Stream<TestEvent> stream = Arrays.stream(eventsToThrow);
        stream.forEach(event -> {
            try {
                Thread.sleep(CaptureReplayIT.INTERVAL_BETWEEN_EVENTS);
            } catch (final Exception e) {
            }
            capture.capture(event);
        });
        stream.close();
        capture.stopCapture();
        capture.saveEvents();
        replay.load();
        this.previousTimestamp = System.currentTimeMillis();
        final TestReplayListener listener = new TestReplayListener();
        replay.addReplayListener(listener);
        replay.startReplay();
        Awaitility.await().atMost(3, TimeUnit.MINUTES)
                .until(this.hasReplayEnded(listener));
        Assert.assertArrayEquals(eventsToThrow, catchedEvents.toArray());
        Assert.assertTrue("Replay is in stopped state.", replay.hasEnded());
    }
@ Test public void captureReplayIT ( ) { final TestEvent [ ] eventsToThrow = new TestEvent [ CaptureReplayIT . NUMBER_OF_EVENTS_TO_THROW ] ; for ( int i = 0 ; i < eventsToThrow . length ; i ++ ) { eventsToThrow [ i ] = new TestEvent ( ) ; } final ArrayList < TestEvent > catchedEvents = new ArrayList < > ( CaptureReplayIT . NUMBER_OF_EVENTS_TO_THROW ) ; final Consumer < TestEvent > eventCatcher = event -> { final long deviation = System . currentTimeMillis ( ) - this . previousTimestamp - CaptureReplayIT . INTERVAL_BETWEEN_EVENTS ; Assert . assertTrue ( "Error margin to big: " + deviation , deviation < CaptureReplayIT . ACCEPTABLE_ERROR_MARGIN ) ; catchedEvents . add ( event ) ; this . previousTimestamp = System . currentTimeMillis ( ) ; } ; final Capture < TestEvent > capture = new Capture < > ( TestHelper :: serialize , json -> this . persistedJSON = json . toString ( ) ) ; final Replay < TestEvent > replay = new Replay < > ( TestHelper :: deserialize , ( ) -> new JSONObject ( this . persistedJSON ) , eventCatcher ) ; capture . startCapture ( ) ; final Stream < TestEvent > stream = Arrays . stream ( eventsToThrow ) ; stream . forEach ( event -> { try { Thread . sleep ( CaptureReplayIT . INTERVAL_BETWEEN_EVENTS ) ; } catch ( final Exception e ) { } capture . capture ( event ) ; } ) ; stream . close ( ) ; capture . stopCapture ( ) ; capture . saveEvents ( ) ; replay . load ( ) ; this . previousTimestamp = System . currentTimeMillis ( ) ; final TestReplayListener listener = new TestReplayListener ( ) ; replay . addReplayListener ( listener ) ; replay . startReplay ( ) ; Awaitility . await ( ) . atMost ( 3 , TimeUnit . MINUTES ) . until ( this . hasReplayEnded ( listener ) ) ; Assert . assertArrayEquals ( eventsToThrow , catchedEvents . toArray ( ) ) ; Assert . assertTrue ( "Replay is in stopped state." , replay . hasEnded ( ) ) ; }
Capture/Replay integration test.
Capture / Replay integration test .
// Create the events to throw. // Create the arraylist to store the thrown events during replay. // Function that serves as an event catcher. // Deviation of intended time. // Assert deviation is inside the error margin. // Add the caught event to the arraylist. // Set the previous time stamp for the next iteration. // The capture object to use during the test. // The replay object to use during the test. // Start the capture // ignore // Stop the capture // Persist the events (using the consumer passed during construction). // Load the persisted events in the replayer (using the supplier passed // during construction). // Set the previoustimestamp for the first iteration. // set listener // Start the replay. // Await till replay has finished // Assert that the catched events are equal to the replayed events (deep // equals).