use of org.ballerinalang.siddhi.core.stream.input.InputHandler in project ballerina by ballerina-lang.
the class LenghtBatchWindowTestCase method testLengthBatchWindow5.
@Test
public void testLengthBatchWindow5() throws InterruptedException {
final int length = 2;
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseWindow (symbol string, price float, volume int) lengthBatch(" + length + ") output " + "expired events; ";
String query = "@info(name = 'query1') from cseEventStream select symbol,price,volume insert into cseWindow ;" + "@info(name = 'query2') from cseWindow " + "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) {
count++;
AssertJUnit.assertEquals("Remove event order", count, event.getData(2));
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 700f, 1 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
inputHandler.send(new Object[] { "IBM", 700f, 3 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
inputHandler.send(new Object[] { "IBM", 700f, 5 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
Thread.sleep(500);
AssertJUnit.assertEquals("Remove event count", 4, count);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.stream.input.InputHandler in project ballerina by ballerina-lang.
the class LenghtBatchWindowTestCase method testLengthBatchWindow6.
@Test
public void testLengthBatchWindow6() throws InterruptedException {
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseWindow (symbol string, price float, volume int) lengthBatch(4); ";
String query = "@info(name = 'query1') from cseEventStream select symbol,price,volume insert into cseWindow; " + "@info(name = 'query2') from cseWindow " + "select symbol,sum(price) as sumPrice,volume " + "insert 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) {
AssertJUnit.assertEquals("Events cannot be expired", false, event.isExpired());
inEventCount++;
if (inEventCount == 1) {
AssertJUnit.assertEquals(100.0, event.getData(1));
} else if (inEventCount == 2) {
AssertJUnit.assertEquals(240.0, event.getData(1));
}
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 10f, 0 });
inputHandler.send(new Object[] { "WSO2", 20f, 1 });
inputHandler.send(new Object[] { "IBM", 30f, 0 });
inputHandler.send(new Object[] { "WSO2", 40f, 1 });
inputHandler.send(new Object[] { "IBM", 50f, 0 });
inputHandler.send(new Object[] { "WSO2", 60f, 1 });
inputHandler.send(new Object[] { "WSO2", 60f, 1 });
inputHandler.send(new Object[] { "IBM", 70f, 0 });
inputHandler.send(new Object[] { "WSO2", 80f, 1 });
Thread.sleep(500);
siddhiAppRuntime.shutdown();
AssertJUnit.assertEquals(2, inEventCount);
AssertJUnit.assertTrue(eventArrived);
}
use of org.ballerinalang.siddhi.core.stream.input.InputHandler in project ballerina by ballerina-lang.
the class LenghtBatchWindowTestCase method testLengthBatchWindow7.
@Test
public void testLengthBatchWindow7() throws InterruptedException {
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseWindow (symbol string, price float, volume int) lengthBatch(4); ";
String query = "@info(name = 'query1') from cseEventStream select symbol,price,volume insert into cseWindow; " + "@info(name = 'query2') from cseWindow " + "select symbol,sum(price) as sumPrice,volume " + "insert into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
siddhiAppRuntime.addCallback("query2", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
AssertJUnit.assertEquals("Events cannot be expired", false, removeEvents != null);
for (Event event : inEvents) {
inEventCount++;
if (inEventCount == 1) {
AssertJUnit.assertEquals(100.0, event.getData(1));
} else if (inEventCount == 2) {
AssertJUnit.assertEquals(240.0, event.getData(1));
}
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 10f, 0 });
inputHandler.send(new Object[] { "WSO2", 20f, 1 });
inputHandler.send(new Object[] { "IBM", 30f, 0 });
inputHandler.send(new Object[] { "WSO2", 40f, 1 });
inputHandler.send(new Object[] { "IBM", 50f, 0 });
inputHandler.send(new Object[] { "WSO2", 60f, 1 });
inputHandler.send(new Object[] { "WSO2", 60f, 1 });
inputHandler.send(new Object[] { "IBM", 70f, 0 });
inputHandler.send(new Object[] { "WSO2", 80f, 1 });
Thread.sleep(500);
siddhiAppRuntime.shutdown();
AssertJUnit.assertEquals(2, inEventCount);
AssertJUnit.assertTrue(eventArrived);
}
use of org.ballerinalang.siddhi.core.stream.input.InputHandler in project ballerina by ballerina-lang.
the class JoinTableTestCase method testTableJoinQuery1.
@Test
public void testTableJoinQuery1() throws InterruptedException {
log.info("testTableJoinQuery1 - OUT 2");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream StockStream (symbol2 string, price2 float, volume2 long); " + "define stream CheckStockStream (symbol1 string); " + "define table StockTable (symbol2 string, price2 float, volume2 long); ";
String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from CheckStockStream#window.length(1) join StockTable " + "select symbol1, symbol2, volume2 " + "insert into OutputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("query2", new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
if (inEvents != null) {
for (Event event : inEvents) {
inEventCount++;
switch(inEventCount) {
case 1:
AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "WSO2", 100L }, event.getData());
break;
case 2:
AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "IBM", 10L }, event.getData());
break;
default:
AssertJUnit.assertSame(2, inEventCount);
}
}
eventArrived = true;
}
if (removeEvents != null) {
removeEventCount = removeEventCount + removeEvents.length;
}
eventArrived = true;
}
});
InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
siddhiAppRuntime.start();
stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
stockStream.send(new Object[] { "IBM", 75.6f, 10L });
checkStockStream.send(new Object[] { "WSO2" });
Thread.sleep(500);
AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.stream.input.InputHandler in project ballerina by ballerina-lang.
the class JoinTableTestCase method testTableJoinQuery3.
@Test
public void testTableJoinQuery3() throws InterruptedException {
log.info("testTableJoinQuery3 - OUT 1");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string); " + "define table StockTable (symbol string, price float, volume long); ";
String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from CheckStockStream#window.length(1) join StockTable as t " + " on CheckStockStream.symbol!=t.symbol " + "select CheckStockStream.symbol as checkSymbol, t.symbol as symbol, t.volume as volume " + "insert into OutputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("query2", new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
if (inEvents != null) {
for (Event event : inEvents) {
inEventCount++;
switch(inEventCount) {
case 1:
AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "IBM", 10L }, event.getData());
break;
default:
AssertJUnit.assertSame(1, inEventCount);
}
}
eventArrived = true;
}
if (removeEvents != null) {
removeEventCount = removeEventCount + removeEvents.length;
}
eventArrived = true;
}
});
InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
siddhiAppRuntime.start();
stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
stockStream.send(new Object[] { "IBM", 75.6f, 10L });
checkStockStream.send(new Object[] { "WSO2" });
Thread.sleep(500);
AssertJUnit.assertEquals("Number of success events", 1, inEventCount);
AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
siddhiAppRuntime.shutdown();
}
Aggregations