Search in sources :

Example 86 with SiddhiManager

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

the class QueryAPITestCaseForTestStore method test10.

@Test
public void test10() throws InterruptedException {
    log.info("Test10 table with cache");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (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 ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    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 });
    Thread.sleep(500);
    String storeQuery = "" + "from StockTable " + "on volume > 10 " + "select symbol, sum(volume) as totalVolume " + "group by symbol " + "having symbol == 'WSO2'";
    Event[] events = siddhiAppRuntime.query(storeQuery);
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    AssertJUnit.assertEquals(200L, events[0].getData()[1]);
    events = siddhiAppRuntime.query(storeQuery);
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    AssertJUnit.assertEquals(200L, events[0].getData()[1]);
}
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) Test(org.testng.annotations.Test)

Example 87 with SiddhiManager

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

the class UpdateTestStoreTestCase method updateFromTableTest6.

@Test
public void updateFromTableTest6() throws InterruptedException, SQLException {
    log.info("updateFromTableTest6");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, 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 CheckStockStream[(StockTable.symbol==symbol) in StockTable] " + "insert into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query2", 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++;
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 55.6f, 100L });
    checkStockStream.send(new Object[] { "IBM", 100L });
    checkStockStream.send(new Object[] { "WSO2", 100L });
    checkStockStream.send(new Object[] { "IBM", 100L });
    Thread.sleep(1000);
    AssertJUnit.assertEquals("Number of success events", 3, inEventCount);
    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 88 with SiddhiManager

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

the class DistinctCountAggregationTestCase method incrementalStreamProcessorTest1.

@Test
public void incrementalStreamProcessorTest1() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest1: testing distinctCount incremental aggregator");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
    String query = "define aggregation stockAggregation " + "from stockStream " + "select distinctCount(symbol) as distinctCnt " + "aggregate by timestamp every sec...year ;" + "define stream inputStream (symbol string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596535449000L " + "per \"days\" " + "select AGG_TIMESTAMP, s.distinctCnt " + "order by AGG_TIMESTAMP " + "insert all events into outputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                if (inEvents != null) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
                if (removeEvents != null) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    for (Event event : removeEvents) {
                        removeEventsList.add(event.getData());
                        removeEventCount.incrementAndGet();
                    }
                }
                eventArrived = true;
            }
        });
        InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
        InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
        siddhiAppRuntime.start();
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
        stockStreamInputHandler.send(new Object[] { "WSO22", 70f, null, 40L, 10, 1496289950000L });
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "WSO23", 60f, 44f, 200L, 56, 1496289952000L });
        stockStreamInputHandler.send(new Object[] { "WSO24", 100f, null, 200L, 16, 1496289952000L });
        // Thursday, June 1, 2017 4:05:54 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 26, 1496289954000L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 102f, null, 200L, 96, 1496289954000L });
        // Thursday, June 1, 2017 4:05:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 500f, null, 200L, 7, 1496289956000L });
        // Thursday, June 1, 2017 4:06:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496290016000L });
        // Thursday, June 1, 2017 4:07:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM2", 600f, null, 200L, 6, 1496290076000L });
        // Thursday, June 1, 2017 5:07:56 AM
        stockStreamInputHandler.send(new Object[] { "CISCO", 700f, null, 200L, 20, 1496293676000L });
        // Thursday, June 1, 2017 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 61f, 44f, 200L, 56, 1496297276000L });
        // Friday, June 2, 2017 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "CISCO", 801f, null, 100L, 10, 1496383676000L });
        // Saturday, June 3, 2017 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "CISCO", 901f, null, 100L, 15, 1496470076000L });
        // Monday, July 3, 2017 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 96, 1499062076000L });
        // Thursday, August 3, 2017 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 402f, null, 200L, 9, 1501740476000L });
        // Friday, August 3, 2018 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 63f, 44f, 200L, 6, 1533276476000L });
        // Saturday, August 3, 2019 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 260f, 44f, 200L, 16, 1564812476000L });
        // Monday, August 3, 2020 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "CISCO", 26f, 44f, 200L, 16, 1596434876000L });
        Thread.sleep(100);
        inputStreamInputHandler.send(new Object[] { "IBM" });
        Thread.sleep(100);
        List<Object[]> expected = Arrays.asList(new Object[] { 1496275200000L, 8 }, new Object[] { 1496361600000L, 1 }, new Object[] { 1496448000000L, 1 }, new Object[] { 1499040000000L, 1 }, new Object[] { 1501718400000L, 1 }, new Object[] { 1533254400000L, 1 }, new Object[] { 1564790400000L, 1 }, new Object[] { 1596412800000L, 1 });
        SiddhiTestHelper.waitForEvents(100, 8, inEventCount, 10000);
        AssertJUnit.assertTrue("Event arrived", eventArrived);
        AssertJUnit.assertEquals("Number of success events", 8, inEventCount.get());
        AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isEventsMatch(inEventsList, expected));
        AssertJUnit.assertEquals("Number of remove events", 8, removeEventCount.get());
        AssertJUnit.assertTrue("Remove events matched", SiddhiTestHelper.isEventsMatch(removeEventsList, expected));
    } 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 89 with SiddhiManager

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

