Search in sources :

Example 96 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime 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 97 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class AbsentPatternTestCase method testQueryAbsent10.

@Test(dependsOnMethods = { "testQueryAbsent9" })
public void testQueryAbsent10() throws InterruptedException {
    log.info("Test the query e1 -> e2 -> not e3 with e1, e2 and e3 which does not meet the condition for 1 sec");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); " + "define stream Stream3 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "from e1=Stream1[price>10] -> e2=Stream2[price>20] -> not Stream3[price>30] for 1 sec " + "select e1.symbol as symbol1, e2.symbol as symbol2 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    TestUtil.TestCallback callback = TestUtil.addQueryCallback(siddhiAppRuntime, "query1", new Object[] { "WSO2", "IBM" });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    InputHandler stream3 = siddhiAppRuntime.getInputHandler("Stream3");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 15.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 28.7f, 100 });
    Thread.sleep(100);
    stream3.send(new Object[] { "GOOGLE", 25.7f, 100 });
    TestUtil.waitForInEvents(500, callback, 5);
    callback.throwAssertionErrors();
    AssertJUnit.assertEquals("Number of success events", 1, callback.getInEventCount());
    AssertJUnit.assertEquals("Number of remove events", 0, callback.getRemoveEventCount());
    AssertJUnit.assertTrue("Event arrived", callback.isEventArrived());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) TestUtil(io.siddhi.core.TestUtil) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 98 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery12.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery11" })
public void testSnapshotOutputRateLimitQuery12() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test12");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest12') " + "" + "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);
            count.incrementAndGet();
            eventArrived = true;
        }
    });
    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);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event value", 1, 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) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 99 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime 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 100 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery3.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery2" })
public void testSnapshotOutputRateLimitQuery3() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test3");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest3') " + "" + "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);
            eventArrived = true;
            for (Event event : events) {
                if (event.isExpired()) {
                    AssertJUnit.fail("Remove events emitted");
                } else {
                    count.incrementAndGet();
                    AssertJUnit.assertTrue("192.10.1.3".equals(event.getData(0)) || "192.10.1.4".equals(event.getData(0)));
                }
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
    Thread.sleep(100);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3" });
    Thread.sleep(2200);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.9" });
    Thread.sleep(100);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.4" });
    Thread.sleep(1100);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertTrue("Number of output event value", 3 == count.get());
    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) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1761 SiddhiManager (io.siddhi.core.SiddhiManager)1756 Test (org.testng.annotations.Test)1744 InputHandler (io.siddhi.core.stream.input.InputHandler)1590 Event (io.siddhi.core.event.Event)1257 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)823 StreamCallback (io.siddhi.core.stream.output.StreamCallback)305 TestUtil (io.siddhi.core.TestUtil)304 SiddhiApp (io.siddhi.query.api.SiddhiApp)90 Query (io.siddhi.query.api.execution.query.Query)85 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)84 ArrayList (java.util.ArrayList)73 Logger (org.apache.logging.log4j.core.Logger)57 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)34 InMemoryBroker (io.siddhi.core.util.transport.InMemoryBroker)32 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)30 HashMap (java.util.HashMap)25 UnitTestAppender (io.siddhi.core.UnitTestAppender)23 InMemoryConfigManager (io.siddhi.core.util.config.InMemoryConfigManager)21 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)15