use of com.datatorrent.stram.engine.AbstractReservoir in project apex-core by apache.
the class InlineStreamTest method test.
@Test
@SuppressWarnings("SleepWhileInLoop")
public void test() throws Exception {
final int totalTupleCount = 5000;
final PassThroughNode<Object> operator1 = new PassThroughNode<>();
final GenericNode node1 = new GenericNode(operator1, new OperatorContext(1, "operator1", new DefaultAttributeMap(), null));
node1.setId(1);
operator1.setup(node1.context);
final PassThroughNode<Object> operator2 = new PassThroughNode<>();
final GenericNode node2 = new GenericNode(operator2, new OperatorContext(2, "operator2", new DefaultAttributeMap(), null));
node2.setId(2);
operator2.setup(node2.context);
StreamContext streamContext = new StreamContext("node1->node2");
final InlineStream stream = new InlineStream(1024);
stream.setup(streamContext);
node1.connectOutputPort("output", stream);
node2.connectInputPort("input", stream.getReservoir());
prev = null;
Sink<Object> sink = new Sink<Object>() {
@Override
public void put(Object payload) {
if (payload instanceof Tuple) {
return;
}
if (prev == null) {
prev = payload;
} else {
if (Integer.valueOf(payload.toString()) - Integer.valueOf(prev.toString()) != 1) {
synchronized (InlineStreamTest.this) {
InlineStreamTest.this.notify();
}
}
prev = payload;
}
if (Integer.valueOf(prev.toString()) == totalTupleCount - 1) {
synchronized (InlineStreamTest.this) {
InlineStreamTest.this.notify();
}
}
}
@Override
public int getCount(boolean reset) {
return 0;
}
};
node2.connectOutputPort("output", sink);
AbstractReservoir reservoir1 = AbstractReservoir.newReservoir("input", 1024 * 5);
node1.connectInputPort("input", reservoir1);
Map<Integer, Node<?>> activeNodes = new ConcurrentHashMap<>();
launchNodeThread(node1, activeNodes);
launchNodeThread(node2, activeNodes);
stream.activate(streamContext);
reservoir1.put(StramTestSupport.generateBeginWindowTuple("irrelevant", 0));
for (int i = 0; i < totalTupleCount; i++) {
reservoir1.put(i);
}
reservoir1.put(StramTestSupport.generateEndWindowTuple("irrelevant", 0));
synchronized (this) {
this.wait(200);
}
Assert.assertNotNull(prev);
Assert.assertEquals("processing complete", totalTupleCount, Integer.valueOf(prev.toString()) + 1);
Assert.assertEquals("active operators", 2, activeNodes.size());
WaitCondition c = new WaitCondition() {
@Override
public boolean isComplete() {
final SweepableReservoir reservoir = stream.getReservoir();
logger.debug("stream {} empty {}, size {}", stream, reservoir.isEmpty(), reservoir.size(false));
return reservoir.isEmpty();
}
};
Assert.assertTrue("operator should finish processing all events within 1 second", StramTestSupport.awaitCompletion(c, 1000));
stream.deactivate();
for (Node<?> node : activeNodes.values()) {
node.shutdown();
}
for (int i = 0; i < 10; i++) {
Thread.sleep(20);
if (activeNodes.isEmpty()) {
break;
}
}
stream.teardown();
operator2.teardown();
operator1.teardown();
Assert.assertEquals("active operators", 0, activeNodes.size());
}
Aggregations