use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class TestDebugger method testDebugger4.
@Test(dependsOnMethods = "testDebugger3")
public void testDebugger4() throws InterruptedException {
log.info("Siddi Debugger Test 4: Test next traversal in a query with time batch window where next call delays" + " 1 sec");
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
final String query = "@info(name = 'query1')" + "from cseEventStream#window.timeBatch(5 sec) " + "select symbol, price, volume " + "insert into OutputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
inEventCount.addAndGet(events.length);
AssertJUnit.assertEquals("Cannot emit all three in one time", 1, events.length);
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
siddhiDebugger.acquireBreakPoint("query1", SiddhiDebugger.QueryTerminal.IN);
siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {
@Override
public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
log.info(event.toString());
int count = debugEventCount.addAndGet(getCount(event));
if (count != 1 && queryTerminal == SiddhiDebugger.QueryTerminal.IN) {
try {
Thread.sleep(1100);
} catch (InterruptedException e) {
}
}
// next call will not reach OUT since there is a window
debugger.next();
}
});
inputHandler.send(new Object[] { "WSO2", 50f, 60 });
inputHandler.send(new Object[] { "WSO2", 70f, 40 });
inputHandler.send(new Object[] { "WSO2", 60f, 50 });
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> inEventCount.get() == 3 && debugEventCount.get() == 3);
AssertJUnit.assertEquals("Invalid number of output events", 3, inEventCount.get());
AssertJUnit.assertEquals("Invalid number of debug events", 3, debugEventCount.get());
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class TestDebugger method testDebugger10.
@Test(dependsOnMethods = "testDebugger8")
public void testDebugger10() throws InterruptedException {
log.info("Siddi Debugger Test 10: Test next traversal in a query with two consequent streams");
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "@config(async = 'true') " + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream stockEventStream (symbol string, price float, volume int); ";
final String query = "@info(name = 'query1')" + "from cseEventStream " + "select symbol, price, volume " + "insert into stockEventStream; " + "@info(name = 'query2')" + "from stockEventStream " + "select * " + "insert into OutputStream;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
inEventCount.addAndGet(events.length);
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
siddhiDebugger.acquireBreakPoint("query1", SiddhiDebugger.QueryTerminal.IN);
siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {
@Override
public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
log.info("Query: " + queryName + ":" + queryTerminal);
log.info(event.toString());
int count = debugEventCount.addAndGet(getCount(event));
if ((count - 1) / 4 == 0) {
// First four events
AssertJUnit.assertArrayEquals("Incorrect debug event received", new Object[] { "WSO2", 50f, 60 }, event.getOutputData());
} else {
// Next four events
AssertJUnit.assertArrayEquals("Incorrect debug event received", new Object[] { "WSO2", 70f, 40 }, event.getOutputData());
}
if (count == 1 || count == 5) {
AssertJUnit.assertEquals("Incorrect break point", "query1IN", queryName + queryTerminal);
} else if (count == 2 || count == 6) {
AssertJUnit.assertEquals("Incorrect break point", "query1OUT", queryName + queryTerminal);
} else if (count == 3 || count == 7) {
AssertJUnit.assertEquals("Incorrect break point", "query2IN", queryName + queryTerminal);
} else {
AssertJUnit.assertEquals("Incorrect break point", "query2OUT", queryName + queryTerminal);
}
debugger.next();
}
});
inputHandler.send(new Object[] { "WSO2", 50f, 60 });
inputHandler.send(new Object[] { "WSO2", 70f, 40 });
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> inEventCount.get() == 2 && debugEventCount.get() == 8);
AssertJUnit.assertEquals("Invalid number of output events", 2, inEventCount.get());
AssertJUnit.assertEquals("Invalid number of debug events", 8, debugEventCount.get());
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class CronWindowTestCase method testCronWindow2.
@Test
public void testCronWindow2() throws InterruptedException {
log.info("Testing cron window for expired events");
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseEventWindow (symbol string, price float, volume int) cron('*/5 * * * * ?') output " + "expired events; ";
String query = "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') from cseEventWindow " + "select symbol,price,volume " + "insert expired events into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
for (Event event : events) {
removeEventCount++;
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 700f, 0 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 1 });
Thread.sleep(7000);
inputHandler.send(new Object[] { "IBM1", 700f, 0 });
inputHandler.send(new Object[] { "WSO22", 60.5f, 1 });
Thread.sleep(7000);
inputHandler.send(new Object[] { "IBM43", 700f, 0 });
inputHandler.send(new Object[] { "WSO4343", 60.5f, 1 });
Thread.sleep(7000);
AssertJUnit.assertEquals(4, removeEventCount);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class CustomJoinWindowTestCase method testJoinWindowWithWindowForDeadLock.
/**
* Window shares locks event outside of queries. This test is to ensure that there is no
* deadlock according to the implementation.
*
* @throws InterruptedException throw exception if interrupted the input handler sender.
*/
@Test
public void testJoinWindowWithWindowForDeadLock() throws InterruptedException {
log.info("Test join window with another window for deadlock");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream TempStream(deviceID long, roomNo int, temp double); " + "define stream RegulatorStream(deviceID long, roomNo int, isOn bool); " + "define window TempWindow(deviceID long, roomNo int, temp double) time(1 min); " + "define window RegulatorWindow(deviceID long, roomNo int, isOn bool) length(1); " + "define window RegulatorActionWindow(deviceID long, roomNo int, action string) timeBatch(1 sec); ";
String query = "" + "@info(name = 'query1') " + "from TempStream[temp > 30.0] " + "insert into TempWindow; " + "" + "@info(name = 'query2') " + "from RegulatorStream[isOn == false] " + "insert into RegulatorWindow; " + "" + "@info(name = 'query3') " + "from TempWindow " + "join RegulatorWindow " + "on TempWindow.roomNo == RegulatorWindow.roomNo " + "select RegulatorWindow.deviceID as deviceID, TempWindow.roomNo as roomNo, 'start' as action " + "insert into RegulatorActionWindow; " + "" + "@info(name = 'query5') " + "from RegulatorActionWindow join TempWindow " + "on TempWindow.roomNo == RegulatorActionWindow.roomNo " + "select RegulatorActionWindow.deviceID as deviceID, TempWindow.roomNo as roomNo, true as isOn " + "insert into OutputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
inEventCount++;
}
});
InputHandler tempStream = siddhiAppRuntime.getInputHandler("TempStream");
InputHandler regulatorStream = siddhiAppRuntime.getInputHandler("RegulatorStream");
siddhiAppRuntime.start();
tempStream.send(new Object[] { 100L, 1, 20.0 });
tempStream.send(new Object[] { 100L, 2, 25.0 });
tempStream.send(new Object[] { 100L, 3, 30.0 });
tempStream.send(new Object[] { 100L, 4, 35.0 });
tempStream.send(new Object[] { 100L, 5, 40.0 });
regulatorStream.send(new Object[] { 100L, 1, false });
regulatorStream.send(new Object[] { 100L, 2, false });
regulatorStream.send(new Object[] { 100L, 3, false });
regulatorStream.send(new Object[] { 100L, 4, false });
regulatorStream.send(new Object[] { 100L, 5, false });
Thread.sleep(1500);
assertEquals("Number of success events", 2, inEventCount);
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class CustomJoinWindowTestCase method testJoinWindowWithStream.
@Test
public void testJoinWindowWithStream() throws InterruptedException {
log.info("Test join window with a stream");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream TempStream(deviceID long, roomNo int, temp double); " + "define stream RegulatorStream(deviceID long, roomNo int, isOn bool); " + "define window TempWindow(deviceID long, roomNo int, temp double) time(1 min); ";
String query = "" + "@info(name = 'query1') " + "from TempStream[temp > 30.0] " + "insert into TempWindow;" + "" + "@info(name = 'query2') " + "from TempWindow " + "join RegulatorStream[isOn == false]#window.length(1) as R " + "on TempWindow.roomNo == R.roomNo " + "select TempWindow.roomNo, R.deviceID, 'start' as action " + "insert into RegulatorActionStream;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("RegulatorActionStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
inEventCount++;
}
});
InputHandler tempStream = siddhiAppRuntime.getInputHandler("TempStream");
InputHandler regulatorStream = siddhiAppRuntime.getInputHandler("RegulatorStream");
siddhiAppRuntime.start();
tempStream.send(new Object[] { 100L, 1, 20.0 });
tempStream.send(new Object[] { 100L, 2, 25.0 });
tempStream.send(new Object[] { 100L, 3, 30.0 });
tempStream.send(new Object[] { 100L, 4, 35.0 });
tempStream.send(new Object[] { 100L, 5, 40.0 });
regulatorStream.send(new Object[] { 100L, 1, false });
regulatorStream.send(new Object[] { 100L, 2, false });
regulatorStream.send(new Object[] { 100L, 3, false });
regulatorStream.send(new Object[] { 100L, 4, false });
regulatorStream.send(new Object[] { 100L, 5, false });
Thread.sleep(500);
assertEquals("Number of success events", 2, inEventCount);
siddhiAppRuntime.shutdown();
}
Aggregations