Search in sources :

Example 1 with SiddhiManager

use of io.siddhi.core.SiddhiManager in project siddhi by wso2.

the class UpdateOrInsertTestStoreTestCase method updateOrInsertTableTest10.

@Test
public void updateOrInsertTableTest10() throws InterruptedException, SQLException {
    log.info("updateOrInsertTableTest10");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, volume long, price float); " + "define stream UpdateStockStream (comp string, vol long); " + "@Store(type=\"testStoreContainingInMemoryTable\")\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream left outer join StockTable " + "   on UpdateStockStream.comp == StockTable.symbol " + "select comp as symbol, ifThenElse(price is nulL,5F,price) as price, vol as volume " + "update or insert into StockTable " + "   on StockTable.symbol==symbol;" + "" + "@info(name = 'query3') " + "from CheckStockStream[(symbol==StockTable.symbol and volume == StockTable.volume and " + "price < StockTable.price) in StockTable] " + "insert into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query3", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { "IBM", 200L, 0f }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { "WSO2", 300L, 4.6f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(2, inEventCount);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6F, 100L });
    checkStockStream.send(new Object[] { "IBM", 100L, 155.6f });
    checkStockStream.send(new Object[] { "WSO2", 100L, 155.6f });
    updateStockStream.send(new Object[] { "IBM", 200L });
    updateStockStream.send(new Object[] { "WSO2", 300L });
    checkStockStream.send(new Object[] { "IBM", 200L, 0f });
    checkStockStream.send(new Object[] { "WSO2", 300L, 4.6f });
    Thread.sleep(1000);
    AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, 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 2 with SiddhiManager

use of io.siddhi.core.SiddhiManager in project siddhi by wso2.

the class UpdateOrInsertTestStoreTestCase method updateOrInsertTableTest7.

@Test
public void updateOrInsertTableTest7() throws InterruptedException, SQLException {
    log.info("updateOrInsertTableTest7");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, volume long, price float); " + "define stream UpdateStockStream (comp string, vol long); " + "@Store(type=\"testStoreContainingInMemoryTable\")\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream " + "select comp as symbol,  5f as price, vol as volume " + "update or insert into StockTable " + "   on StockTable.symbol==symbol;" + "" + "@info(name = 'query3') " + "from CheckStockStream[(symbol==StockTable.symbol and volume==StockTable.volume and price " + "< StockTable.price) in StockTable] " + "insert into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query3", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { "IBM", 100L, 56.6f }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { "IBM", 200L, 0f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(2, inEventCount);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6F, 100L });
    stockStream.send(new Object[] { "IBM", 155.6F, 100L });
    checkStockStream.send(new Object[] { "IBM", 100L, 56.6f });
    checkStockStream.send(new Object[] { "WSO2", 100L, 155.6f });
    updateStockStream.send(new Object[] { "IBM", 200L });
    checkStockStream.send(new Object[] { "IBM", 200L, 0f });
    checkStockStream.send(new Object[] { "WSO2", 100L, 155.6f });
    Thread.sleep(2000);
    AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, 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 3 with SiddhiManager

use of io.siddhi.core.SiddhiManager in project siddhi by wso2.

the class UpdateOrInsertTestStoreTestCase method updateOrInsertTableTest1.

@Test
public void updateOrInsertTableTest1() throws InterruptedException, SQLException {
    log.info("updateOrInsertTableTest1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream UpdateStockStream (symbol string, price float, volume long); " + "@Store(type=\"testStoreContainingInMemoryTable\")\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream#window.timeBatch(1 sec) " + "update or insert into StockTable " + "   on StockTable.symbol=='IBM' and StockTable.symbol=='GOOG';";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    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 });
    updateStockStream.send(new Object[] { "GOOG", 10.6F, 100L });
    Thread.sleep(3000);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 4 with SiddhiManager

use of io.siddhi.core.SiddhiManager in project siddhi by wso2.

the class CronWindowTestCase method cronWindowTest2.

@Test
public void cronWindowTest2() throws InterruptedException {
    log.info("Testing cron window for expired events");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "@info(name = 'query1') " + "from cseEventStream#window.cron('*/5 * * * * ?') " + "select symbol,price,volume " + "insert expired events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                removeEventCount++;
            }
            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(7000);
    inputHandler.send(new Object[] { "IBM1", 700f, 0 });
    inputHandler.send(new Object[] { "WSO22", 60.5f, 1 });
    Thread.sleep(7000);
    inputHandler.send(new Object[] { "IBM43", 700f, 0 });
    inputHandler.send(new Object[] { "WSO4343", 60.5f, 1 });
    SiddhiTestHelper.waitForEvents(7000, 4, removeEventCount, 20000);
    AssertJUnit.assertEquals(4, 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) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 5 with SiddhiManager

use of io.siddhi.core.SiddhiManager in project siddhi by wso2.

the class CronWindowTestCase method cronWindowTest1.

@Test
public void cronWindowTest1() throws InterruptedException {
    log.info("Testing cron window for current events");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "@info(name = 'query1') " + "from cseEventStream#window.cron('*/5 * * * * ?') " + "select symbol,price,volume " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                if (event.isExpired()) {
                    removeEventCount++;
                } else {
                    inEventCount++;
                }
            }
            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(7000);
    inputHandler.send(new Object[] { "IBM1", 700f, 0 });
    inputHandler.send(new Object[] { "WSO22", 60.5f, 1 });
    Thread.sleep(7000);
    inputHandler.send(new Object[] { "IBM43", 700f, 0 });
    inputHandler.send(new Object[] { "WSO4343", 60.5f, 1 });
    Thread.sleep(7000);
    AssertJUnit.assertEquals(6, inEventCount);
    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) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiManager (io.siddhi.core.SiddhiManager)1871 Test (org.testng.annotations.Test)1855 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1756 InputHandler (io.siddhi.core.stream.input.InputHandler)1585 Event (io.siddhi.core.event.Event)1255 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)821 StreamCallback (io.siddhi.core.stream.output.StreamCallback)305 TestUtil (io.siddhi.core.TestUtil)304 SiddhiApp (io.siddhi.query.api.SiddhiApp)89 Query (io.siddhi.query.api.execution.query.Query)85 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)84 ArrayList (java.util.ArrayList)73 Logger (org.apache.logging.log4j.core.Logger)57 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)34 InMemoryBroker (io.siddhi.core.util.transport.InMemoryBroker)32 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)30 HashMap (java.util.HashMap)25 UnitTestAppender (io.siddhi.core.UnitTestAppender)23 InMemoryConfigManager (io.siddhi.core.util.config.InMemoryConfigManager)21 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)15