Search in sources :

Example 6 with TestProcessorContext

use of com.hazelcast.jet.core.test.TestProcessorContext 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 7 with TestProcessorContext

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

the class TransformUsingContextPTest method testSharing.

private void testSharing(boolean share) {
    int[] createCounter = { 0 };
    int[] destroyCounter = { 0 };
    ContextFactory<String> contextFactory = ContextFactory.withCreateFn(jet -> "context-" + createCounter[0]++).withDestroyFn(ctx -> destroyCounter[0]++);
    if (share) {
        contextFactory = contextFactory.shareLocally();
    }
    ProcessorSupplier supplier = supplier(contextFactory, mapToContext());
    TestOutbox outbox1 = new TestOutbox(1);
    TestOutbox outbox2 = new TestOutbox(1);
    supplier.init(new TestProcessorSupplierContext());
    assertEquals(share ? 1 : 0, createCounter[0]);
    // noinspection SuspiciousToArrayCall
    TransformUsingContextP[] processors = supplier.get(2).toArray(new TransformUsingContextP[0]);
    processors[0].init(outbox1, new TestProcessorContext());
    assertEquals(1, createCounter[0]);
    processors[1].init(outbox2, new TestProcessorContext());
    assertEquals(share ? 1 : 2, createCounter[0]);
    assertEquals(share, processors[0].contextObject == processors[1].contextObject);
    processors[0].tryProcess(0, "foo");
    processors[1].tryProcess(0, "foo");
    assertEquals("context-0", outbox1.queue(0).poll());
    assertEquals(share ? "context-0" : "context-1", outbox2.queue(0).poll());
    processors[0].close(null);
    assertEquals(share ? 0 : 1, destroyCounter[0]);
    processors[1].close(null);
    assertEquals(share ? 0 : 2, destroyCounter[0]);
    supplier.close(null);
    assertEquals(share ? 1 : 2, destroyCounter[0]);
}
Also used : Traverser(com.hazelcast.jet.Traverser) TestProcessorSupplierContext(com.hazelcast.jet.core.test.TestProcessorSupplierContext) ContextFactory(com.hazelcast.jet.pipeline.ContextFactory) TransformUsingContextP.supplier(com.hazelcast.jet.impl.processor.TransformUsingContextP.supplier) ResettableSingletonTraverser(com.hazelcast.jet.core.ResettableSingletonTraverser) Test(org.junit.Test) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) DistributedTriFunction(com.hazelcast.jet.function.DistributedTriFunction) ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) Assert.assertEquals(org.junit.Assert.assertEquals) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) TestProcessorSupplierContext(com.hazelcast.jet.core.test.TestProcessorSupplierContext)

Example 8 with TestProcessorContext

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

the class StreamEventJournalPTest method when_futureSequence_thenResetOffset.

@Test
public void when_futureSequence_thenResetOffset() {
    TestOutbox outbox = new TestOutbox(new int[] { 16 }, 16);
    StreamEventJournalP p = (StreamEventJournalP) supplier.get();
    // fill journal so that it overflows
    fillJournal(CAPACITY_PER_PARTITION + 1);
    // initial offsets will be 5, since capacity per partition is 5
    p.init(outbox, new TestProcessorContext());
    // clear partitions before doing any read, but after initializing offsets
    map.destroy();
    // when we consume, we should not retrieve anything because we will ask for
    // offset 5, but current head is 0. This should not cause any error
    List<Object> actual = new ArrayList<>();
    // we should not receive any items, but the offset should be reset back to 0
    assertTrueFiveSeconds(() -> {
        assertFalse("Processor should never complete", p.complete());
        outbox.drainQueueAndReset(0, actual, true);
        assertTrue("consumed different number of items than expected", actual.size() == 0);
    });
    // add one item to each partition
    fillJournal(1);
    // receive the items we just added
    assertTrueEventually(() -> {
        assertFalse("Processor should never complete", p.complete());
        outbox.drainQueueAndReset(0, actual, true);
        assertTrue("consumed different number of items than expected", actual.size() == 2);
    });
}
Also used : TestOutbox(com.hazelcast.jet.core.test.TestOutbox) ArrayList(java.util.ArrayList) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) Test(org.junit.Test)

Example 9 with TestProcessorContext

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

the class StreamSocketPTest method before.

@Before
public void before() {
    outbox = new TestOutbox(10);
    context = new TestProcessorContext();
    bucket = outbox.queue(0);
}
Also used : TestOutbox(com.hazelcast.jet.core.test.TestOutbox) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) Before(org.junit.Before)

Example 10 with TestProcessorContext

use of com.hazelcast.jet.core.test.TestProcessorContext 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)

Aggregations

TestProcessorContext (com.hazelcast.jet.core.test.TestProcessorContext)58 TestOutbox (com.hazelcast.jet.core.test.TestOutbox)50 Test (org.junit.Test)39 QuickTest (com.hazelcast.test.annotation.QuickTest)22 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)21 Processor (com.hazelcast.jet.core.Processor)17 TestInbox (com.hazelcast.jet.core.test.TestInbox)16 Watermark (com.hazelcast.jet.core.Watermark)13 ArrayList (java.util.ArrayList)12 Before (org.junit.Before)10 Entry (java.util.Map.Entry)9 Outbox (com.hazelcast.jet.core.Outbox)7 Context (com.hazelcast.jet.core.Processor.Context)6 ProcessorMetaSupplier (com.hazelcast.jet.core.ProcessorMetaSupplier)6 ProcessorSupplier (com.hazelcast.jet.core.ProcessorSupplier)6 FunctionEx (com.hazelcast.function.FunctionEx)4 SimpleTestInClusterSupport (com.hazelcast.jet.SimpleTestInClusterSupport)4 Traverser (com.hazelcast.jet.Traverser)4 LongAccumulator (com.hazelcast.jet.accumulator.LongAccumulator)4 HashSet (java.util.HashSet)4