the class PurgingTestCase method incrementalPurgingTest2.

@Test
public void incrementalPurgingTest2() throws InterruptedException {
    LOG.info("incrementalPurgingTest2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = " define stream stockStream (symbol string, price float, lastClosingPrice float," + " volume long ,quantity int);";
    String query = "  @purge(enable='true',interval='5 sec',@retentionPeriod(sec='120 sec',min='all',hours='all'" + "                ,days='all',months='all',years='all'))  " + "define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue  " + "group by symbol " + "aggregate every sec...years ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();
    stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6 });
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10 });
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56 });
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16 });
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26 });
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96 });
    Thread.sleep(1000);
    Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"" + Calendar.getInstance().get(Calendar.YEAR) + "-**-** **:**:**\" " + "per \"seconds\"");
    EventPrinter.print(events);
    AssertJUnit.assertNotNull("Initial queried events cannot be null", events);
    AssertJUnit.assertEquals("Number of events before purging", 6, events.length);
    Thread.sleep(120100);
    events = siddhiAppRuntime.query("from stockAggregation " + "within \"" + Calendar.getInstance().get(Calendar.YEAR) + "-**-** **:**:**\" " + "per \"seconds\"");
    EventPrinter.print(events);
    AssertJUnit.assertNull("No events expected after purging", events);
    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) Test(org.testng.annotations.Test)

Example 90 with SiddhiManager

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

the class PurgingTestCase method incrementalPurgingTestCase3.

@Test
public void incrementalPurgingTestCase3() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, " + "volume long , quantity int, timestamp long); ";
    String query = " @purge(enable='true',interval='10 sec',@retentionPeriod(sec='120 sec',min='all',hours='all'," + "days='all',months='all',years='all'))   " + "define aggregation stockAggregation " + "from stockStream " + "select symbol, sum(price) as totalPrice " + "group by symbol " + "aggregate by timestamp every sec...year ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    try {
        InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
        siddhiAppRuntime.start();
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
        Thread.sleep(1000);
        // Thursday, June 1, 2017 4:05:51 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 16, 1496289951011L });
        Thread.sleep(1000);
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496289952000L });
        Thread.sleep(1000);
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289950000L });
        Thread.sleep(1000);
        stockStreamInputHandler.send(new Object[] { "WSO2", 500f, null, 200L, 7, 1496289951011L });
        Thread.sleep(1000);
        // Thursday, June 1, 2017 4:05:53 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289953000L });
        Thread.sleep(1000);
        stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 96, 1496289953000L });
        Thread.sleep(2000);
        Event[] events = siddhiAppRuntime.query("from stockAggregation within 0L, 1543664151000L per " + "'seconds' select AGG_TIMESTAMP, symbol, totalPrice ");
        EventPrinter.print(events);
        AssertJUnit.assertEquals("Check time windows", 7, events.length);
        List<Object[]> eventsList = new ArrayList<>();
        for (Event event : events) {
            eventsList.add(event.getData());
        }
        List<Object[]> expected = Arrays.asList(new Object[] { 1496289950000L, "WSO2", 50.0 }, new Object[] { 1496289950000L, "IBM", 900.0 }, new Object[] { 1496289951000L, "IBM", 100.0 }, new Object[] { 1496289951000L, "WSO2", 500.0 }, new Object[] { 1496289952000L, "IBM", 400.0 }, new Object[] { 1496289953000L, "IBM", 100.0 }, new Object[] { 1496289953000L, "WSO2", 100.0 });
        AssertJUnit.assertTrue("Data Matched", SiddhiTestHelper.isUnsortedEventsMatch(eventsList, expected));
        Thread.sleep(120000);
        events = siddhiAppRuntime.query("from stockAggregation within 0L, 1543664151000L per " + "'seconds' select AGG_TIMESTAMP, symbol, totalPrice ");
        EventPrinter.print(events);
        Assert.assertNull(events);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) ArrayList(java.util.ArrayList) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) 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