Search in sources :

Example 41 with InputHandler

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

the class SequencePartitionTestCase method testSequencePartitionQuery10.

@Test
public void testSequencePartitionQuery10() throws InterruptedException {
    log.info("Pattern -testSequence10 - 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 partitionStart = "partition with (volume of Stream1 , volume of Stream2) begin ";
    String query = "" + "@info(name = 'query1') " + "from every e1=Stream2[price>20]+, e2=Stream1[price>e1[0].price] " + "select e1[0].price as price1, e1[1].price as price2, e2.price as price3 " + "insert into OutputStream ;";
    String partitionEnd = "end";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + partitionStart + query + partitionEnd);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                if (event.isExpired()) {
                    removeEventCount++;
                } else {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { 55.6f, null, 57.6f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount);
                    }
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 59.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 57.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 55.6f, 120 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 57.6f, 150 });
    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(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 42 with InputHandler

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

the class TablePartitionTestCase method testPartitionQuery2.

@Test
public void testPartitionQuery2() throws InterruptedException {
    log.info("Table Partition test 2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('PartitionTest') " + "define stream streamA (symbol string, price int);" + "define stream streamB (symbol string);" + "define table tableA (symbol string, price int);" + "partition with (symbol of streamA, symbol of streamB) " + "begin " + "   @info(name = 'query1') " + "   from streamA " + "   select symbol, price " + "   insert into tableA;  " + "" + "end ;" + "" + "@info(name = 'query2') " + "from streamB[(symbol==tableA.symbol) in tableA] " + "select symbol " + "insert into outputStream;  " + "";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
            }
            eventArrived = true;
        }
    });
    InputHandler streamAInputHandler = siddhiAppRuntime.getInputHandler("streamA");
    InputHandler streamBInputHandler = siddhiAppRuntime.getInputHandler("streamB");
    siddhiAppRuntime.start();
    streamAInputHandler.send(new Object[] { "IBM", 700 });
    streamAInputHandler.send(new Object[] { "WSO2", 60 });
    streamAInputHandler.send(new Object[] { "WSO2", 60 });
    streamBInputHandler.send(new Object[] { "WSO2" });
    streamBInputHandler.send(new Object[] { "FB" });
    streamBInputHandler.send(new Object[] { "IBM" });
    SiddhiTestHelper.waitForEvents(100, 2, count, 60000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(2, count.get());
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 43 with InputHandler

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

the class InstanceOfFunctionTestCase method testInstanceOfIntegerFunctionExtensionTestCase.

@Test
public void testInstanceOfIntegerFunctionExtensionTestCase() throws InterruptedException {
    log.info("testInstanceOfIntegerFunctionExtension TestCase");
    SiddhiManager siddhiManager = new SiddhiManager();
    String sensorEventStream = "define stream sensorEventStream (timestamp long, " + "isPowerSaverEnabled bool, sensorId int , sensorName string, longitude double, latitude double, " + "humidity float, sensorValue double);";
    String query = ("@info(name = 'query1') " + "from sensorEventStream " + "select sensorName ,instanceOfInteger(sensorId) as valid, sensorId " + "insert into outputStream;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(sensorEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            for (Event inEvent : inEvents) {
                count++;
                if (count == 1) {
                    AssertJUnit.assertEquals(true, inEvent.getData(1));
                }
                if (count == 2) {
                    AssertJUnit.assertEquals(false, inEvent.getData(1));
                }
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("sensorEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 19900813115534L, false, 601, "temperature", 90.34344, 20.44345, 2.3f, 20.44345 });
    inputHandler.send(new Object[] { 19900813115534L, true, 60232434.657, "temperature", 90.34344, 20.44345, 2.3f, 20.44345 });
    Thread.sleep(100);
    org.testng.AssertJUnit.assertEquals(2, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 44 with InputHandler

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

the class InstanceOfFunctionTestCase method testInstanceOfLongFunctionExtensionTestCase.

@Test
public void testInstanceOfLongFunctionExtensionTestCase() throws InterruptedException {
    log.info("testInstanceOfLongFunctionExtension TestCase");
    SiddhiManager siddhiManager = new SiddhiManager();
    String sensorEventStream = "define stream sensorEventStream (timestamp long, " + "isPowerSaverEnabled bool, sensorId int , sensorName string, longitude double, latitude double, " + "humidity float, sensorValue double);";
    String query = ("@info(name = 'query1') " + "from sensorEventStream " + "select sensorName ,instanceOfLong(timestamp) as valid, timestamp " + "insert into outputStream;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(sensorEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            for (Event inEvent : inEvents) {
                count++;
                if (count == 1) {
                    AssertJUnit.assertEquals(true, inEvent.getData(1));
                }
                if (count == 2) {
                    AssertJUnit.assertEquals(false, inEvent.getData(1));
                }
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("sensorEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 19900813115534L, false, 601, "temperature", 90.34344, 20.44345, 2.3f, 20.44345 });
    inputHandler.send(new Object[] { 1990, false, 602, "temperature", 90.34344, 20.44345, 2.3f, 20.44345 });
    Thread.sleep(100);
    org.testng.AssertJUnit.assertEquals(2, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 45 with InputHandler

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

the class InstanceOfFunctionTestCase method testInstanceOfFloatFunctionExtensionTestCase.

@Test
public void testInstanceOfFloatFunctionExtensionTestCase() throws InterruptedException {
    log.info("testInstanceOfFloatFunctionExtension TestCase");
    SiddhiManager siddhiManager = new SiddhiManager();
    String sensorEventStream = "define stream sensorEventStream (timestamp long, " + "isPowerSaverEnabled bool, sensorId int , sensorName string, longitude double, latitude double, " + "humidity float, sensorValue double);";
    String query = ("@info(name = 'query1') " + "from sensorEventStream " + "select sensorName ,instanceOfFloat(humidity) as valid, longitude " + "insert into outputStream;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(sensorEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            for (Event inEvent : inEvents) {
                count++;
                if (count == 1) {
                    AssertJUnit.assertEquals(true, inEvent.getData(1));
                }
                if (count == 2) {
                    AssertJUnit.assertEquals(false, inEvent.getData(1));
                }
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("sensorEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 19900813115534L, false, 601, "temperature", 90.34344, 20.44345, 2.3f, 20.44345 });
    inputHandler.send(new Object[] { 19900813115534L, true, 602, "temperature", 90.34344, 20.44345, 2.3, 20.44345 });
    Thread.sleep(100);
    org.testng.AssertJUnit.assertEquals(2, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)1155 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)1151 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)1149 Test (org.testng.annotations.Test)1136 Event (org.wso2.siddhi.core.event.Event)800 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)587 TestUtil (org.wso2.siddhi.core.TestUtil)300 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)201 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)80 Query (org.wso2.siddhi.query.api.execution.query.Query)79 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)77 ArrayList (java.util.ArrayList)25 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)13 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)13 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)13 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)13 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)13 InMemoryBroker (org.wso2.siddhi.core.util.transport.InMemoryBroker)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6