Search in sources :

Example 71 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime in project ballerina by ballerina-lang.

the class TimeBatchWindowTestCase method timeWindowBatchTest3.

@Test
public void timeWindowBatchTest3() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.timeBatch(1 sec) " + "select symbol, sum(price) as price " + "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 (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            AssertJUnit.assertTrue("Remove events should not arrive ", removeEvents == 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(3, inEventCount);
    AssertJUnit.assertEquals(0, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 72 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime in project ballerina by ballerina-lang.

the class TimeBatchWindowTestCase method timeWindowBatchTest7.

@Test
public void timeWindowBatchTest7() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.timeBatch(2 sec , 0) " + "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.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 73 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime in project ballerina by ballerina-lang.

the class TimeBatchWindowTestCase method timeWindowBatchTest6.

@Test
public void timeWindowBatchTest6() throws InterruptedException {
    log.info("timeWindowBatch Test6");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream twitterStream (user string, tweet string, company string); ";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.timeBatch(1 sec) join twitterStream#window.timeBatch(1 sec) " + "on cseEventStream.symbol== twitterStream.company " + "select cseEventStream.symbol as symbol, twitterStream.tweet, cseEventStream.price " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    inEventCount += (inEvents.length);
                }
                if (removeEvents != null) {
                    removeEventCount += (removeEvents.length);
                }
                eventArrived = true;
            }
        });
        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
        siddhiAppRuntime.start();
        cseEventStreamHandler.send(new Object[] { "WSO2", 55.6f, 100 });
        twitterStreamHandler.send(new Object[] { "User1", "Hello World", "WSO2" });
        cseEventStreamHandler.send(new Object[] { "IBM", 75.6f, 100 });
        Thread.sleep(1500);
        cseEventStreamHandler.send(new Object[] { "WSO2", 57.6f, 100 });
        Thread.sleep(1000);
        AssertJUnit.assertTrue("In Events can be 1 or 2 ", inEventCount == 1 || inEventCount == 2);
        AssertJUnit.assertEquals(0, removeEventCount);
        AssertJUnit.assertTrue(eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 74 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime in project ballerina by ballerina-lang.

the class TimeBatchWindowTestCase method timeWindowBatchTest5.

@Test
public void timeWindowBatchTest5() throws InterruptedException {
    log.info("timeWindowBatch Test5");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream twitterStream (user string, tweet string, company string); ";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.timeBatch(1 sec) join twitterStream#window.timeBatch(1 sec) " + "on cseEventStream.symbol== twitterStream.company " + "select cseEventStream.symbol as symbol, twitterStream.tweet, cseEventStream.price " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    inEventCount += (inEvents.length);
                }
                if (removeEvents != null) {
                    removeEventCount += (removeEvents.length);
                }
                eventArrived = true;
            }
        });
        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
        siddhiAppRuntime.start();
        cseEventStreamHandler.send(new Object[] { "WSO2", 55.6f, 100 });
        twitterStreamHandler.send(new Object[] { "User1", "Hello World", "WSO2" });
        cseEventStreamHandler.send(new Object[] { "IBM", 75.6f, 100 });
        Thread.sleep(1100);
        cseEventStreamHandler.send(new Object[] { "WSO2", 57.6f, 100 });
        Thread.sleep(1000);
        AssertJUnit.assertTrue("In Events can be 1 or 2 ", inEventCount == 1 || inEventCount == 2);
        AssertJUnit.assertTrue("Removed Events can be 1 or 2 ", removeEventCount == 1 || removeEventCount == 2);
        AssertJUnit.assertTrue(eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 75 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime in project ballerina by ballerina-lang.

the class TimeBatchWindowTestCase method timeWindowBatchTest2.

@Test
public void timeWindowBatchTest2() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.timeBatch(1 sec) " + "select symbol, sum(price) as price " + "insert all 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) {
                AssertJUnit.assertTrue("InEvents arrived before RemoveEvents", inEventCount > removeEventCount);
                removeEventCount = removeEventCount + removeEvents.length;
            }
            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(3, inEventCount);
    AssertJUnit.assertEquals(1, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)1235 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)1231 Test (org.testng.annotations.Test)1231 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)1123 Event (org.ballerinalang.siddhi.core.event.Event)798 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)574 TestUtil (org.ballerinalang.siddhi.core.TestUtil)300 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)201 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)85 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)82 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)80 ArrayList (java.util.ArrayList)25 InMemoryBroker (org.ballerinalang.siddhi.core.util.transport.InMemoryBroker)16 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)12 CannotRestoreSiddhiAppStateException (org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException)12 InMemoryPersistenceStore (org.ballerinalang.siddhi.core.util.persistence.InMemoryPersistenceStore)12 PersistenceStore (org.ballerinalang.siddhi.core.util.persistence.PersistenceStore)12 HashMap (java.util.HashMap)11 InMemoryConfigManager (org.ballerinalang.siddhi.core.util.config.InMemoryConfigManager)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6