Search in sources :

Example 61 with InputHandler

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

the class PlaybackTestCase method playbackTest12.

@Test(dependsOnMethods = { "playbackTest11" })
public void playbackTest12() throws InterruptedException {
    log.info("Playback Test 12: Testing playback with out of order event with greater than system timestamp");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "@app:playback(idle.time = '100 millisecond', increment = '1 sec') " + "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.timeBatch(2 sec) " + "select symbol,price,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 (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                AssertJUnit.assertTrue("InEvents arrived before RemoveEvents", inEventCount > removeEventCount);
                removeEventCount = removeEventCount + removeEvents.length;
                if (removeEventCount == 3) {
                    // Last timestamp is 1900 + 3 sec (increment) = 2200
                    AssertJUnit.assertEquals(4900, removeEvents[0].getTimestamp());
                }
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(100, new Object[] { "IBM", 700f, 0 });
    inputHandler.send(200, new Object[] { "WSO2", 600.5f, 1 });
    Thread.sleep(150);
    inputHandler.send(1900, new Object[] { "ORACLE", 500.0f, 2 });
    // Anything more than 100 is enough. Used 200 to be on safe side
    Thread.sleep(350);
    SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 60000);
    SiddhiTestHelper.waitForEvents(100, 3, removeEventCount, 60000);
    AssertJUnit.assertEquals(3, inEventCount);
    AssertJUnit.assertEquals(3, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 62 with InputHandler

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

the class PlaybackTestCase method playbackTest17_1.

@Test(dependsOnMethods = { "playbackTest17" })
public void playbackTest17_1() throws InterruptedException {
    log.info("Playback Test 17: Switching between Playback mode and live mode with heartbeat disabled in query " + "containing regular time batch window test only current batch ");
    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 * " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    try {
        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;
                }
                if (removeEvents != null) {
                    removeEventCount = removeEventCount + removeEvents.length;
                }
                eventArrived = true;
            }
        });
        InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        siddhiAppRuntime.start();
        siddhiAppRuntime.enablePlayBack(true, null, null);
        long timestamp = System.currentTimeMillis();
        inputHandler.send(timestamp - 500, new Object[] { "IBM", 700f, 0 });
        Thread.sleep(10);
        inputHandler.send(timestamp - 100, new Object[] { "WSO2", 60.5f, 1 });
        siddhiAppRuntime.enablePlayBack(false, null, null);
        // 1 sec passed
        Thread.sleep(605);
        SiddhiTestHelper.waitForEvents(100, 2, inEventCount, 60000);
        SiddhiTestHelper.waitForEvents(100, 0, removeEventCount, 60000);
        AssertJUnit.assertEquals(2, inEventCount);
        AssertJUnit.assertEquals(0, removeEventCount);
        AssertJUnit.assertTrue(eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 63 with InputHandler

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

the class FilterTestCase1 method testFilterQuery65.

@Test
public void testFilterQuery65() throws InterruptedException {
    log.info("Filter test65");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.LONG);
    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"), Compare.Operator.EQUAL, Expression.value(40))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    query.insertInto("outputStream");
    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    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(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 50f, 60L });
    inputHandler.send(new Object[] { "WSO2", 70f, 40L });
    inputHandler.send(new Object[] { "WSO2", 44f, 200L });
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiApp(io.siddhi.query.api.SiddhiApp) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) Query(io.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 64 with InputHandler

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

the class FilterTestCase1 method testFilterQuery3.

@Test
public void testFilterQuery3() throws InterruptedException {
    log.info("Filter test3");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "@info(name = 'query1') from cseEventStream[70 > price] select symbol,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);
            AssertJUnit.assertTrue("WSO2".equals(inEvents[0].getData(0)));
            count.addAndGet(inEvents.length);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 55.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler.send(new Object[] { "WSO2", 57.6f, 200 });
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 65 with InputHandler

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

the class SandboxTestCase method sandboxTest2.

@Test
public void sandboxTest2() throws InterruptedException {
    log.info("sandbox test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String app = "" + "@source(type='foo')" + "@source(type='foo1')" + "@sink(type='foo1')" + "@source(type='inMemory', topic='myTopic')" + "define stream StockStream (symbol string, price float, vol long);\n" + "" + "@sink(type='foo1')" + "@sink(type='inMemory', topic='myTopic1')" + "define stream DeleteStockStream (symbol string, price float, vol long);\n" + "" + "@store(type='rdbms')" + "define table StockTable (symbol string, price float, volume long);\n" + "" + "define stream CountStockStream (symbol string);\n" + "" + "@info(name = 'query1') " + "from StockStream " + "select symbol, price, vol as volume " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from DeleteStockStream[vol>=100] " + "delete StockTable " + "   on StockTable.symbol==symbol ;" + "" + "@info(name = 'query3') " + "from CountStockStream#window.length(0) join StockTable" + " on CountStockStream.symbol==StockTable.symbol " + "select CountStockStream.symbol as symbol " + "insert into CountResultsStream ;";
    SiddhiApp siddhiApp = SiddhiCompiler.parse(app);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSandboxSiddhiAppRuntime(siddhiApp);
    Assert.assertEquals(siddhiAppRuntime.getSources().size(), 1);
    Assert.assertEquals(siddhiAppRuntime.getSinks().size(), 1);
    Assert.assertEquals(siddhiAppRuntime.getTables().size(), 1);
    for (List<Source> sources : siddhiAppRuntime.getSources()) {
        for (Source source : sources) {
            Assert.assertTrue(source.getType().equalsIgnoreCase("inMemory"));
        }
    }
    for (List<Sink> sinks : siddhiAppRuntime.getSinks()) {
        for (Sink sink : sinks) {
            Assert.assertTrue(sink.getType().equalsIgnoreCase("inMemory"));
        }
    }
    for (Table table : siddhiAppRuntime.getTables()) {
        Assert.assertTrue(table instanceof InMemoryTable);
    }
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler deleteStockStream = siddhiAppRuntime.getInputHandler("DeleteStockStream");
    InputHandler countStockStream = siddhiAppRuntime.getInputHandler("CountStockStream");
    siddhiAppRuntime.addCallback("CountResultsStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
        }
    });
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 75.6f, 100L });
    stockStream.send(new Object[] { "WSO2", 57.6f, 100L });
    deleteStockStream.send(new Object[] { "IBM", 57.6f, 100L });
    countStockStream.send(new Object[] { "WSO2" });
    Thread.sleep(500);
    Assert.assertEquals(count.get(), 2);
    siddhiAppRuntime.shutdown();
}
Also used : InMemoryTable(io.siddhi.core.table.InMemoryTable) InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiApp(io.siddhi.query.api.SiddhiApp) Table(io.siddhi.core.table.Table) InMemoryTable(io.siddhi.core.table.InMemoryTable) Source(io.siddhi.core.stream.input.source.Source) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Sink(io.siddhi.core.stream.output.sink.Sink) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

InputHandler (io.siddhi.core.stream.input.InputHandler)1591 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1590 SiddhiManager (io.siddhi.core.SiddhiManager)1585 Test (org.testng.annotations.Test)1575 Event (io.siddhi.core.event.Event)1216 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)817 TestUtil (io.siddhi.core.TestUtil)304 StreamCallback (io.siddhi.core.stream.output.StreamCallback)289 SiddhiApp (io.siddhi.query.api.SiddhiApp)84 Query (io.siddhi.query.api.execution.query.Query)82 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)81 ArrayList (java.util.ArrayList)72 Logger (org.apache.logging.log4j.core.Logger)56 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)33 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)30 InMemoryBroker (io.siddhi.core.util.transport.InMemoryBroker)28 UnitTestAppender (io.siddhi.core.UnitTestAppender)23 InMemoryPersistenceStore (io.siddhi.core.util.persistence.InMemoryPersistenceStore)15 PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)15 HashMap (java.util.HashMap)15