Search in sources :

Example 1 with TestProcessorSupplierContext

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

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

the class TransformUsingContextPTest method testEqualCooperativity.

private void testEqualCooperativity(boolean cooperative) {
    ContextFactory<String> contextFactory = ContextFactory.withCreateFn(jet -> "foo");
    if (!cooperative) {
        contextFactory = contextFactory.nonCooperative();
    }
    ProcessorSupplier supplier = supplier(contextFactory, mapToContext());
    supplier.init(new TestProcessorSupplierContext());
    assertEquals(cooperative, supplier.get(1).iterator().next().isCooperative());
}
Also used : ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) TestProcessorSupplierContext(com.hazelcast.jet.core.test.TestProcessorSupplierContext)

Example 3 with TestProcessorSupplierContext

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

the class SinksTest method mapWithMerging_when_multipleValuesForSingleKeyInABatch.

@Test
public void mapWithMerging_when_multipleValuesForSingleKeyInABatch() throws Exception {
    ProcessorMetaSupplier metaSupplier = adaptSupplier(SinkProcessors.<Entry<String, Integer>, String, Integer>mergeMapP(sinkName, Entry::getKey, Entry::getValue, Integer::sum));
    TestProcessorSupplierContext psContext = new TestProcessorSupplierContext().setHazelcastInstance(member);
    Processor p = TestSupport.supplierFrom(metaSupplier, psContext).get();
    TestOutbox outbox = new TestOutbox();
    p.init(outbox, new TestProcessorContext().setHazelcastInstance(member));
    TestInbox inbox = new TestInbox();
    inbox.add(entry("k", 1));
    inbox.add(entry("k", 2));
    p.process(0, inbox);
    assertTrue("inbox.isEmpty()", inbox.isEmpty());
    assertTrueEventually(() -> {
        assertTrue("p.complete()", p.complete());
    }, 10);
    p.close();
    // assert the output map contents
    IMap<Object, Object> actual = member.getMap(sinkName);
    assertEquals(1, actual.size());
    assertEquals(3, actual.get("k"));
}
Also used : Processor(com.hazelcast.jet.core.Processor) EntryProcessor(com.hazelcast.map.EntryProcessor) TestInbox(com.hazelcast.jet.core.test.TestInbox) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) TestProcessorSupplierContext(com.hazelcast.jet.core.test.TestProcessorSupplierContext) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Aggregations

TestProcessorSupplierContext (com.hazelcast.jet.core.test.TestProcessorSupplierContext)3 ProcessorSupplier (com.hazelcast.jet.core.ProcessorSupplier)2 TestOutbox (com.hazelcast.jet.core.test.TestOutbox)2 TestProcessorContext (com.hazelcast.jet.core.test.TestProcessorContext)2 Test (org.junit.Test)2 Traverser (com.hazelcast.jet.Traverser)1 Processor (com.hazelcast.jet.core.Processor)1 ProcessorMetaSupplier (com.hazelcast.jet.core.ProcessorMetaSupplier)1 ResettableSingletonTraverser (com.hazelcast.jet.core.ResettableSingletonTraverser)1 TestInbox (com.hazelcast.jet.core.test.TestInbox)1 DistributedTriFunction (com.hazelcast.jet.function.DistributedTriFunction)1 TransformUsingContextP.supplier (com.hazelcast.jet.impl.processor.TransformUsingContextP.supplier)1 ContextFactory (com.hazelcast.jet.pipeline.ContextFactory)1 EntryProcessor (com.hazelcast.map.EntryProcessor)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 SlowTest (com.hazelcast.test.annotation.SlowTest)1 Assert.assertEquals (org.junit.Assert.assertEquals)1