Search in sources :

Example 1 with TestInbox

use of com.hazelcast.jet.core.test.TestInbox in project hazelcast-jet by hazelcast.

the class StreamKafkaPTest method when_snapshotSaved_then_offsetsRestored.

@Test
public void when_snapshotSaved_then_offsetsRestored() throws Exception {
    StreamKafkaP processor = createProcessor(2, StreamKafkaP::recordToEntry, 10_000);
    TestOutbox outbox = new TestOutbox(new int[] { 10 }, 10);
    processor.init(outbox, new TestProcessorContext().setProcessingGuarantee(ProcessingGuarantee.EXACTLY_ONCE));
    produce(topic1Name, 0, "0");
    assertEquals(entry(0, "0"), consumeEventually(processor, outbox));
    // create snapshot
    TestInbox snapshot = saveSnapshot(processor, outbox);
    Set snapshotItems = unwrapBroadcastKey(snapshot.queue());
    // consume one more item
    produce(topic1Name, 1, "1");
    assertEquals(entry(1, "1"), consumeEventually(processor, outbox));
    // create new processor and restore snapshot
    processor = createProcessor(2, StreamKafkaP::recordToEntry, 10_000);
    outbox = new TestOutbox(new int[] { 10 }, 10);
    processor.init(outbox, new TestProcessorContext().setProcessingGuarantee(ProcessingGuarantee.EXACTLY_ONCE));
    // restore snapshot
    processor.restoreFromSnapshot(snapshot);
    assertTrue("snapshot not fully processed", snapshot.isEmpty());
    TestInbox snapshot2 = saveSnapshot(processor, outbox);
    assertEquals("new snapshot not equal after restore", snapshotItems, unwrapBroadcastKey(snapshot2.queue()));
    // the second item should be produced one more time
    assertEquals(entry(1, "1"), consumeEventually(processor, outbox));
    assertNoMoreItems(processor, outbox);
}
Also used : Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) HashSet(java.util.HashSet) TestInbox(com.hazelcast.jet.core.test.TestInbox) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) Test(org.junit.Test)

Example 2 with TestInbox

use of com.hazelcast.jet.core.test.TestInbox in project hazelcast-jet by hazelcast.

the class SlidingWindowP_twoStageSnapshotTest method test.

@Test
public void test() {
    SlidingWindowP stage1p1 = stage1Supplier.get();
    SlidingWindowP stage1p2 = stage1Supplier.get();
    SlidingWindowP stage2p = stage2Supplier.get();
    TestOutbox stage1p1Outbox = newOutbox();
    TestOutbox stage1p2Outbox = newOutbox();
    TestOutbox stage2Outbox = newOutbox();
    TestInbox inbox = new TestInbox();
    TestProcessorContext context = new TestProcessorContext().setProcessingGuarantee(ProcessingGuarantee.EXACTLY_ONCE);
    stage1p1.init(stage1p1Outbox, context);
    stage1p2.init(stage1p2Outbox, context);
    stage2p.init(stage2Outbox, context);
    // process some events in the 1st stage
    // entry key is time
    assertTrue(stage1p1.tryProcess(0, entry(1L, 1L)));
    assertTrue(stage1p2.tryProcess(0, entry(2L, 2L)));
    assertTrue(stage1p1Outbox.queue(0).isEmpty() && stage2Outbox.queue(0).isEmpty());
    // save state in stage1
    assertTrue(stage1p1.saveToSnapshot());
    assertTrue(stage1p2.saveToSnapshot());
    assertTrue("something put to snapshot outbox in stage1", stage1p1Outbox.snapshotQueue().isEmpty() && stage1p2Outbox.snapshotQueue().isEmpty());
    assertEmptyState(stage1p1);
    assertEmptyState(stage1p2);
    // process normal outbox in stage2
    processStage2(stage2p, stage1p1Outbox, stage1p2Outbox, inbox);
    if (simulateRestore) {
        // create new instances for stage1
        stage1p1 = stage1Supplier.get();
        stage1p2 = stage1Supplier.get();
        stage1p1Outbox = newOutbox();
        stage1p2Outbox = newOutbox();
        stage1p1.init(stage1p1Outbox, context);
        stage1p2.init(stage1p2Outbox, context);
    }
    // process some more events in 1st stage
    assertTrue(stage1p1.tryProcess(0, entry(3L, 3L)));
    assertTrue(stage1p1.tryProcess(0, entry(4L, 4L)));
    // process flushing WM
    assertTrue(stage1p1.tryProcessWatermark(wm(10)));
    assertTrue(stage1p2.tryProcessWatermark(wm(10)));
    processStage2(stage2p, stage1p1Outbox, stage1p2Outbox, inbox);
    assertTrue(stage2p.tryProcessWatermark(wm(10)));
    // Then
    assertEquals(collectionToString(asList(outboxFrame(2, 1), outboxFrame(3, 3), outboxFrame(4, 6), outboxFrame(5, 10), outboxFrame(6, 9), outboxFrame(7, 7), outboxFrame(8, 4))), collectionToString(stage2Outbox.queue(0)));
}
Also used : TestInbox(com.hazelcast.jet.core.test.TestInbox) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) Processors.combineToSlidingWindowP(com.hazelcast.jet.core.processor.Processors.combineToSlidingWindowP) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) Test(org.junit.Test)

Example 3 with TestInbox

use of com.hazelcast.jet.core.test.TestInbox in project hazelcast-jet by hazelcast.

the class WriteLoggerPTest method test.

