Search in sources :

Example 56 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery16.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery15" })
public void testSnapshotOutputRateLimitQuery16() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test16");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest16') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(1 sec) " + "select  ip " + "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("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
            if (inEvents != null) {
                count.incrementAndGet();
                for (Event event : inEvents) {
                    AssertJUnit.assertTrue("192.10.1.5".equals(event.getData(0)) || "192.10.1.3".equals(event.getData(0)));
                }
                value += inEvents.length;
            }
        }
    });
    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() == 2 && value == 4);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event bundles with inEvents", 2, count.get());
    AssertJUnit.assertEquals("Number of output event", 4, value);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 57 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery15.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery14" })
public void testSnapshotOutputRateLimitQuery15() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test15");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest15') " + "" + "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("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
            if (inEvents != null) {
                value++;
            } else if (value == 1) {
                AssertJUnit.assertNull(inEvents);
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    Thread.sleep(100);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3" });
    Thread.sleep(2200);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3" });
    Thread.sleep(2200);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event value", 2, value);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 58 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery20.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery19" })
public void testSnapshotOutputRateLimitQuery20() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test20");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest20') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(5 sec) " + "select  ip, 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("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
            if (inEvents != null) {
                count.incrementAndGet();
                if (count.get() == 1 || count.get() == 2) {
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(1) == 3L && (Long) inEvents[1].getData(1) == 6L);
                } else if (count.get() == 3 || count.get() == 4 || count.get() == 5) {
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(1) == 5L && (Long) inEvents[1].getData(1) == 16L);
                    AssertJUnit.assertTrue((Long) inEvents[2].getData(1) == 5L && (Long) inEvents[3].getData(1) == 16L);
                } else if (count.get() == 6 || count.get() == 7) {
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(1) == 2L && (Long) inEvents[1].getData(1) == 10L);
                }
                value += inEvents.length;
            }
        }
    });
    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 == 20);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event bundles", 7, count.get());
    AssertJUnit.assertEquals("Number of output event value", 20, value);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 59 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery17.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery16" })
public void testSnapshotOutputRateLimitQuery17() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test17");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest17') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(5 sec) " + "select  ip " + "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("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                count.incrementAndGet();
                value += inEvents.length;
                eventArrived = true;
            }
        }
    });
    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 == 20);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event bundles equal to 5 ", 7, count.get());
    AssertJUnit.assertEquals("Number of output event value", 20, value);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 60 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery19.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery18" })
public void testSnapshotOutputRateLimitQuery19() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test19");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest19') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(5 sec) " + "select  ip, 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("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
            if (inEvents != null) {
                count.incrementAndGet();
                if (count.get() == 1 || count.get() == 2) {
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(1) == 9L);
                    AssertJUnit.assertTrue((Long) inEvents[1].getData(1) == 9L);
                } else if (count.get() == 3 || count.get() == 4 || count.get() == 5) {
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(1) == 21L);
                    AssertJUnit.assertTrue((Long) inEvents[1].getData(1) == 21L);
                    AssertJUnit.assertTrue((Long) inEvents[2].getData(1) == 21L);
                    AssertJUnit.assertTrue((Long) inEvents[3].getData(1) == 21L);
                } else if (count.get() == 6 || count.get() == 7) {
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(1) == 12L);
                    AssertJUnit.assertTrue((Long) inEvents[1].getData(1) == 12L);
                }
            }
        }
    });
    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);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event value", 7, count.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)575 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)574 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)574 Test (org.testng.annotations.Test)574 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)572 Event (org.ballerinalang.siddhi.core.event.Event)569 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)73 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)73 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)73 ArrayList (java.util.ArrayList)25 InMemoryPersistenceStore (org.ballerinalang.siddhi.core.util.persistence.InMemoryPersistenceStore)11 PersistenceStore (org.ballerinalang.siddhi.core.util.persistence.PersistenceStore)11 CannotRestoreSiddhiAppStateException (org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException)10 StringConcatAggregatorString (org.ballerinalang.siddhi.core.query.extension.util.StringConcatAggregatorString)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)4 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)1