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