Search in sources :

Example 86 with InputHandler

use of io.siddhi.core.stream.input.InputHandler in project siddhi by wso2.

the class LogicalPatternTestCase method testQuery7.

@Test
public void testQuery7() throws InterruptedException {
    log.info("testPatternLogical7 - 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 e1=Stream1[price > 20] and e2=Stream2[price >30] -> e3=Stream2['IBM' == symbol] " + "select e1.symbol as symbol1, e2.price as price2, e3.price as price3 " + "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", 72.7f, 4.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(100);
    stream2.send(new Object[] { "GOOG", 72.7f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 4.7f, 100 });
    Thread.sleep(100);
    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 87 with InputHandler

use of io.siddhi.core.stream.input.InputHandler in project siddhi by wso2.

the class LogicalPatternTestCase method testQuery1.

@Test
public void testQuery1() throws InterruptedException {
    log.info("testPatternLogical1 - 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 e1=Stream1[price > 20] -> e2=Stream2[price > e1.price] or e3=Stream2['IBM' == symbol] " + "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[] { "WSO2", "GOOG" }, 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(100);
    stream2.send(new Object[] { "GOOG", 59.6f, 100 });
    Thread.sleep(100);
    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 88 with InputHandler

use of io.siddhi.core.stream.input.InputHandler 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 89 with InputHandler

use of io.siddhi.core.stream.input.InputHandler 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 90 with InputHandler

use of io.siddhi.core.stream.input.InputHandler 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)

Aggregations

InputHandler (io.siddhi.core.stream.input.InputHandler)1591 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1590 SiddhiManager (io.siddhi.core.SiddhiManager)1585 Test (org.testng.annotations.Test)1575 Event (io.siddhi.core.event.Event)1216 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)817 TestUtil (io.siddhi.core.TestUtil)304 StreamCallback (io.siddhi.core.stream.output.StreamCallback)289 SiddhiApp (io.siddhi.query.api.SiddhiApp)84 Query (io.siddhi.query.api.execution.query.Query)82 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)81 ArrayList (java.util.ArrayList)72 Logger (org.apache.logging.log4j.core.Logger)56 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)33 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)30 InMemoryBroker (io.siddhi.core.util.transport.InMemoryBroker)28 UnitTestAppender (io.siddhi.core.UnitTestAppender)23 InMemoryPersistenceStore (io.siddhi.core.util.persistence.InMemoryPersistenceStore)15 PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)15 HashMap (java.util.HashMap)15