use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery7.
@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery6" })
public void testSnapshotOutputRateLimitQuery7() throws InterruptedException {
log.info("SnapshotOutputRateLimit test7");
SiddhiManager siddhiManager = new SiddhiManager();
String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest7') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(5 sec) " + "select sum(calls) as totalCalls " + "group by ip " + "output snapshot every 1 sec " + "insert all events into uniqueIps ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
log.info("Running : " + siddhiAppRuntime.getName());
siddhiAppRuntime.addCallback("uniqueIps", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
eventArrived = true;
count.incrementAndGet();
value += events.length;
if (count.get() == 1 || count.get() == 2) {
AssertJUnit.assertTrue((Long) events[0].getData(0) == 3L || (Long) events[1].getData(0) == 6L);
} else if (count.get() == 3) {
AssertJUnit.assertTrue((Long) events[0].getData(0) == 5L || (Long) events[1].getData(0) == 16L);
}
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
siddhiAppRuntime.start();
Thread.sleep(1100);
inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5", 3 });
inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3", 6 });
Thread.sleep(2200);
inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5", 2 });
inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3", 10 });
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> eventArrived && count.get() == 7 && value == 14);
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
AssertJUnit.assertEquals("Number of output event bundles", 7, count.get());
AssertJUnit.assertTrue("Number of output event value", 14 == value);
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery11.
@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery10" })
public void testSnapshotOutputRateLimitQuery11() throws InterruptedException {
log.info("SnapshotOutputRateLimit test11");
SiddhiManager siddhiManager = new SiddhiManager();
String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest11') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(1 sec) " + "select ip " + "output snapshot every 1 sec " + "insert all events into uniqueIps ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
log.info("Running : " + siddhiAppRuntime.getName());
siddhiAppRuntime.addCallback("uniqueIps", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
eventArrived = true;
for (Event event : events) {
if (event.isExpired()) {
AssertJUnit.fail("Remove events emitted");
} else {
count.incrementAndGet();
AssertJUnit.assertTrue("192.10.1.5".equals(event.getData(0)) || "192.10.1.3".equals(event.getData(0)));
}
}
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
siddhiAppRuntime.start();
Thread.sleep(1200);
inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3" });
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> eventArrived && count.get() == 2);
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
AssertJUnit.assertEquals("Number of output event value", 2, count.get());
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery1.
@Test
public void testSnapshotOutputRateLimitQuery1() throws InterruptedException {
log.info("SnapshotOutputRateLimit test1");
SiddhiManager siddhiManager = new SiddhiManager();
String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest1') " + "" + "define stream LoginEvents (timestamp long, ip string);" + "" + "@info(name = 'query1') " + "from LoginEvents " + "select ip " + "output snapshot every 1 sec " + "insert all events into uniqueIps ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
log.info("Running : " + siddhiAppRuntime.getName());
siddhiAppRuntime.addCallback("uniqueIps", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
for (Event event : events) {
eventArrived = true;
if (event.isExpired()) {
AssertJUnit.fail("Remove events emitted");
} else {
count.incrementAndGet();
AssertJUnit.assertTrue("192.10.1.3".equals(event.getData(0)));
}
}
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
eventsSent++;
Thread.sleep(10);
inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3" });
eventsSent++;
SiddhiTestHelper.waitForEvents(100, 1, count, 60000);
siddhiAppRuntime.shutdown();
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> eventArrived && count.get() == 1);
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
AssertJUnit.assertTrue("Number of output event value", 1 == count.get());
Thread.sleep(2000);
}
use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery8.
@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery7" })
public void testSnapshotOutputRateLimitQuery8() throws InterruptedException {
log.info("SnapshotOutputRateLimit test8");
SiddhiManager siddhiManager = new SiddhiManager();
String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest8') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(1 sec) " + "select sum(calls) as totalCalls " + "output snapshot every 1 sec " + "insert all events into uniqueIps ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
log.info("Running : " + siddhiAppRuntime.getName());
siddhiAppRuntime.addCallback("uniqueIps", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
eventArrived = true;
count.incrementAndGet();
if (count.get() == 1) {
AssertJUnit.assertTrue((Long) events[0].getData(0) == 9L);
} else if (count.get() == 2) {
AssertJUnit.assertTrue((Long) events[0].getData(0) == 12L);
}
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
siddhiAppRuntime.start();
Thread.sleep(1100);
inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5", 3 });
Thread.sleep(100);
inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3", 6 });
Thread.sleep(2200);
inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5", 2 });
Thread.sleep(100);
inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3", 10 });
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> eventArrived && count.get() == 2);
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
AssertJUnit.assertEquals("Number of output event value", 2, count.get());
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class DistinctCountAttributeAggregatorTestCase method distinctCountTest.
@Test
public void distinctCountTest() throws InterruptedException {
log.info("Distinct Count TestCase");
SiddhiManager siddhiManager = new SiddhiManager();
String inStreamDefinition = "" + "define stream inputStream (eventId string, userID string, pageID string); ";
String query = "" + "@info(name = 'query1') " + "from inputStream#window.time(5 sec) " + "select userID, pageID, distinctCount(pageID) as distinctPages " + "group by userID " + "having distinctPages > 3 " + "insert into outputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inStreamDefinition + query);
siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {
@Override
public void receive(org.ballerinalang.siddhi.core.event.Event[] events) {
for (org.ballerinalang.siddhi.core.event.Event event : events) {
AssertJUnit.assertEquals("User ID", "USER_1", event.getData(0));
AssertJUnit.assertEquals("Page ID", "WEB_PAGE_4", event.getData(1));
AssertJUnit.assertEquals("Distinct Pages", 4L, event.getData(2));
count++;
}
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("inputStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "E001", "USER_1", "WEB_PAGE_1" });
Thread.sleep(1000);
inputHandler.send(new Object[] { "E002", "USER_2", "WEB_PAGE_1" });
Thread.sleep(1000);
// 1st Event in window
inputHandler.send(new Object[] { "E003", "USER_1", "WEB_PAGE_2" });
Thread.sleep(1000);
inputHandler.send(new Object[] { "E004", "USER_2", "WEB_PAGE_2" });
Thread.sleep(1000);
// 2nd Event in window
inputHandler.send(new Object[] { "E005", "USER_1", "WEB_PAGE_3" });
Thread.sleep(1000);
// 3rd Event in window
inputHandler.send(new Object[] { "E006", "USER_1", "WEB_PAGE_1" });
Thread.sleep(1000);
// 4th Event in window
inputHandler.send(new Object[] { "E007", "USER_1", "WEB_PAGE_4" });
Thread.sleep(1000);
inputHandler.send(new Object[] { "E008", "USER_2", "WEB_PAGE_2" });
Thread.sleep(1000);
inputHandler.send(new Object[] { "E009", "USER_1", "WEB_PAGE_1" });
Thread.sleep(1000);
inputHandler.send(new Object[] { "E010", "USER_2", "WEB_PAGE_1" });
Thread.sleep(2000);
siddhiAppRuntime.shutdown();
AssertJUnit.assertEquals("Event count", 1, count);
}
Aggregations