use of com.datatorrent.stram.tuple.EndWindowTuple in project apex-core by apache.
the class GenericNodeTest method testSynchingLogic.
@Test
@SuppressWarnings("SleepWhileInLoop")
public void testSynchingLogic() throws InterruptedException {
long sleeptime = 25L;
final ArrayList<Object> list = new ArrayList<>();
GenericOperator go = new GenericOperator();
final GenericNode gn = new GenericNode(go, new com.datatorrent.stram.engine.OperatorContext(0, "operator", new DefaultAttributeMap(), null));
gn.setId(1);
AbstractReservoir reservoir1 = AbstractReservoir.newReservoir("ip1Res", 1024);
AbstractReservoir reservoir2 = AbstractReservoir.newReservoir("ip2Res", 1024);
Sink<Object> output = new Sink<Object>() {
@Override
public void put(Object tuple) {
list.add(tuple);
}
@Override
public int getCount(boolean reset) {
return 0;
}
};
gn.connectInputPort("ip1", reservoir1);
gn.connectInputPort("ip2", reservoir2);
gn.connectOutputPort("op", output);
gn.firstWindowMillis = 0;
gn.windowWidthMillis = 100;
final AtomicBoolean ab = new AtomicBoolean(false);
Thread t = new Thread() {
@Override
public void run() {
ab.set(true);
gn.activate();
gn.run();
gn.deactivate();
}
};
t.start();
do {
Thread.sleep(sleeptime);
} while (ab.get() == false);
Tuple beginWindow1 = new Tuple(MessageType.BEGIN_WINDOW, 0x1L);
reservoir1.add(beginWindow1);
Thread.sleep(sleeptime);
Assert.assertEquals(1, list.size());
reservoir2.add(beginWindow1);
Thread.sleep(sleeptime);
Assert.assertEquals(1, list.size());
Tuple endWindow1 = new EndWindowTuple(0x1L);
reservoir1.add(endWindow1);
Thread.sleep(sleeptime);
Assert.assertEquals(1, list.size());
Tuple beginWindow2 = new Tuple(MessageType.BEGIN_WINDOW, 0x2L);
reservoir1.add(beginWindow2);
Thread.sleep(sleeptime);
Assert.assertEquals(1, list.size());
reservoir2.add(endWindow1);
Thread.sleep(sleeptime);
Assert.assertEquals(3, list.size());
reservoir2.add(beginWindow2);
Thread.sleep(sleeptime);
Assert.assertEquals(3, list.size());
Tuple endWindow2 = new EndWindowTuple(0x2L);
reservoir2.add(endWindow2);
Thread.sleep(sleeptime);
Assert.assertEquals(3, list.size());
reservoir1.add(endWindow2);
Thread.sleep(sleeptime);
Assert.assertEquals(4, list.size());
EndStreamTuple est = new EndStreamTuple(0L);
reservoir1.add(est);
Thread.sleep(sleeptime);
Assert.assertEquals(4, list.size());
Tuple beginWindow3 = new Tuple(MessageType.BEGIN_WINDOW, 0x3L);
reservoir2.add(beginWindow3);
Thread.sleep(sleeptime);
Assert.assertEquals(5, list.size());
Tuple endWindow3 = new EndWindowTuple(0x3L);
reservoir2.add(endWindow3);
Thread.sleep(sleeptime);
Assert.assertEquals(6, list.size());
Assert.assertNotSame(Thread.State.TERMINATED, t.getState());
reservoir2.add(est);
Thread.sleep(sleeptime);
Assert.assertEquals(7, list.size());
Thread.sleep(sleeptime);
Assert.assertEquals(Thread.State.TERMINATED, t.getState());
}
use of com.datatorrent.stram.tuple.EndWindowTuple in project apex-core by apache.
the class GenericNodeTest method testPrematureTermination.
@Test
public void testPrematureTermination() throws InterruptedException {
long maxSleep = 5000;
long sleeptime = 25L;
GenericOperator go = new GenericOperator();
final GenericNode gn = new GenericNode(go, new com.datatorrent.stram.engine.OperatorContext(0, "operator", new DefaultAttributeMap(), null));
gn.setId(1);
AbstractReservoir reservoir1 = AbstractReservoir.newReservoir("ip1Res", 1024);
AbstractReservoir reservoir2 = AbstractReservoir.newReservoir("ip2Res", 1024);
gn.connectInputPort("ip1", reservoir1);
gn.connectInputPort("ip2", reservoir2);
gn.connectOutputPort("op", Sink.BLACKHOLE);
gn.firstWindowMillis = 0;
gn.windowWidthMillis = 100;
final AtomicBoolean ab = new AtomicBoolean(false);
Thread t = new Thread() {
@Override
public void run() {
ab.set(true);
gn.activate();
gn.run();
gn.deactivate();
}
};
t.start();
long interval = 0;
do {
Thread.sleep(sleeptime);
interval += sleeptime;
} while ((ab.get() == false) && (interval < maxSleep));
int controlTupleCount = gn.controlTupleCount;
Tuple beginWindow1 = new Tuple(MessageType.BEGIN_WINDOW, 0x1L);
reservoir1.add(beginWindow1);
reservoir2.add(beginWindow1);
interval = 0;
do {
Thread.sleep(sleeptime);
interval += sleeptime;
} while ((gn.controlTupleCount == controlTupleCount) && (interval < maxSleep));
Assert.assertTrue("Begin window called", go.endWindowId != go.beginWindowId);
controlTupleCount = gn.controlTupleCount;
Tuple endWindow1 = new EndWindowTuple(0x1L);
reservoir1.add(endWindow1);
reservoir2.add(endWindow1);
interval = 0;
do {
Thread.sleep(sleeptime);
interval += sleeptime;
} while ((gn.controlTupleCount == controlTupleCount) && (interval < maxSleep));
Assert.assertTrue("End window called", go.endWindowId == go.beginWindowId);
controlTupleCount = gn.controlTupleCount;
Tuple beginWindow2 = new Tuple(MessageType.BEGIN_WINDOW, 0x2L);
reservoir1.add(beginWindow2);
reservoir2.add(beginWindow2);
interval = 0;
do {
Thread.sleep(sleeptime);
interval += sleeptime;
} while ((gn.controlTupleCount == controlTupleCount) && (interval < maxSleep));
gn.shutdown();
t.join();
Assert.assertTrue("End window not called", go.endWindowId != go.beginWindowId);
}
use of com.datatorrent.stram.tuple.EndWindowTuple in project apex-core by apache.
the class FastStreamTest method testBufferServerStream.
/**
* Test buffer server stream by sending
* tuple on outputstream and receive same tuple from inputstream
*
* @throws Exception
*/
@Test
@SuppressWarnings({ "SleepWhileInLoop" })
public void testBufferServerStream() throws Exception {
final StreamCodec<Object> serde = new DefaultStatefulStreamCodec<>();
final AtomicInteger messageCount = new AtomicInteger();
Sink<Object> sink = new Sink<Object>() {
@Override
public void put(Object tuple) {
logger.debug("received: " + tuple);
messageCount.incrementAndGet();
}
@Override
public int getCount(boolean reset) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
String streamName = "streamName";
String upstreamNodeId = "upstreamNodeId";
String downstreamNodeId = "downStreamNodeId";
StreamContext issContext = new StreamContext(streamName);
issContext.setSourceId(upstreamNodeId);
issContext.setSinkId(downstreamNodeId);
issContext.setFinishedWindowId(-1);
issContext.setBufferServerAddress(InetSocketAddress.createUnresolved("localhost", bufferServerPort));
issContext.put(StreamContext.CODEC, serde);
issContext.put(StreamContext.EVENT_LOOP, eventloop);
FastSubscriber subscriber = new FastSubscriber(downstreamNodeId, 1024);
subscriber.setup(issContext);
SweepableReservoir reservoir = subscriber.acquireReservoir("testReservoir", 1);
reservoir.setSink(sink);
StreamContext ossContext = new StreamContext(streamName);
ossContext.setSourceId(upstreamNodeId);
ossContext.setSinkId(downstreamNodeId);
ossContext.setFinishedWindowId(-1);
ossContext.setBufferServerAddress(InetSocketAddress.createUnresolved("localhost", bufferServerPort));
ossContext.put(StreamContext.CODEC, serde);
ossContext.put(StreamContext.EVENT_LOOP, eventloop);
FastPublisher publisher = new FastPublisher(upstreamNodeId, 8);
StreamContext publisherContext = new StreamContext(streamName);
publisherContext.setSourceId(upstreamNodeId);
publisherContext.setSinkId(downstreamNodeId);
publisherContext.setBufferServerAddress(InetSocketAddress.createUnresolved("localhost", bufferServerPort));
publisherContext.put(StreamContext.CODEC, serde);
publisherContext.put(StreamContext.EVENT_LOOP, eventloop);
publisher.setup(publisherContext);
subscriber.activate(issContext);
LOG.debug("input stream activated");
publisher.activate(publisherContext);
LOG.debug("output stream activated");
LOG.debug("Sending hello message");
publisher.put(StramTestSupport.generateBeginWindowTuple(upstreamNodeId, 0));
publisher.put(StramTestSupport.generateTuple("hello", 0));
publisher.put(StramTestSupport.generateEndWindowTuple(upstreamNodeId, 0));
// it's a spurious tuple, presence of it should not affect the outcome of the test.
publisher.put(StramTestSupport.generateBeginWindowTuple(upstreamNodeId, 1));
for (int i = 0; i < 100; i++) {
Tuple t = reservoir.sweep();
if (t == null) {
sleep(5);
continue;
}
reservoir.remove();
if (t instanceof EndWindowTuple) {
break;
}
}
eventloop.disconnect(publisher);
eventloop.disconnect(subscriber);
Assert.assertEquals("Received messages", 1, messageCount.get());
}
use of com.datatorrent.stram.tuple.EndWindowTuple in project apex-core by apache.
the class SocketStreamTest method verify.
@After
public void verify() throws InterruptedException {
for (int i = 0; i < 100; i++) {
Tuple t = reservoir.sweep();
if (t == null) {
sleep(5);
continue;
}
reservoir.remove();
if (t instanceof EndWindowTuple) {
break;
}
}
eventloop.disconnect(oss);
eventloop.disconnect(iss);
Assert.assertEquals("Received messages", 1, messageCount.get());
}
Aggregations