Search in sources :

Example 51 with QueryCallback

use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class OrderByLimitTestCase method limitTest5.

@Test
public void limitTest5() 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.lengthBatch(4) " + "select symbol, sum(volume) as totalVolume, volume, price " + "group by symbol " + "order by price, totalVolume " + "limit 2 " + "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);
            Assert.assertEquals(2, inEvents.length);
            Assert.assertTrue(inEvents[0].getData(2).equals(0) || inEvents[0].getData(2).equals(4));
            inEventCount = inEventCount + inEvents.length;
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 60.5f, 0 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 1 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
    inputHandler.send(new Object[] { "XYZ", 60.5f, 3 });
    inputHandler.send(new Object[] { "IBM", 60.5f, 4 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 5 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 7 });
    Thread.sleep(500);
    AssertJUnit.assertEquals(4, inEventCount);
    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 52 with QueryCallback

use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class OrderByLimitTestCase method limitTest1.

@Test
public void limitTest1() 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.lengthBatch(4) " + "select symbol, price, volume " + "limit 2 " + "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);
            Assert.assertEquals(2, inEvents.length);
            Assert.assertTrue(inEvents[0].getData(2).equals(0) || inEvents[0].getData(2).equals(4));
            inEventCount = inEventCount + inEvents.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 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 3 });
    inputHandler.send(new Object[] { "IBM", 700f, 4 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 5 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 7 });
    Thread.sleep(500);
    AssertJUnit.assertEquals(4, inEventCount);
    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 53 with QueryCallback

use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class PassThroughTestCase method passThroughTest4.

@Test
public void passThroughTest4() throws InterruptedException {
    log.info("pass through test4");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('passThroughTest4') " + "" + "define stream cseEventStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from cseEventStream " + "insert into outputStream;" + "" + "@info(name = 'query2') " + "from outputStream " + "select * " + "insert into outputStream2 ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    log.info("Running : " + siddhiAppRuntime.getName());
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            AssertJUnit.assertTrue("WSO2".equals(inEvents[0].getData(0)));
            count = count + inEvents.length;
            eventArrived = true;
        }
    };
    siddhiAppRuntime.addCallback("query2", queryCallback);
    queryCallback.startProcessing();
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 700f, 100L });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 200L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(2, count);
    AssertJUnit.assertTrue(eventArrived);
    queryCallback.stopProcessing();
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 54 with QueryCallback

use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class PassThroughTestCase method passThroughTest2.

@Test
public void passThroughTest2() throws InterruptedException {
    log.info("pass through test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT);
    StreamDefinition cseEventStream1 = StreamDefinition.id("cseEventStream1").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT);
    Query query = new Query();
    query.from(InputStream.stream("cseEventStream"));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    query.insertInto("StockQuote");
    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.defineStream(cseEventStream1);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(inEvents);
            count += inEvents.length;
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream1");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 100 });
    inputHandler.send(new Object[] { "WSO2", 100 });
    Thread.sleep(100);
    AssertJUnit.assertEquals(0, count);
    AssertJUnit.assertFalse(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiApp(org.wso2.siddhi.query.api.SiddhiApp) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) Query(org.wso2.siddhi.query.api.execution.query.Query) 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 55 with QueryCallback

use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class PersistenceTestCase method persistenceTest11.

@Test(dependsOnMethods = "persistenceTest10")
public void persistenceTest11() throws InterruptedException {
    log.info("persistence test 11 - batch window query");
    PersistenceStore persistenceStore = new InMemoryPersistenceStore();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);
    String siddhiApp = "" + "@app:name('Test') " + "" + "define stream StockStream ( symbol string, price float, volume long );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.lengthBatch(2) " + "select *" + "insert all events into OutStream ";
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            eventArrived = true;
            atomicCount.incrementAndGet();
            for (Event inEvent : inEvents) {
                AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
            }
            if (removeEvents != null) {
                for (Event removeEvent : removeEvents) {
                    lastValue = (Long) removeEvent.getData(2);
                }
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100L });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 101L });
    inputHandler.send(new Object[] { "IBM", 75.6f, 102L });
    inputHandler.send(new Object[] { "IBM", 75.6f, 103L });
    SiddhiTestHelper.waitForEvents(100, 2, atomicCount, 10000);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(new Long(101), lastValue);
    // persisting
    siddhiAppRuntime.persist();
    Thread.sleep(500);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 50L });
    inputHandler.send(new Object[] { "IBM", 75.6f, 50L });
    inputHandler.send(new Object[] { "IBM", 75.6f, 50L });
    // restarting execution plan
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    inputHandler.send(new Object[] { "IBM", 75.6f, 100L });
    // shutdown siddhi app
    Thread.sleep(500);
    SiddhiTestHelper.waitForEvents(100, 3, atomicCount, 10000);
    AssertJUnit.assertEquals(new Long(103), lastValue);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) PersistenceStore(org.wso2.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore) InMemoryPersistenceStore(org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore) Event(org.wso2.siddhi.core.event.Event) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) CannotRestoreSiddhiAppStateException(org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiManager (org.wso2.siddhi.core.SiddhiManager)590 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)590 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)588 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)585 Event (org.wso2.siddhi.core.event.Event)584 Test (org.testng.annotations.Test)582 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)73 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)73 Query (org.wso2.siddhi.query.api.execution.query.Query)73 ArrayList (java.util.ArrayList)25 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)12 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)12 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)11 Test (org.junit.Test)5 ExecutionPlanRuntime (org.wso2.siddhi.core.ExecutionPlanRuntime)5 StringConcatAggregatorString (org.wso2.siddhi.core.query.extension.util.StringConcatAggregatorString)5 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1