Java Code Examples for io.siddhi.core.SiddhiAppRuntime#persist()
The following examples show how to use
io.siddhi.core.SiddhiAppRuntime#persist() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@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(); }
Example 2
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(dependsOnMethods = "persistenceTest6") public void persistenceTest7() throws InterruptedException { log.info("persistence test 7 - external time window with group by 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, timestamp long);" + "" + "@info(name = 'query1')" + "from StockStream#window.externalTime(timestamp,3 sec) " + "select symbol, price, sum(volume) as totalVol, timestamp " + "group by symbol " + "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))); if (count == 5) { AssertJUnit.assertEquals(300L, inEvent.getData(2)); } if (count == 6) { AssertJUnit.assertEquals(100L, inEvent.getData(2)); } } } }; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); long currentTime = 0; inputHandler.send(new Object[]{"IBM", 75.1f, 100, currentTime + 1000}); Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 75.2f, 100, currentTime + 2000}); Thread.sleep(100); inputHandler.send(new Object[]{"IBM", 75.3f, 100, currentTime + 3000}); Thread.sleep(500); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(3, count); //persisting Thread.sleep(500); siddhiAppRuntime.persist(); //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.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } inputHandler.send(new Object[]{"IBM", 75.4f, 100, currentTime + 4000}); Thread.sleep(100); inputHandler.send(new Object[]{"IBM", 75.5f, 100, currentTime + 5000}); Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 75.6f, 100, currentTime + 6000}); //shutdown siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); AssertJUnit.assertEquals(count, 6); AssertJUnit.assertEquals(true, eventArrived); }
Example 3
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(dependsOnMethods = "persistenceTest5") public void persistenceTest6() throws InterruptedException { log.info("persistence test 6 - batch 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.timeBatch(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))); } } }; 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(500); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(2, count); //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); inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { log.error(e.getMessage(), e); Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } inputHandler.send(new Object[]{"IBM", 75.6f, 100}); Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 75.6f, 100}); //shutdown siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); AssertJUnit.assertEquals(count, 6); AssertJUnit.assertEquals(true, eventArrived); }
Example 4
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(dependsOnMethods = "persistenceTest4") public void persistenceTest5() throws InterruptedException { log.info("persistence test 5 - window restart expired event "); 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 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; if (inEvents != null) { for (Event inEvent : inEvents) { count++; AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0))); firstValue = (Long) inEvent.getData(2); } } if (removeEvents != null) { for (Event removeEvent : removeEvents) { count++; lastValue = (Long) removeEvent.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(firstValue, 200); //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(15000); siddhiAppRuntime.shutdown(); AssertJUnit.assertEquals(400, firstValue); AssertJUnit.assertEquals(null, lastValue); AssertJUnit.assertEquals(true, eventArrived); }
Example 5
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(dependsOnMethods = "persistenceTest11") public void persistenceTest12() throws InterruptedException { log.info("persistence test 12 - partition query"); PersistenceStore persistenceStore = new InMemoryPersistenceStore(); SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setPersistenceStore(persistenceStore); String siddhiApp = "@App:name('TestPlan1')\n" + "define stream TempStream(deviceID long);\n" + "\n" + "define stream DeviceTempStream (deviceID long, count long);\n" + "\n" + "from TempStream\n" + "select * insert into TempInternalStream;\n" + "\n" + "partition with ( deviceID of TempInternalStream )\n" + "begin\n" + "from TempInternalStream\n" + "select deviceID, count() as count\n" + "insert into DeviceTempStream\n" + "end;"; StreamCallback queryCallback = new StreamCallback() { @Override public void receive(Event[] events) { EventPrinter.print(events); eventArrived = true; count++; lastValue = (Long) events[0].getData(1); } }; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("DeviceTempStream", queryCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("TempStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{1}); Thread.sleep(100); inputHandler.send(new Object[]{1}); Thread.sleep(100); inputHandler.send(new Object[]{1}); Thread.sleep(100); inputHandler.send(new Object[]{2}); Thread.sleep(100); inputHandler.send(new Object[]{2}); Thread.sleep(600); //persisting siddhiAppRuntime.persist(); inputHandler.send(new Object[]{2}); Thread.sleep(100); inputHandler.send(new Object[]{2}); //restarting siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("DeviceTempStream", queryCallback); inputHandler = siddhiAppRuntime.getInputHandler("TempStream"); siddhiAppRuntime.start(); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } inputHandler.send(new Object[]{1}); Thread.sleep(10); inputHandler.send(new Object[]{1}); Thread.sleep(10); inputHandler.send(new Object[]{2}); Thread.sleep(10); inputHandler.send(new Object[]{2}); //shutdown siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); AssertJUnit.assertTrue(count == 11); AssertJUnit.assertEquals(new Long(4), lastValue); AssertJUnit.assertEquals(true, eventArrived); }
Example 6
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(expectedExceptions = NoPersistenceStoreException.class, dependsOnMethods = "persistenceTest2") public void persistenceTest3() throws Exception { log.info("persistence test 3 - no store defined"); SiddhiManager siddhiManager = new SiddhiManager(); String siddhiApp = "" + "@app:name('Test') " + "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); " + "" + "@info(name = 'query1') " + "from e1=Stream1[price>20] <2:5> -> e2=Stream2[price>20] " + "select e1[0].price as price1_0, e1[1].price as price1_1, e1[2].price as price1_2, " + " e1[3].price as price1_3, e2.price as price2 " + "insert into OutputStream ;"; 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.assertArrayEquals(new Object[]{25.6f, 47.6f, null, null, 45.7f}, inEvent.getData()); } } }; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1"); InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2"); siddhiAppRuntime.start(); stream1.send(new Object[]{"WSO2", 25.6f, 100}); Thread.sleep(100); stream1.send(new Object[]{"GOOG", 47.6f, 100}); Thread.sleep(100); stream1.send(new Object[]{"GOOG", 13.7f, 100}); Thread.sleep(100); AssertJUnit.assertEquals("Number of success events", 0, count); AssertJUnit.assertEquals("Event arrived", false, eventArrived); //persisting Thread.sleep(500); PersistenceReference persistenceReference = siddhiAppRuntime.persist(); try { persistenceReference.getFuture().get(); } catch (ExecutionException e) { throw e.getCause() instanceof NoPersistenceStoreException ? new NoPersistenceStoreException() : e; } //restarting siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); }
Example 7
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(dependsOnMethods = "persistenceTest1") public void persistenceTest2() throws InterruptedException { log.info("persistence test 2 - pattern count query"); PersistenceStore persistenceStore = new InMemoryPersistenceStore(); SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setPersistenceStore(persistenceStore); String siddhiApp = "" + "@app:name('Test') " + "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); " + "" + "@info(name = 'query1') " + "from e1=Stream1[price>20] <2:5> -> e2=Stream2[price>20] " + "select e1[0].price as price1_0, e1[1].price as price1_1, e1[2].price as price1_2, " + " e1[3].price as price1_3, e2.price as price2 " + "insert into OutputStream ;"; 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.assertArrayEquals(new Object[]{25.6f, 47.6f, null, null, 45.7f}, inEvent.getData()); } } }; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1"); InputHandler stream2; siddhiAppRuntime.start(); stream1.send(new Object[]{"WSO2", 25.6f, 100}); Thread.sleep(100); stream1.send(new Object[]{"GOOG", 47.6f, 100}); Thread.sleep(100); stream1.send(new Object[]{"GOOG", 13.7f, 100}); Thread.sleep(100); AssertJUnit.assertEquals("Number of success events", 0, count); AssertJUnit.assertEquals("Event arrived", false, eventArrived); //persisting Thread.sleep(500); siddhiAppRuntime.persist(); //restarting siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); stream1 = siddhiAppRuntime.getInputHandler("Stream1"); stream2 = siddhiAppRuntime.getInputHandler("Stream2"); siddhiAppRuntime.start(); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } stream2.send(new Object[]{"IBM", 45.7f, 100}); Thread.sleep(500); stream1.send(new Object[]{"GOOG", 47.8f, 100}); Thread.sleep(500); stream2.send(new Object[]{"IBM", 55.7f, 100}); Thread.sleep(500); //shutdown siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); AssertJUnit.assertEquals("Number of success events", 1, count); AssertJUnit.assertEquals("Event arrived", true, eventArrived); }
Example 8
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@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[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); 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); inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed", e); } 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); }
Example 9
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest12() throws InterruptedException { log.info("Incremental file persistence test 12 - length window query with max attribute aggregator"); final int eventWindowSize = 5; SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String siddhiApp = "" + "@app:name('incrementalPersistenceTest12') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream#window.length(" + eventWindowSize + ") " + "select symbol, price, max(volume) as maxVol " + "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 = new Long((Integer) 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, 500}); inputHandler.send(new Object[]{"IBM", 75.6f, 200}); inputHandler.send(new Object[]{"IBM", 75.6f, 300}); inputHandler.send(new Object[]{"IBM", 75.6f, 250}); inputHandler.send(new Object[]{"IBM", 75.6f, 150}); Thread.sleep(100); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(new Long(500L), lastValue); //persisting siddhiAppRuntime.persist(); Thread.sleep(5000); //persisting for the second time to store the inc-snapshot siddhiAppRuntime.persist(); Thread.sleep(100); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { log.error(e.getMessage(), e); Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } siddhiAppRuntime.start(); Thread.sleep(5000); inputHandler.send(new Object[]{"IBM", 100.4f, 280}); AssertJUnit.assertEquals((Long) 300L, lastValue); inputHandler.send(new Object[]{"WSO2", 200.4f, 150}); AssertJUnit.assertEquals((Long) 300L, lastValue); inputHandler.send(new Object[]{"IBM", 300.4f, 200}); AssertJUnit.assertEquals((Long) 280L, lastValue); inputHandler.send(new Object[]{"WSO2", 400.4f, 270}); AssertJUnit.assertEquals((Long) 280L, lastValue); inputHandler.send(new Object[]{"WSO2", 400.4f, 280}); AssertJUnit.assertEquals((Long) 280L, lastValue); //shutdown Siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); AssertJUnit.assertEquals(true, eventArrived); }
Example 10
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest11() throws InterruptedException { log.info("Incremental persistence test 11"); AtomicInteger count = new AtomicInteger(); AtomicLong ibmCount = new AtomicLong(); AtomicLong wso2Count = new AtomicLong(); SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String siddhiApp = "@app:name('incrementalPersistenceTest11') " + "define stream StockQuote (symbol string, price float, volume int);" + "partition with (symbol of StockQuote) " + "begin " + "@info(name = 'query1') " + "from StockQuote#window.length(4) " + "select symbol, count(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); AssertJUnit.assertTrue("IBM".equals(events[0].getData(0)) || "WSO2".equals(events[0].getData(0))); count.addAndGet(events.length); for (Event event : events) { if ("IBM".equals(event.getData(0))) { ibmCount.set((Long) event.getData(1)); } else { wso2Count.set((Long) event.getData(1)); } } eventArrived = true; } }; siddhiAppRuntime.addCallback("OutStockStream", streamCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockQuote"); siddhiAppRuntime.start(); inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"IBM", 700f, 100})); inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"WSO2", 60f, 50})); inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"WSO2", 50f, 60})); inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"WSO2", 40f, 60})); inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"WSO2", 30f, 60})); siddhiAppRuntime.persist(); Thread.sleep(2000); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("OutStockStream", streamCallback); inputHandler = siddhiAppRuntime.getInputHandler("StockQuote"); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } siddhiAppRuntime.start(); Thread.sleep(2000); inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"IBM", 800f, 100})); inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"WSO2", 20f, 60})); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(7, count.get()); AssertJUnit.assertEquals(2, ibmCount.get()); AssertJUnit.assertEquals(4, wso2Count.get()); siddhiAppRuntime.shutdown(); }
Example 11
Source File: SessionWindowTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@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(); }
Example 12
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(dependsOnMethods = "persistenceTest8") public void persistenceTest9() throws InterruptedException { log.info("persistence test 9 - batch 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 long );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.timeBatch(300) " + "select * " + "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; if (inEvents != null) { for (Event inEvent : inEvents) { atomicCount.incrementAndGet(); 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, 100L}); inputHandler.send(new Object[]{"WSO2", 75.6f, 101L}); inputHandler.send(new Object[]{"IBM", 75.6f, 102L}); Thread.sleep(400); inputHandler.send(new Object[]{"WSO2", 75.6f, 103L}); inputHandler.send(new Object[]{"WSO2", 75.6f, 104L}); Thread.sleep(100); AssertJUnit.assertTrue(eventArrived); //persisting siddhiAppRuntime.persist(); inputHandler.send(new Object[]{"IBM", 75.6f, 105L}); inputHandler.send(new Object[]{"WSO2", 75.6f, 106L}); Thread.sleep(50); //restarting execution plan siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } inputHandler.send(new Object[]{"IBM", 75.6f, 107L}); inputHandler.send(new Object[]{"IBM", 75.6f, 108L}); Thread.sleep(10); SiddhiTestHelper.waitForEvents(100, 7, atomicCount, 10000); AssertJUnit.assertEquals(7, atomicCount.get()); //shutdown siddhi app siddhiAppRuntime.shutdown(); }
Example 13
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest8() throws InterruptedException { log.info("Incremental persistence test 8 - min-max counting."); final int inputEventCount = 10; SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String streams = "" + "@app:name('incrementalPersistenceTest8') " + "define stream TempStream (roomNo long, temp long); " + "define stream MaxTempStream (roomNo long, maxTemp long); "; String query = "" + "@info(name = 'query1') " + "from TempStream#window.length(10) " + "select roomNo, max(temp) as maxTemp " + "insert into MaxTempStream; "; 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); eventArrived = true; if (inEvents != null) { for (Event event : inEvents) { count++; inEventsList.add(event.getData()); inEventCount.incrementAndGet(); lastValue = (Long) event.getData(1); } eventArrived = true; } if (removeEvents != null) { removeEventCount = removeEventCount + removeEvents.length; } } }; try { siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler stockStream = siddhiAppRuntime.getInputHandler("TempStream"); siddhiAppRuntime.start(); for (int i = 0; i < inputEventCount; i++) { stockStream.send(new Object[]{i, 55L + i}); } //persisting siddhiAppRuntime.persist(); Thread.sleep(5000); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query); siddhiAppRuntime.addCallback("query1", queryCallback); stockStream = siddhiAppRuntime.getInputHandler("TempStream"); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } siddhiAppRuntime.start(); Thread.sleep(2000); stockStream.send(new Object[]{inputEventCount + 1, 1000L}); //persisting siddhiAppRuntime.persist(); Thread.sleep(2000); stockStream.send(new Object[]{inputEventCount + 2, 20L}); siddhiAppRuntime.persist(); Thread.sleep(2000); stockStream.send(new Object[]{inputEventCount + 3, 30L}); AssertJUnit.assertTrue(count <= (inputEventCount + 3)); AssertJUnit.assertEquals(new Long(1000), lastValue); AssertJUnit.assertEquals(true, eventArrived); } finally { siddhiAppRuntime.shutdown(); } }
Example 14
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest5() throws InterruptedException { log.info("Incremental persistence test 5 - external time window query"); final int inputEventCount = 10; final int eventWindowSizeInSeconds = 2; SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String siddhiApp = "" + "@app:name('incrementalPersistenceTest5') " + "" + "define stream StockStream ( iij_timestamp long, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.externalTime(iij_timestamp, " + eventWindowSizeInSeconds + " sec) " + "select iij_timestamp, 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++; 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(new Object[]{(long) i, 75.6f + i, 100}); } Thread.sleep(4000); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(new Long(1000), lastValue); //persisting siddhiAppRuntime.persist(); Thread.sleep(5000); inputHandler.send(new Object[]{(1L + inputEventCount), 100.4f, 150}); //Thread.sleep(100); inputHandler.send(new Object[]{(2L + inputEventCount), 200.4f, 110}); inputHandler.send(new Object[]{(3L + inputEventCount), 300.4f, 100}); //Thread.sleep(100); inputHandler.send(new Object[]{(4L + inputEventCount), 400.4f, 300}); inputHandler.send(new Object[]{(5L + inputEventCount), 500.6f, 120}); Thread.sleep(10); inputHandler.send(new Object[]{(6L + inputEventCount), 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"); } siddhiAppRuntime.start(); inputHandler.send(new Object[]{(7L + inputEventCount), 700.6f, 230}); Thread.sleep(10); inputHandler.send(new Object[]{(8L + inputEventCount), 800.6f, 125}); inputHandler.send(new Object[]{(9L + inputEventCount), 900.6f, 370}); Thread.sleep(10); inputHandler.send(new Object[]{(10L + inputEventCount), 1000.6f, 140}); //shutdown Siddhi app Thread.sleep(5000); siddhiAppRuntime.shutdown(); AssertJUnit.assertTrue(count <= (inputEventCount + 10)); AssertJUnit.assertEquals(new Long(3045), lastValue); AssertJUnit.assertEquals(true, eventArrived); }
Example 15
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest4_1() throws InterruptedException { log.info("Incremental persistence test 4_1 - time batch window query"); final int inputEventCount = 10; final int eventWindowSizeInSeconds = 2; SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String siddhiApp = "" + "@app:name('incrementalPersistenceTest4') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.timeBatch(" + 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(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(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"); } siddhiAppRuntime.start(); Thread.sleep(5000); 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(5000); siddhiAppRuntime.shutdown(); AssertJUnit.assertTrue(count <= (inputEventCount + 10)); AssertJUnit.assertEquals(new Long(865), lastValue); AssertJUnit.assertEquals(true, eventArrived); }
Example 16
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest4() throws InterruptedException { log.info("Incremental persistence test 4 - time batch window query"); final int inputEventCount = 10; final int eventWindowSizeInSeconds = 2; SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String siddhiApp = "" + "@app:name('incrementalPersistenceTest4') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.timeBatch(" + 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(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(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"); } siddhiAppRuntime.start(); Thread.sleep(50); 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(5000); siddhiAppRuntime.shutdown(); AssertJUnit.assertTrue(count <= (inputEventCount + 10)); AssertJUnit.assertEquals(new Long(2045), lastValue); AssertJUnit.assertEquals(true, eventArrived); }
Example 17
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@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); }
Example 18
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(dependsOnMethods = "persistenceTest10") public void persistenceTest11() throws InterruptedException { log.info("persistence test 11 - batch 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 long );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.lengthBatch(2) " + "select *" + "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; atomicCount.incrementAndGet(); for (Event inEvent : inEvents) { AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0))); } if (removeEvents != null) { for (Event removeEvent : removeEvents) { lastValue = (Long) removeEvent.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, 100L}); inputHandler.send(new Object[]{"WSO2", 75.6f, 101L}); inputHandler.send(new Object[]{"IBM", 75.6f, 102L}); inputHandler.send(new Object[]{"IBM", 75.6f, 103L}); SiddhiTestHelper.waitForEvents(100, 2, atomicCount, 10000); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(new Long(101), lastValue); //persisting siddhiAppRuntime.persist(); Thread.sleep(500); inputHandler.send(new Object[]{"WSO2", 75.6f, 50L}); inputHandler.send(new Object[]{"IBM", 75.6f, 50L}); inputHandler.send(new Object[]{"IBM", 75.6f, 50L}); //restarting execution plan Thread.sleep(500); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } inputHandler.send(new Object[]{"IBM", 75.6f, 100L}); //shutdown siddhi app Thread.sleep(500); SiddhiTestHelper.waitForEvents(100, 3, atomicCount, 10000); AssertJUnit.assertEquals(new Long(103), lastValue); siddhiAppRuntime.shutdown(); }
Example 19
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest1() throws InterruptedException { log.info("Incremental persistence test 1 - length window query"); final int inputEventCount = 10; final int eventWindowSize = 4; SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String siddhiApp = "" + "@app:name('incrementalPersistenceTest1') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.length(" + eventWindowSize + ") " + "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(); 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[]{"IBM", 100.4f, 100}); //Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 200.4f, 100}); inputHandler.send(new Object[]{"IBM", 300.4f, 100}); //Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 400.4f, 200}); 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) { log.error(e.getMessage(), e); Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } siddhiAppRuntime.start(); Thread.sleep(5000); inputHandler.send(new Object[]{"IBM", 500.6f, 300}); inputHandler.send(new Object[]{"WSO2", 600.6f, 400}); //shutdown Siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); AssertJUnit.assertTrue(count <= (inputEventCount + 6)); AssertJUnit.assertEquals(new Long(1000), lastValue); AssertJUnit.assertEquals(true, eventArrived); }
Example 20
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(dependsOnMethods = "persistenceTest9") public void persistenceTest10() throws InterruptedException { log.info("persistence test 10 - sort 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#window.sort(2,volume) " + "select volume " + "insert all events into outputStream ;"; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); QueryCallback queryCallback = new QueryCallback() { @Override public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timeStamp, inEvents, removeEvents); eventArrived = true; atomicCount.incrementAndGet(); for (Event inEvent : inEvents) { count++; } if (removeEvents != null) { for (Event removeEvent : removeEvents) { lastValueRemoved = (Integer) removeEvent.getData(0); } } } }; siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"WSO2", 55.6f, 100}); inputHandler.send(new Object[]{"IBM", 75.6f, 300}); inputHandler.send(new Object[]{"WSO2", 57.6f, 200}); Thread.sleep(1000); AssertJUnit.assertEquals(3, count); AssertJUnit.assertTrue(eventArrived); // persisting siddhiAppRuntime.persist(); inputHandler.send(new Object[]{"WSO2", 55.6f, 20}); inputHandler.send(new Object[]{"WSO2", 57.6f, 40}); Thread.sleep(500); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } inputHandler.send(new Object[]{"WSO2", 55.6f, 20}); SiddhiTestHelper.waitForEvents(100, 6, atomicCount, 10000); AssertJUnit.assertEquals(true, eventArrived); AssertJUnit.assertEquals(200, lastValueRemoved); siddhiAppRuntime.shutdown(); }