@Test
public void test() {
    // Given
    Processor p = supplierFrom(writeLoggerP()).get();
    TestInbox inbox = new TestInbox();
    Outbox outbox = mock(Outbox.class);
    ILogger logger = mock(ILogger.class);
    p.init(outbox, new TestProcessorContext().setLogger(logger));
    // When
    inbox.add(1);
    p.process(0, inbox);
    Watermark wm = new Watermark(2);
    p.tryProcessWatermark(wm);
    // Then
    verifyZeroInteractions(outbox);
    verify(logger).info("1");
    verify(logger).fine(wm.toString());
    verifyZeroInteractions(logger);
}
Also used : Processor(com.hazelcast.jet.core.Processor) TestInbox(com.hazelcast.jet.core.test.TestInbox) Outbox(com.hazelcast.jet.core.Outbox) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) ILogger(com.hazelcast.logging.ILogger) Watermark(com.hazelcast.jet.core.Watermark) Test(org.junit.Test)

Example 4 with TestInbox

use of com.hazelcast.jet.core.test.TestInbox in project hazelcast-jet by hazelcast.

the class WriteSocketTest method unitTest.

@Test
public void unitTest() throws Exception {
    AtomicInteger counter = new AtomicInteger();
    ServerSocket serverSocket = new ServerSocket(0);
    new Thread(() -> uncheckRun(() -> {
        Socket socket = serverSocket.accept();
        serverSocket.close();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
            while (reader.readLine() != null) {
                counter.incrementAndGet();
            }
        }
    })).start();
    TestInbox inbox = new TestInbox();
    range(0, ITEM_COUNT).forEach(inbox::add);
    Processor p = supplierFrom(writeSocketP("localhost", serverSocket.getLocalPort(), Object::toString, UTF_8)).get();
    p.init(mock(Outbox.class), new TestProcessorContext());
    p.process(0, inbox);
    p.complete();
    assertTrueEventually(() -> assertTrue(counter.get() >= ITEM_COUNT));
    // wait a little to check, if the counter doesn't get too far
    Thread.sleep(500);
    assertEquals(ITEM_COUNT, counter.get());
}
Also used : Processor(com.hazelcast.jet.core.Processor) InputStreamReader(java.io.InputStreamReader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestInbox(com.hazelcast.jet.core.test.TestInbox) BufferedReader(java.io.BufferedReader) Outbox(com.hazelcast.jet.core.Outbox) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 5 with TestInbox

use of com.hazelcast.jet.core.test.TestInbox in project hazelcast by hazelcast.

the class SlidingWindowP_changeWindowSizeTest method test.

private static void test(@Nonnull SlidingWindowPolicy policy1, @Nonnull SlidingWindowPolicy policy2, @Nullable List expectedOutboxAfterRestore) throws Exception {
    SlidingWindowP p = createProcessor(policy1);
    TestOutbox outbox = new TestOutbox(new int[] { 1024 }, 1024);
    p.init(outbox, new TestProcessorContext());
    TestInbox inbox = new TestInbox();
    inbox.addAll(asList(0, 1, 2, 3));
    p.process(0, inbox);
    p.tryProcessWatermark(wm(2));
    outbox.drainQueueAndReset(0, new ArrayList<>(), false);
    // take a snapshot and restore to a new processor with different window size
    p.saveToSnapshot();
    TestInbox snapshotInbox = new TestInbox();
    outbox.drainSnapshotQueueAndReset(snapshotInbox.queue(), false);
    p = createProcessor(policy2);
    p.init(outbox, new TestProcessorContext());
    for (Object o : snapshotInbox.queue()) {
        Entry e = (Entry) o;
        p.restoreFromSnapshot(e.getKey(), e.getValue());
    }
    p.finishSnapshotRestore();
    while (!p.complete()) {
    }
    if (expectedOutboxAfterRestore != null) {
        assertEquals(expectedOutboxAfterRestore, new ArrayList<>(outbox.queue(0)));
    }
    assertTrue("tsToKeyToAcc not empty", p.tsToKeyToAcc.isEmpty());
    assertTrue("slidingWindow not empty", p.slidingWindow == null || p.slidingWindow.isEmpty());
}
Also used : Entry(java.util.Map.Entry) TestInbox(com.hazelcast.jet.core.test.TestInbox) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext)

Aggregations

TestInbox (com.hazelcast.jet.core.test.TestInbox)22 TestOutbox (com.hazelcast.jet.core.test.TestOutbox)16 TestProcessorContext (com.hazelcast.jet.core.test.TestProcessorContext)16 Test (org.junit.Test)13 Processor (com.hazelcast.jet.core.Processor)11 QuickTest (com.hazelcast.test.annotation.QuickTest)8 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)7 Watermark (com.hazelcast.jet.core.Watermark)6 SimpleTestInClusterSupport (com.hazelcast.jet.SimpleTestInClusterSupport)4 Outbox (com.hazelcast.jet.core.Outbox)4 Before (org.junit.Before)4 FunctionEx (com.hazelcast.function.FunctionEx)3 Traverser (com.hazelcast.jet.Traverser)3 ProcessorMetaSupplier (com.hazelcast.jet.core.ProcessorMetaSupplier)3 ILogger (com.hazelcast.logging.ILogger)3 ArrayList (java.util.ArrayList)3 Arrays.asList (java.util.Arrays.asList)3 Collection (java.util.Collection)3 Collections.singletonList (java.util.Collections.singletonList)3 Entry (java.util.Map.Entry)3