Search in sources :

Example 61 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class TimeBatchWindowTestCase method testTimeWindowBatch4.

@Test
public void testTimeWindowBatch4() throws InterruptedException {
    log.info("TimeWindowBatch Test4");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseEventWindow (symbol string, price float, volume int) timeBatch(1 sec) output " + "expired events; ";
    String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') " + "from cseEventWindow " + "select symbol, sum(price) as price " + "insert expired events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + 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;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            AssertJUnit.assertTrue("inEvents should not arrive ", inEvents == null);
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 1 });
    Thread.sleep(1100);
    inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
    inputHandler.send(new Object[] { "IBM", 700f, 3 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
    Thread.sleep(1100);
    inputHandler.send(new Object[] { "IBM", 700f, 5 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
    Thread.sleep(2000);
    AssertJUnit.assertEquals(0, inEventCount);
    AssertJUnit.assertEquals(3, removeEventCount);
    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 62 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class TimeBatchWindowTestCase method timeWindowBatchTest8.

@Test
public void timeWindowBatchTest8() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseEventWindow (symbol string, price float, volume int) timeBatch(2 sec , 0); ";
    String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') " + "from cseEventWindow " + "select symbol, sum(price) as sumPrice, volume " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEventCount == 0) {
                AssertJUnit.assertTrue("Remove Events will only arrive after the second time period. ", removeEvents == null);
            }
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            } else if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    // Start sending events in the beginning of a cycle
    while (System.currentTimeMillis() % 2000 != 0) {
    }
    inputHandler.send(new Object[] { "IBM", 700f, 0 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 1 });
    Thread.sleep(8500);
    inputHandler.send(new Object[] { "WSO2", 60.5f, 1 });
    inputHandler.send(new Object[] { "II", 60.5f, 1 });
    Thread.sleep(13000);
    inputHandler.send(new Object[] { "TT", 60.5f, 1 });
    inputHandler.send(new Object[] { "YY", 60.5f, 1 });
    Thread.sleep(5000);
    AssertJUnit.assertEquals(3, inEventCount);
    AssertJUnit.assertEquals(0, removeEventCount);
    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 63 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class TimeLengthWindowTestCase method testTimeLengthWindow6.

/*
        Time period > Window time
        Number of events > length
    */
@Test
public void testTimeLengthWindow6() throws InterruptedException {
    log.info("Testing timeLength window with no of events greater than window length and time period greater than" + " window time");
    SiddhiManager siddhiManager = new SiddhiManager();
    String sensorStream = "define stream sensorStream (id string, sensorValue int); " + "define window sensorWindow (id string, sensorValue int) timeLength(3 sec,6); ";
    String query = "@info(name = 'query0') from sensorStream " + "insert into sensorWindow; " + "@info(name = 'query1') from sensorWindow " + " select id,sum(sensorValue) as sum" + " insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(sensorStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                if (inEvents[0].getData(0).toString().equals("id6")) {
                    AssertJUnit.assertEquals("6", inEvents[0].getData(1).toString());
                }
                if (inEvents[0].getData(0).toString().equals("id7")) {
                    AssertJUnit.assertEquals("6", inEvents[0].getData(1).toString());
                }
                if (inEvents[0].getData(0).toString().equals("id8")) {
                    AssertJUnit.assertEquals("6", inEvents[0].getData(1).toString());
                }
                inEventCount++;
            }
            if (removeEvents != null) {
                if (removeEvents[0].getData(0).toString().equals("id1")) {
                    AssertJUnit.assertEquals("5", removeEvents[0].getData(1).toString());
                }
                if (removeEvents[0].getData(0).toString().equals("id2")) {
                    AssertJUnit.assertEquals("5", removeEvents[0].getData(1).toString());
                }
                if (removeEvents[0].getData(0).toString().equals("id3")) {
                    AssertJUnit.assertEquals("3", removeEvents[0].getData(1).toString());
                }
                removeEventCount++;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("sensorStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "id1", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id2", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id3", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id4", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id5", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id6", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id7", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id8", 1 });
    Thread.sleep(1000);
    AssertJUnit.assertEquals(8, inEventCount);
    AssertJUnit.assertEquals(2, removeEventCount);
    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 64 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class TimeLengthWindowTestCase method testTimeLengthWindow7.

/*
        Time period < Window time
        Number of events < length
    */
@Test
public void testTimeLengthWindow7() throws InterruptedException {
    log.info("Testing timeLength window with no of events less than window length and time period less than " + "window time");
    SiddhiManager siddhiManager = new SiddhiManager();
    String sensorStream = "define stream sensorStream (id string, sensorValue int); " + "define window sensorWindow (id string, sensorValue int) timeLength(5 sec,5); ";
    String query = "@info(name = 'query0') from sensorStream " + "insert into sensorWindow; " + "@info(name = 'query1') from sensorWindow " + " select id,sum(sensorValue) as sum" + " insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(sensorStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            AssertJUnit.assertEquals((long) count + 1, (inEvents[0].getData(1)));
            count++;
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("sensorStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "id1", 1 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "id2", 1 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "id3", 1 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "id4", 1 });
    Thread.sleep(1000);
    AssertJUnit.assertEquals(4, 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 65 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class TimeLengthWindowTestCase method testTimeLengthWindow4.

/*
           Time Period > Window time
           Number of Events > Window length
    */
@Test
public void testTimeLengthWindow4() throws InterruptedException {
    log.info("Testing timeLength window with no of events greater than window length and time period greater than" + " window time");
    SiddhiManager siddhiManager = new SiddhiManager();
    String sensorStream = "define stream sensorStream (id string, sensorValue float); " + "define window sensorWindow (id string, sensorValue float) timeLength(2 sec,4); ";
    String query = "@info(name = 'query0') from sensorStream " + "insert into sensorWindow; " + "@info(name = 'query1') from sensorWindow " + " select id,sensorValue" + " insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(sensorStream + 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;
            }
            if (removeEvents != null) {
                AssertJUnit.assertTrue("InEvents arrived before RemoveEvents", inEventCount > removeEventCount);
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("sensorStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "id1", 10d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id2", 20d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id3", 30d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id4", 40d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id5", 50d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id6", 60d });
    Thread.sleep(2100);
    AssertJUnit.assertEquals(6, inEventCount);
    AssertJUnit.assertEquals(6, removeEventCount);
    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

Test (org.testng.annotations.Test)161 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)130 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)130 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)118 Event (org.wso2.siddhi.core.event.Event)117 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)82 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)35 Query (org.wso2.siddhi.query.api.execution.query.Query)32 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)14 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)12 ArrayList (java.util.ArrayList)10 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)10 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)10 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)9 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)9 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)8 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)8 Window (org.wso2.siddhi.core.window.Window)7 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)6 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)6