Search in sources :

Example 1 with ContextFactory

use of com.hazelcast.jet.pipeline.ContextFactory 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)

Aggregations

Traverser (com.hazelcast.jet.Traverser)1 ProcessorSupplier (com.hazelcast.jet.core.ProcessorSupplier)1 ResettableSingletonTraverser (com.hazelcast.jet.core.ResettableSingletonTraverser)1 TestOutbox (com.hazelcast.jet.core.test.TestOutbox)1 TestProcessorContext (com.hazelcast.jet.core.test.TestProcessorContext)1 TestProcessorSupplierContext (com.hazelcast.jet.core.test.TestProcessorSupplierContext)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 Assert.assertEquals (org.junit.Assert.assertEquals)1 Test (org.junit.Test)1