Search in sources :

Example 11 with InMemoryPersistenceStore

use of io.siddhi.core.util.persistence.InMemoryPersistenceStore in project siddhi by wso2.

the class PersistenceTestCase method persistenceTest4.

@Test(dependsOnMethods = "persistenceTest3")
public void persistenceTest4() throws InterruptedException {
    log.info("persistence test 4 - window restart");
    PersistenceStore persistenceStore = new InMemoryPersistenceStore();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);
    String siddhiApp = "" + "@app:name('Test') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.time(10 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);
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    Thread.sleep(100);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(new Long(200), lastValue);
    // persisting
    Thread.sleep(500);
    siddhiAppRuntime.persist();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    // restarting siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    // shutdown siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(new Long(400), lastValue);
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) 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 12 with InMemoryPersistenceStore

use of io.siddhi.core.util.persistence.InMemoryPersistenceStore in project siddhi by wso2.

the class PersistenceTestCase method persistenceTest13.

@Test(dependsOnMethods = "persistenceTest12")
public void persistenceTest13() throws InterruptedException {
    log.info("Persistence test 13 - partitioned sum with group-by on length windows.");
    final int inputEventCount = 10;
    PersistenceStore persistenceStore = new InMemoryPersistenceStore();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);
    String siddhiApp = "@app:name('incrementalPersistenceTest10') " + "define stream cseEventStreamOne (symbol string, price float,volume int);" + "partition with (price>=100 as 'large' or price<100 as 'small' of cseEventStreamOne) " + "begin " + "@info(name = 'query1') " + "from cseEventStreamOne#window.length(4) " + "select symbol,sum(price) as price " + "group by symbol " + "insert into OutStockStream;  " + "end ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    StreamCallback streamCallback = new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            if (events != null) {
                for (Event event : events) {
                    count++;
                    lastValue = ((Double) event.getData(1)).longValue();
                }
            }
        }
    };
    siddhiAppRuntime.addCallback("OutStockStream", streamCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStreamOne");
    siddhiAppRuntime.start();
    for (int i = 0; i < inputEventCount; i++) {
        inputHandler.send(new Object[] { "IBM", 95f + i, 100 });
        Thread.sleep(100);
        siddhiAppRuntime.persist();
    }
    inputHandler.send(new Object[] { "IBM", 205f, 100 });
    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", streamCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("cseEventStreamOne");
    siddhiAppRuntime.start();
    Thread.sleep(1000);
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    Thread.sleep(1000);
    inputHandler.send(new Object[] { "IBM", 105f, 100 });
    Thread.sleep(1000);
    AssertJUnit.assertEquals(new Long(414), lastValue);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 13 with InMemoryPersistenceStore

use of io.siddhi.core.util.persistence.InMemoryPersistenceStore in project siddhi by wso2.

the class PersistenceTestCase method persistenceTest8.

@Test(dependsOnMethods = "persistenceTest7")
public void persistenceTest8() throws InterruptedException {
    log.info("persistence test 8 - 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 int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.length(10) " + "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);
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    Thread.sleep(100);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(new Long(200), lastValue);
    // persisting
    Thread.sleep(500);
    byte[] snapshot = siddhiAppRuntime.snapshot();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    // restarting siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    // loading
    try {
        siddhiAppRuntime.restore(snapshot);
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    // shutdown siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(count == 6);
    AssertJUnit.assertEquals(new Long(400), lastValue);
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) 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 14 with InMemoryPersistenceStore

use of io.siddhi.core.util.persistence.InMemoryPersistenceStore in project siddhi by wso2.

the class LogTestCase method persistenceTest1.

@Test
public void persistenceTest1() throws InterruptedException {
    log.info("persistence test 1 - 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 int );" + "" + "@info(name = 'query1')" + "from StockStream" + "   #log()" + "   #log('test message')" + "   #log(false)" + "   #log(true)" + "   #log('test message',false)" + "   #log('test message',true)" + "   #log('error','test message')" + "   #log('error','test message',false)" + "   #log('warn','test message',true)" + "select * " + "insert into OutStream ";
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Event[] { new Event(System.currentTimeMillis(), new Object[] { "IBM", 75.6f, 100 }), new Event(System.currentTimeMillis(), new Object[] { "GOOG", 70.6f, 100 }) });
    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) 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 15 with InMemoryPersistenceStore

use of io.siddhi.core.util.persistence.InMemoryPersistenceStore in project siddhi by wso2.

the class SessionWindowTestCase method testSessionWindow18.

@Test(description = "Check if events are persist when using session window")
public void testSessionWindow18() throws InterruptedException {
    log.info("SessionWindow Test18: Testing persistence ");
    PersistenceStore persistenceStore = new InMemoryPersistenceStore();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);
    String purchaseEventStream = "" + "define stream purchaseEventStream (user string, item_number int, price float, quantity int); ";
    String query = "" + "@info(name = 'query0') " + "from purchaseEventStream#window.session(2 sec, user) " + "select * " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(purchaseEventStream + query);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            count.addAndGet(events.length);
            for (Event event : events) {
                innerAssertionsPassed = false;
                AssertJUnit.assertTrue(("101".equals(event.getData(1).toString()) || "102".equals(event.getData(1).toString())) || "103".equals(event.getData(1).toString()));
                innerAssertionsPassed = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("purchaseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "user0", 101, 34.4, 5 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "user0", 102, 24.5, 2 });
    siddhiAppRuntime.persist();
    siddhiAppRuntime.shutdown();
    inputHandler = siddhiAppRuntime.getInputHandler("purchaseEventStream");
    siddhiAppRuntime.start();
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    inputHandler.send(new Object[] { "user0", 103, 24.5, 2 });
    SiddhiTestHelper.waitForEvents(100, 3, count, 4200);
    AssertJUnit.assertTrue(innerAssertionsPassed);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)15 SiddhiManager (io.siddhi.core.SiddhiManager)15 InputHandler (io.siddhi.core.stream.input.InputHandler)15 InMemoryPersistenceStore (io.siddhi.core.util.persistence.InMemoryPersistenceStore)15 PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)15 Test (org.testng.annotations.Test)15 Event (io.siddhi.core.event.Event)14 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)14 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)11 StreamCallback (io.siddhi.core.stream.output.StreamCallback)4