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)));
}
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]);
}
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);
});
}
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);
}
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);
}
Aggregations