Search in sources :

Example 21 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class QuerySyncTestCase method querySyncTest4.

@Test
public void querySyncTest4() throws InterruptedException {
    log.info("querySync test4");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "@synchronized('true') " + "from every ( e1=Stream1[price>20] -> e3=Stream1[price>20]) -> e2=Stream2[price>e1.price] " + "select e1.price as price1, e3.price as price3, e2.price as price2 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query1", 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.incrementAndGet();
                    switch(inEventCount.get()) {
                        case 1:
                            org.testng.AssertJUnit.assertArrayEquals(new Object[] { 55.6f, 54f, 57.7f }, event.getData());
                            break;
                        case 2:
                            org.testng.AssertJUnit.assertArrayEquals(new Object[] { 53.6f, 53f, 57.7f }, event.getData());
                            break;
                        default:
                            org.testng.AssertJUnit.assertSame(2, inEventCount);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount.addAndGet(removeEvents.length);
            }
            eventArrived = true;
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 54f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 53.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 53f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 57.7f, 100 });
    Thread.sleep(100);
    org.testng.AssertJUnit.assertEquals("Number of success events", 2, inEventCount.get());
    org.testng.AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
    org.testng.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 22 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class YAMLConfigManagerTestCase method yamlConfigManagerTest2.

@Test
public void yamlConfigManagerTest2() throws InterruptedException {
    log.info("YAMLConfigManagerTest2 - References test");
    String baseDir = Paths.get(".").toString();
    Path path = Paths.get(baseDir, "src", "test", "resources", "systemProperties.yaml");
    String fileContent = FileReader.readYAMLConfigFile(path);
    YAMLConfigManager yamlConfigManager = new YAMLConfigManager(fileContent);
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setConfigManager(yamlConfigManager);
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "@Store(ref='ref1')\n" + "define table StockTable (symbol string, volume long); ";
    String query1 = "" + "@info(name = 'query1') " + "from StockStream\n" + "select symbol, volume\n" + "insert into StockTable ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query1);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 75.6f, 100L });
    Thread.sleep(1000);
    Event[] events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(2, events.length);
    siddhiAppRuntime.shutdown();
}
Also used : Path(java.nio.file.Path) InputHandler(io.siddhi.core.stream.input.InputHandler) YAMLConfigManager(io.siddhi.core.util.config.YAMLConfigManager) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) StringConcatAggregatorExecutorString(io.siddhi.core.query.extension.util.StringConcatAggregatorExecutorString) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 23 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class IncrementalPersistenceTestCase method incrementalPersistenceTest2.

