Search in sources :

Example 76 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class WithinPatternTestCase method testQuery2.

@Test
public void testQuery2() throws InterruptedException {
    log.info("testPatternWithin2 - OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "from (every e1=Stream1[price>20]-> e2=Stream2[price>e1.price]) " + "within 1 sec " + "select e1.symbol as symbol1, e2.symbol as symbol2 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
                AssertJUnit.assertArrayEquals(new Object[] { "GOOG", "IBM" }, inEvents[0].getData());
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(1500);
    stream1.send(new Object[] { "GOOG", 54f, 100 });
    Thread.sleep(500);
    stream2.send(new Object[] { "IBM", 55.7f, 100 });
    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();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 77 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class WithinPatternTestCase method testQuery3.

@Test
public void testQuery3() throws InterruptedException {
    log.info("testPatternWithin3 - OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "from (every (e1=Stream1[price>20] -> e3=Stream1[price>20]) -> e2=Stream2[price>e1.price]) " + "within 2 sec " + "select e1.price as price1, e3.price as price3, e2.price as price2 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
                AssertJUnit.assertArrayEquals(new Object[] { 53.6f, 53f, 57.7f }, inEvents[0].getData());
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(600);
    stream1.send(new Object[] { "GOOG", 54f, 100 });
    Thread.sleep(600);
    stream1.send(new Object[] { "WSO2", 53.6f, 100 });
    Thread.sleep(900);
    stream1.send(new Object[] { "GOOG", 53f, 100 });
    Thread.sleep(600);
    stream2.send(new Object[] { "IBM", 57.7f, 100 });
    Thread.sleep(600);
    AssertJUnit.assertEquals("Number of success events", 1, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 78 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class WithinPatternTestCase method testQuery7.

@Test
public void testQuery7() throws InterruptedException {
    log.info("testPatternWithin5 - OUT 1 : Within clause for grouped pattern states(3)");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "from every (e1=Stream1 -> e2=Stream1[symbol == e1.symbol] -> e3=Stream1[symbol == e2.symbol]) " + "within 5 sec " + " select e1.symbol as symbol1, e1.volume as volume1, e2.symbol as symbol2, e2.volume as volume2, " + " e3.symbol as symbol3, e3.volume as volume3" + " insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
                AssertJUnit.assertArrayEquals(new Object[] { "WSO2", 150, "WSO2", 200, "WSO2", 250 }, inEvents[0].getData());
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(6000);
    stream1.send(new Object[] { "WSO2", 56.6f, 150 });
    stream1.send(new Object[] { "WSO2", 57.7f, 200 });
    Thread.sleep(500);
    stream1.send(new Object[] { "WSO2", 58.7f, 250 });
    stream1.send(new Object[] { "WSO2", 57.7f, 300 });
    stream1.send(new Object[] { "WSO2", 59.7f, 350 });
    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();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 79 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery18.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery17" })
public void testSnapshotOutputRateLimitQuery18() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test18");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest18') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(1 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) {
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(1) == 9L);
                } else if (count.get() == 2) {
                // AssertJUnit.assertTrue((Long) inEvents[0].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 });
    Thread.sleep(1200);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event value", 2, count.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 80 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery24.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery23" })
public void testSnapshotOutputRateLimitQuery24() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test24");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest23') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.lengthBatch(3) " + "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("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
            count.incrementAndGet();
            for (Event inEvent : inEvents) {
                value++;
                if (value == 1) {
                    AssertJUnit.assertEquals((Long) 4L, inEvent.getData(0));
                }
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    Thread.sleep(100);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5", 3 });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3", 6 });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.4", 2 });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5", 1 });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.6", 1 });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.7", 2 });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.8", 10 });
    Thread.sleep(1200);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event bundles", 1, count.get());
    AssertJUnit.assertEquals("Number of output events", 1, value);
    siddhiAppRuntime.shutdown();
    Thread.sleep(2000);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)824 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)823 Test (org.testng.annotations.Test)822 SiddhiManager (io.siddhi.core.SiddhiManager)821 Event (io.siddhi.core.event.Event)818 InputHandler (io.siddhi.core.stream.input.InputHandler)817 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)75 SiddhiApp (io.siddhi.query.api.SiddhiApp)74 Query (io.siddhi.query.api.execution.query.Query)74 ArrayList (java.util.ArrayList)43 Logger (org.apache.logging.log4j.core.Logger)26 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)23 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)21 InMemoryPersistenceStore (io.siddhi.core.util.persistence.InMemoryPersistenceStore)11 IncrementalFileSystemPersistenceStore (io.siddhi.core.util.persistence.IncrementalFileSystemPersistenceStore)11 PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 StreamCallback (io.siddhi.core.stream.output.StreamCallback)7 StringConcatAggregatorExecutorString (io.siddhi.core.query.extension.util.StringConcatAggregatorExecutorString)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6