Search in sources :

Example 66 with SiddhiAppRuntime

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

the class LogicalAbsentSequenceTestCase method testQueryAbsent35.

@Test(dependsOnMethods = { "testQueryAbsent34" })
public void testQueryAbsent35() throws InterruptedException {
    log.info("Test the query (not e1 for 1 sec or not e2 for 1 sec), e3 with e2 and e3");
    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 (not Stream1[price>10] for 1 sec or not Stream2[price>20] for 1 sec), e3=Stream3[price>30] " + "select e3.symbol as symbol " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    TestUtil.TestCallback callback = TestUtil.addQueryCallback(siddhiAppRuntime, "query1", new Object[] { "WSO2" });
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    InputHandler stream3 = siddhiAppRuntime.getInputHandler("Stream3");
    siddhiAppRuntime.start();
    Thread.sleep(500);
    stream2.send(new Object[] { "IBM", 25.0f, 100 });
    Thread.sleep(600);
    stream3.send(new Object[] { "WSO2", 35.0f, 100 });
    Thread.sleep(100);
    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(org.ballerinalang.siddhi.core.stream.input.InputHandler) TestUtil(org.ballerinalang.siddhi.core.TestUtil) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 67 with SiddhiAppRuntime

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

the class SortWindowTestCase method sortWindowTest2.

@Test
public void sortWindowTest2() throws InterruptedException {
    log.info("sortWindow test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String planName = "@app:name('sortWindow2') ";
    String cseEventStream = "" + "define stream cseEventStream (symbol string, price int, volume long);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.sort(2,volume, 'asc', price, 'desc') " + "select price, volume " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(planName + 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;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 50, 100L });
    inputHandler.send(new Object[] { "IBM", 20, 100L });
    inputHandler.send(new Object[] { "WSO2", 40, 50L });
    inputHandler.send(new Object[] { "WSO2", 100, 20L });
    inputHandler.send(new Object[] { "WSO2", 50, 50L });
    Thread.sleep(1000);
    AssertJUnit.assertEquals(5, inEventCount);
    AssertJUnit.assertEquals(3, 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 68 with SiddhiAppRuntime

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

the class SortWindowTestCase method sortWindowTest6.

@Test(expectedExceptions = SiddhiAppCreationException.class)
public void sortWindowTest6() throws InterruptedException {
    log.info("sortWindowTest6");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, time long, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.sort(2, volume, 'ecs') " + "select symbol,price,volume " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
}
Also used : SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 69 with SiddhiAppRuntime

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

the class SortWindowTestCase method sortWindowTest4.

@Test(expectedExceptions = SiddhiAppCreationException.class)
public void sortWindowTest4() throws InterruptedException {
    log.info("sortWindowTest4");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.sort(2.5) " + "select symbol,price,volume " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
}
Also used : SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 70 with SiddhiAppRuntime

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

the class TimeBatchWindowTestCase method timeWindowBatchTest1.

@Test
public void timeWindowBatchTest1() 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 sumPrice,volume " + "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 (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();
    inputHandler.send(new Object[] { "IBM", 700f, 0 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 1 });
    Thread.sleep(3000);
    AssertJUnit.assertEquals(1, 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