@Test
public void incrementalPersistenceTest2() throws InterruptedException {
    log.info("Incremental persistence test 2 - length batch window query");
    final int inputEventCount = 10;
    final int eventWindowSize = 4;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String siddhiApp = "" + "@app:name('incrementalPersistenceTest2') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.lengthBatch(" + eventWindowSize + ") " + "select symbol, price, sum(volume) as totalVol " + "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;
            for (Event inEvent : inEvents) {
                count++;
                AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
                lastValue = (Long) inEvent.getData(2);
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    for (int i = 0; i < inputEventCount; i++) {
        inputHandler.send(new Object[] { "IBM", 75.6f + i, 100 });
    }
    Thread.sleep(100);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(new Long(400), lastValue);
    // persisting
    siddhiAppRuntime.persist();
    Thread.sleep(5000);
    inputHandler.send(new Object[] { "WSO2", 100.4f, 150 });
    // Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 200.4f, 110 });
    inputHandler.send(new Object[] { "IBM", 300.4f, 100 });
    // Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 400.4f, 300 });
    inputHandler.send(new Object[] { "IBM", 500.6f, 120 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { "WSO2", 600.6f, 400 });
    Thread.sleep(100);
    siddhiAppRuntime.persist();
    Thread.sleep(5000);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed", e);
    }
    siddhiAppRuntime.start();
    Thread.sleep(500);
    inputHandler.send(new Object[] { "IBM", 700.6f, 230 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { "WSO2", 800.6f, 125 });
    inputHandler.send(new Object[] { "IBM", 900.6f, 370 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { "WSO2", 1000.6f, 140 });
    // shutdown Siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(count <= (inputEventCount + 6));
    AssertJUnit.assertEquals(new Long(865), lastValue);
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) IncrementalFileSystemPersistenceStore(io.siddhi.core.util.persistence.IncrementalFileSystemPersistenceStore) AtomicLong(java.util.concurrent.atomic.AtomicLong) Event(io.siddhi.core.event.Event) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 24 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class IncrementalPersistenceTestCase method incrementalPersistenceTest7.

@Test
public void incrementalPersistenceTest7() throws InterruptedException {
    log.info("Incremental persistence test 7 - in-memory table persistance test with primary key.");
    int inputEventCountPerCategory = 10;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String streams = "" + "@app:name('incrementalPersistenceTest7') " + "" + "define stream StockStream (symbol2 string, price float, volume long); " + "define stream CheckStockStream (symbol1 string); " + "define stream OutStream (symbol1 string, TB long); " + "@PrimaryKey('symbol2') " + "define table StockTable (symbol2 string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from CheckStockStream join StockTable " + " on symbol1 == symbol2 " + "select symbol2 as symbol1, volume as TB " + "group by symbol2 " + "insert all events into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventsList.add(event.getData());
                    inEventCount.incrementAndGet();
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    };
    try {
        siddhiAppRuntime.addCallback("query2", queryCallback);
        InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
        siddhiAppRuntime.start();
        for (int i = 0; i < inputEventCountPerCategory; i++) {
            stockStream.send(new Object[] { "WSO2-" + i, 55.6f, 180L + i });
        }
        for (int i = 0; i < inputEventCountPerCategory; i++) {
            stockStream.send(new Object[] { "IBM-" + i, 55.6f, 100L + i });
        }
        // persisting
        siddhiAppRuntime.persist();
        Thread.sleep(5000);
        siddhiAppRuntime.shutdown();
        siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
        siddhiAppRuntime.addCallback("query2", queryCallback);
        stockStream = siddhiAppRuntime.getInputHandler("StockStream");
        InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
        // loading
        try {
            siddhiAppRuntime.restoreLastRevision();
        } catch (CannotRestoreSiddhiAppStateException e) {
            Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed.", e);
        }
        siddhiAppRuntime.start();
        Thread.sleep(2000);
        stockStream.send(new Object[] { "WSO2-" + (inputEventCountPerCategory + 1), 100.6f, 180L });
        stockStream.send(new Object[] { "IBM-" + (inputEventCountPerCategory + 1), 100.6f, 100L });
        stockStream.send(new Object[] { "WSO2-" + (inputEventCountPerCategory + 2), 8.6f, 13L });
        stockStream.send(new Object[] { "IBM-" + (inputEventCountPerCategory + 2), 7.6f, 14L });
        checkStockStream.send(new Object[] { "IBM-5" });
        checkStockStream.send(new Object[] { "WSO2-5" });
        List<Object[]> expected = Arrays.asList(new Object[] { "IBM-5", 105L }, new Object[] { "WSO2-5", 185L });
        Thread.sleep(1000);
        AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isEventsMatch(inEventsList, expected));
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) IncrementalFileSystemPersistenceStore(io.siddhi.core.util.persistence.IncrementalFileSystemPersistenceStore) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 25 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class IncrementalPersistenceTestCase method incrementalPersistenceTest3.

@Test
public void incrementalPersistenceTest3() throws InterruptedException {
    log.info("Incremental persistence test 3 - time window query");
    final int inputEventCount = 10;
    final int eventWindowSizeInSeconds = 2;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String siddhiApp = "" + "@app:name('incrementalPersistenceTest3') " + "@app:playback " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.time(" + eventWindowSizeInSeconds + " sec) " + "select symbol, price, sum(volume) as totalVol " + "insert into OutStream";
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
            for (Event inEvent : inEvents) {
                count++;
                AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
                lastValue = (Long) inEvent.getData(2);
                log.info("last value: " + lastValue);
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    for (int i = 0; i < inputEventCount; i++) {
        inputHandler.send(i, new Object[] { "IBM", 75.6f + i, 100 });
    }
    Thread.sleep(4000);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(new Long(1000), lastValue);
    // persisting
    siddhiAppRuntime.persist();
    Thread.sleep(5000);
    inputHandler.send(3000, new Object[] { "WSO2", 100.4f, 150 });
    // Thread.sleep(100);
    inputHandler.send(3010, new Object[] { "WSO2", 200.4f, 110 });
    inputHandler.send(3020, new Object[] { "IBM", 300.4f, 100 });
    // Thread.sleep(100);
    inputHandler.send(3030, new Object[] { "WSO2", 400.4f, 300 });
    inputHandler.send(3040, new Object[] { "IBM", 500.6f, 120 });
    Thread.sleep(10);
    inputHandler.send(3050, new Object[] { "WSO2", 600.6f, 400 });
    Thread.sleep(100);
    siddhiAppRuntime.persist();
    Thread.sleep(5000);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    try {
        // loading
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    siddhiAppRuntime.start();
    Thread.sleep(500);
    inputHandler.send(3500, new Object[] { "IBM", 700.6f, 230 });
    Thread.sleep(10);
    inputHandler.send(3540, new Object[] { "WSO2", 800.6f, 125 });
    inputHandler.send(3590, new Object[] { "IBM", 900.6f, 370 });
    Thread.sleep(10);
    inputHandler.send(3600, new Object[] { "WSO2", 1000.6f, 140 });
    // shutdown Siddhi app
    Thread.sleep(5000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(count <= (inputEventCount + 10));
    AssertJUnit.assertEquals(new Long(2045), lastValue);
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) IncrementalFileSystemPersistenceStore(io.siddhi.core.util.persistence.IncrementalFileSystemPersistenceStore) AtomicLong(java.util.concurrent.atomic.AtomicLong) Event(io.siddhi.core.event.Event) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

Event (io.siddhi.core.event.Event)1298 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1257 SiddhiManager (io.siddhi.core.SiddhiManager)1255 Test (org.testng.annotations.Test)1251 InputHandler (io.siddhi.core.stream.input.InputHandler)1216 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)818 StreamCallback (io.siddhi.core.stream.output.StreamCallback)303 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)86 SiddhiApp (io.siddhi.query.api.SiddhiApp)82 Query (io.siddhi.query.api.execution.query.Query)81 ArrayList (java.util.ArrayList)80 Logger (org.apache.logging.log4j.core.Logger)52 StreamEvent (io.siddhi.core.event.stream.StreamEvent)38 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)34 ComplexEvent (io.siddhi.core.event.ComplexEvent)29 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)29 UnitTestAppender (io.siddhi.core.UnitTestAppender)18 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)17 HashMap (java.util.HashMap)15 InMemoryPersistenceStore (io.siddhi.core.util.persistence.InMemoryPersistenceStore)14