Search in sources :

Example 36 with Processor

use of com.hazelcast.jet.core.Processor in project hazelcast by hazelcast.

the class StreamEventJournalP_WmCoalescingTest method when_allPartitionsIdleAndThenRecover_then_wmOutput.

@Test
public void when_allPartitionsIdleAndThenRecover_then_wmOutput() throws Exception {
    // Insert to map in parallel to verifyProcessor.
    CountDownLatch latch = new CountDownLatch(1);
    Thread updatingThread = new Thread(() -> uncheckRun(() -> {
        // We will start after a delay so that the source will first become idle and then recover.
        latch.await();
        for (; ; ) {
            map.put(partitionKeys[0], 12);
            Thread.sleep(100);
        }
    }));
    updatingThread.start();
    Processor processor = createSupplier(asList(0, 1), 2000).get();
    TestOutbox outbox = new TestOutbox(1024);
    Queue<Object> outbox0 = outbox.queue(0);
    processor.init(outbox, new TestProcessorContext().setHazelcastInstance(instance));
    assertTrueEventually(() -> {
        processor.complete();
        // after we have the IDLE_MESSAGE, release the latch to let the other thread produce events
        if (IDLE_MESSAGE.equals(outbox0.peek())) {
            latch.countDown();
        }
        assertEquals(asList(IDLE_MESSAGE, wm(12), 12), outbox0.stream().distinct().collect(toList()));
    });
    updatingThread.interrupt();
    updatingThread.join();
}
Also used : Processor(com.hazelcast.jet.core.Processor) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 37 with Processor

use of com.hazelcast.jet.core.Processor in project hazelcast by hazelcast.

the class StreamSocketPTest method runTest.

private void runTest(byte[] inputBytes, int inputSplitAfter) throws Exception {
    try (ServerSocket serverSocket = new ServerSocket(0)) {
        CountDownLatch firstPartWritten = new CountDownLatch(1);
        CountDownLatch readyForSecondPart = new CountDownLatch(1);
        Thread thread = new Thread(() -> uncheckRun(() -> {
            Socket socket = serverSocket.accept();
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(inputBytes, 0, inputSplitAfter);
            firstPartWritten.countDown();
            readyForSecondPart.await();
            outputStream.write(inputBytes, inputSplitAfter, inputBytes.length - inputSplitAfter);
            outputStream.close();
            socket.close();
        }));
        thread.start();
        Processor processor = supplierFrom(streamSocketP("localhost", serverSocket.getLocalPort(), UTF_16)).get();
        processor.init(outbox, context);
        firstPartWritten.await();
        for (int i = 0; i < 10; i++) {
            processor.complete();
            // sleep a little so that the processor has a chance to read the part of the data
            sleepMillis(1);
        }
        readyForSecondPart.countDown();
        while (!processor.complete()) {
            sleepMillis(1);
        }
        for (String s : output) {
            assertEquals(s, bucket.poll());
        }
        assertEquals(null, bucket.poll());
        assertTrueEventually(() -> assertFalse(thread.isAlive()));
    }
}
Also used : Processor(com.hazelcast.jet.core.Processor) OutputStream(java.io.OutputStream) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 38 with Processor

use of com.hazelcast.jet.core.Processor in project hazelcast by hazelcast.

the class StreamEventJournalPTest method assertRestore.

private void assertRestore(List<Entry> snapshotItems) throws Exception {
    Processor p = supplier.get();
    TestOutbox newOutbox = new TestOutbox(new int[] { 16 }, 16);
    List<Object> output = new ArrayList<>();
    p.init(newOutbox, new TestProcessorContext().setHazelcastInstance(instance));
    TestInbox inbox = new TestInbox();
    inbox.addAll(snapshotItems);
    p.restoreFromSnapshot(inbox);
    p.finishSnapshotRestore();
    assertTrueEventually(() -> {
        assertFalse("Processor should never complete", p.complete());
        newOutbox.drainQueueAndReset(0, output, true);
        assertEquals("consumed different number of items than expected", JOURNAL_CAPACITY, output.size());
    }, 3);
}
Also used : Processor(com.hazelcast.jet.core.Processor) TestInbox(com.hazelcast.jet.core.test.TestInbox) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) ArrayList(java.util.ArrayList) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext)

Example 39 with Processor

use of com.hazelcast.jet.core.Processor in project hazelcast by hazelcast.

the class StreamEventJournalPTest method when_newData.

@Test
public void when_newData() throws Exception {
    TestOutbox outbox = new TestOutbox(new int[] { 16 }, 16);
    List<Object> actual = new ArrayList<>();
    Processor p = supplier.get();
    p.init(outbox, new TestProcessorContext().setHazelcastInstance(instance));
    fillJournal(CAPACITY_PER_PARTITION);
    // consume
    assertTrueEventually(() -> {
        assertFalse("Processor should never complete", p.complete());
        outbox.drainQueueAndReset(0, actual, true);
        assertEquals("consumed different number of items than expected", JOURNAL_CAPACITY, actual.size());
        assertEquals(IntStream.range(0, JOURNAL_CAPACITY).boxed().collect(Collectors.toSet()), new HashSet<>(actual));
    }, 3);
    fillJournal(CAPACITY_PER_PARTITION);
    // consume again
    assertTrueEventually(() -> {
        assertFalse("Processor should never complete", p.complete());
        outbox.drainQueueAndReset(0, actual, true);
        assertEquals("consumed different number of items than expected", JOURNAL_CAPACITY + 2, actual.size());
        assertEquals(IntStream.range(0, JOURNAL_CAPACITY).boxed().collect(Collectors.toSet()), new HashSet<>(actual));
    }, 3);
}
Also used : Processor(com.hazelcast.jet.core.Processor) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) ArrayList(java.util.ArrayList) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 40 with Processor

use of com.hazelcast.jet.core.Processor in project hazelcast by hazelcast.

the class WriteLoggerPTest method test.

@Test
public void test() throws Exception {
    // 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) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

Processor (com.hazelcast.jet.core.Processor)49 Test (org.junit.Test)24 ArrayList (java.util.ArrayList)22 TestProcessorContext (com.hazelcast.jet.core.test.TestProcessorContext)17 QuickTest (com.hazelcast.test.annotation.QuickTest)17 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)16 TestOutbox (com.hazelcast.jet.core.test.TestOutbox)14 List (java.util.List)13 Nonnull (javax.annotation.Nonnull)13 TestInbox (com.hazelcast.jet.core.test.TestInbox)11 Watermark (com.hazelcast.jet.core.Watermark)10 Collection (java.util.Collection)9 Collections.singletonList (java.util.Collections.singletonList)9 Entry (java.util.Map.Entry)9 ProcessorSupplier (com.hazelcast.jet.core.ProcessorSupplier)8 FunctionEx (com.hazelcast.function.FunctionEx)7 Job (com.hazelcast.jet.Job)7 JobConfig (com.hazelcast.jet.config.JobConfig)7 DAG (com.hazelcast.jet.core.DAG)7 Arrays (java.util.Arrays)6