Search in sources :

Example 66 with QueryCallback

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

the class TimeOutputRateLimitTestCase method testTimeOutputRateLimitQuery8.

@Test
public void testTimeOutputRateLimitQuery8() throws InterruptedException {
    log.info("TimeOutputRateLimit test8");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('EventOutputRateLimitTest8') " + "" + "define stream LoginEvents (timestamp long, ip string);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.lengthBatch(2) " + "select ip " + "group by ip " + "output last every 1 sec " + "insert 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 += inEvents.length;
            } else {
                AssertJUnit.fail("Remove events emitted");
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.9" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.4" });
    Thread.sleep(1100);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.4" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.4" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.30" });
    Thread.sleep(1200);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event value", 5, count);
    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 67 with QueryCallback

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

the class TimeOutputRateLimitTestCase method testTimeOutputRateLimitQuery11.

@Test
public void testTimeOutputRateLimitQuery11() throws InterruptedException {
    log.info("TimeOutputRateLimit test11");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('EventOutputRateLimitTest11') " + "" + "define stream LoginEvents (timestamp long, ip string);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.lengthBatch(2) " + "select ip, count(*) as total " + "group by ip " + "output first 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) {
                inEventCount += inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount += removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.9" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.4" });
    Thread.sleep(1100);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.4" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.4" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.30" });
    Thread.sleep(1100);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output in event value", 5, inEventCount);
    AssertJUnit.assertEquals("Number of output remove event value", 2, removeEventCount);
    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 68 with QueryCallback

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

the class TimeOutputRateLimitTestCase method testTimeOutputRateLimitQuery4.

@Test
public void testTimeOutputRateLimitQuery4() throws InterruptedException {
    log.info("TimeOutputRateLimit test4");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('EventOutputRateLimitTest4') " + "" + "define stream LoginEvents (timestamp long, ip string);" + "" + "@info(name = 'query1') " + "from LoginEvents " + "select ip " + "output first every 1 sec " + "insert 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 += inEvents.length;
                AssertJUnit.assertTrue("192.10.1.5".equals(inEvents[0].getData(0)) || "192.10.1.9".equals(inEvents[0].getData(0)) || "192.10.1.30".equals(inEvents[0].getData(0)));
            } else {
                AssertJUnit.fail("Remove events emitted");
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3" });
    Thread.sleep(1100);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.9" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.4" });
    Thread.sleep(1100);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.30" });
    Thread.sleep(1000);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event value", 3, count);
    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 69 with QueryCallback

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

the class StdDevAttributeAggregatorTestCase method stdDevAggregatorTest2.

@Test
public void stdDevAggregatorTest2() throws InterruptedException {
    log.info("stdDevAggregator Test #2: Single event in the stream");
    SiddhiManager siddhiManager = new SiddhiManager();
    String execPlan = "" + "@app:name('stdDevAggregatorTests') " + "" + "define stream cseEventStream (symbol string, price double);" + "" + "@info(name = 'query1') " + "from cseEventStream#window.lengthBatch(1) " + "select stdDev(price) as deviation " + "group by symbol " + "insert into outputStream;";
    SiddhiAppRuntime execPlanRunTime = siddhiManager.createSiddhiAppRuntime(execPlan);
    execPlanRunTime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            AssertJUnit.assertTrue(Math.abs((Double) inEvents[0].getData(0) - 0) < epsilon);
        }
    });
    InputHandler inputHandler = execPlanRunTime.getInputHandler("cseEventStream");
    execPlanRunTime.start();
    inputHandler.send(new Object[] { "WSO2", 1.0 });
    Thread.sleep(100);
    execPlanRunTime.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 70 with QueryCallback

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

the class StdDevAttributeAggregatorTestCase method stdDevAggregatorTest4.

@Test
public void stdDevAggregatorTest4() throws InterruptedException {
    log.info("stdDevAggregator Test #4: Two symbols in the stream with same stdDev");
    SiddhiManager siddhiManager = new SiddhiManager();
    String execPlan = "" + "@app:name('stdDevAggregatorTests') " + "" + "define stream cseEventStream (symbol string, price double);" + "" + "@info(name = 'query1') " + "from cseEventStream#window.lengthBatch(6) " + "select stdDev(price) as deviation " + "group by symbol " + "insert into outputStream;";
    SiddhiAppRuntime execPlanRunTime = siddhiManager.createSiddhiAppRuntime(execPlan);
    execPlanRunTime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            AssertJUnit.assertTrue(Math.abs((Double) inEvents[0].getData(0) - 0.40825) < epsilon);
            AssertJUnit.assertTrue(Math.abs((Double) inEvents[1].getData(0) - 0.40825) < epsilon);
        }
    });
    InputHandler inputHandler = execPlanRunTime.getInputHandler("cseEventStream");
    execPlanRunTime.start();
    inputHandler.send(new Object[] { "WSO2", 10000.0 });
    inputHandler.send(new Object[] { "WSO2", 10000.5 });
    inputHandler.send(new Object[] { "WSO2", 10001.0 });
    inputHandler.send(new Object[] { "IBM", 1.0 });
    inputHandler.send(new Object[] { "IBM", 1.5 });
    inputHandler.send(new Object[] { "IBM", 2.0 });
    Thread.sleep(600);
    execPlanRunTime.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