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