Java Code Examples for io.siddhi.core.SiddhiAppRuntime#addCallback()
The following examples show how to use
io.siddhi.core.SiddhiAppRuntime#addCallback() .
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: FilterTestCase1.java From siddhi with Apache License 2.0 | 5 votes |
@Test public void testFilterQuery60() throws InterruptedException { log.info("Filter test60"); SiddhiManager siddhiManager = new SiddhiManager(); StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE) .attribute("quantity", Attribute.Type.INT); Query query = new Query(); query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("quantity"), Compare.Operator.EQUAL, Expression.value(2)))); query.annotation(Annotation.annotation("info").element("name", "query1")); query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression .variable("price")).select("quantity", Expression.variable("quantity"))); query.insertInto("outputStream"); SiddhiApp siddhiApp = new SiddhiApp("ep1"); siddhiApp.defineStream(cseEventStream); siddhiApp.addQuery(query); SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", new QueryCallback() { @Override public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timeStamp, inEvents, removeEvents); count.addAndGet(inEvents.length); } }); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"WSO2", 50f, 60d, 5}); inputHandler.send(new Object[]{"WSO2", 70f, 60d, 2}); inputHandler.send(new Object[]{"WSO2", 60f, 200d, 4}); SiddhiTestHelper.waitForEvents(10, 1, count, 100); siddhiAppRuntime.shutdown(); }
Example 2
Source File: PartitionTestCase1.java From siddhi with Apache License 2.0 | 5 votes |
@Test public void testPartitionQuery22() throws InterruptedException { log.info("Partition test22"); SiddhiManager siddhiManager = new SiddhiManager(); String siddhiApp = "@app:name('PartitionTest10') " + "define stream cseEventStream (symbol string, price float,volume int);" + "define stream cseEventStream1 (symbol string, price float,volume int);" + "partition with (symbol of cseEventStream)" + "begin" + "@info(name = 'query') from cseEventStream#window.time(1 sec) " + "select symbol, avg(price) as avgPrice, volume " + "having avgPrice > 10" + "insert expired events into OutStockStream ;" + "end "; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() { @Override public void receive(Event[] events) { EventPrinter.print(events); for (Event event : events) { count.incrementAndGet(); if (count.get() == 1) { AssertJUnit.assertEquals(75.0, event.getData()[1]); } eventArrived = true; } } }); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"IBM", 75f, 100}); inputHandler.send(new Object[]{"IBM", 75f, 100}); SiddhiTestHelper.waitForEvents(200, 1, count, 60000); AssertJUnit.assertTrue(1 <= count.get()); siddhiAppRuntime.shutdown(); }
Example 3
Source File: SequencePartitionTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testSequencePartitionQuery8() throws InterruptedException { log.info("Pattern -testSequence8 - OUT 2"); 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 partitionStart = "partition with (volume of Stream1 , volume of Stream2) begin "; String query = "" + "@info(name = 'query1') " + "from every e1=Stream2[price>20], e2=Stream2[price>e1.price] or e3=Stream2[symbol=='IBM'] " + "select e1.price as price1, e2.price as price2, e3.price as price3 " + "insert into OutputStream ;"; String partitionEnd = "end"; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + partitionStart + query + partitionEnd); siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() { @Override public void receive(Event[] events) { EventPrinter.print(events); for (Event event : events) { if (event.isExpired()) { removeEventCount++; } else { inEventCount++; switch (inEventCount) { case 1: AssertJUnit.assertArrayEquals(new Object[]{55.6f, null, 55.0f}, event.getData()); break; case 2: AssertJUnit.assertArrayEquals(new Object[]{155.6f, null, 95.0f}, event.getData()); break; case 3: AssertJUnit.assertArrayEquals(new Object[]{55.0f, 57.6f, null}, event.getData()); break; case 4: AssertJUnit.assertArrayEquals(new Object[]{95.0f, 207.6f, null}, event.getData()); break; default: AssertJUnit.assertSame(4, inEventCount); } } eventArrived = true; } } }); InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1"); InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2"); siddhiAppRuntime.start(); stream2.send(new Object[]{"WSO2", 59.6f, 100}); Thread.sleep(100); stream2.send(new Object[]{"WSO2", 259.6f, 200}); Thread.sleep(100); stream2.send(new Object[]{"WSO2", 55.6f, 100}); Thread.sleep(100); stream2.send(new Object[]{"WSO2", 155.6f, 200}); Thread.sleep(100); stream2.send(new Object[]{"IBM", 55.0f, 100}); Thread.sleep(100); stream2.send(new Object[]{"IBM", 95.0f, 200}); Thread.sleep(100); stream2.send(new Object[]{"WSO2", 57.6f, 100}); Thread.sleep(100); stream2.send(new Object[]{"WSO2", 207.6f, 200}); Thread.sleep(100); AssertJUnit.assertEquals("Number of success events", 4, inEventCount); AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount); AssertJUnit.assertEquals("Event arrived", true, eventArrived); siddhiAppRuntime.shutdown(); }
Example 4
Source File: PartitionTestCase1.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testPartitionQuery3() throws InterruptedException { log.info("Partition test3"); SiddhiManager siddhiManager = new SiddhiManager(); SiddhiApp siddhiApp = new SiddhiApp("plan3"); StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT); siddhiApp.defineStream(streamDefinition); Partition partition = Partition.partition(). with("cseEventStream", Expression.variable("symbol")); Query query = Query.query(); query.from(InputStream.stream("cseEventStream")); query.select( Selector.selector(). select("symbol", Expression.variable("symbol")). select("price", Expression.variable("price")). select("volume", Expression.variable("volume")) ); query.insertIntoInner("StockStream"); Query query1 = Query.query(); query1.from(InputStream.innerStream("StockStream")); query1.select( Selector.selector(). select("symbol", Expression.variable("symbol")). select("price", Expression.variable("price")). select("volume", Expression.variable("volume")) ); query1.insertInto("OutStockStream"); partition.addQuery(query); partition.addQuery(query1); siddhiApp.addPartition(partition); SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() { @Override public void receive(Event[] events) { EventPrinter.print(events); count.addAndGet(events.length); eventArrived = true; } }); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"IBM", 75.6f, 100}); inputHandler.send(new Object[]{"WSO2", 75.6f, 100}); inputHandler.send(new Object[]{"IBM", 75.6f, 100}); inputHandler.send(new Object[]{"ORACLE", 75.6f, 100}); SiddhiTestHelper.waitForEvents(100, 4, count, 60000); siddhiAppRuntime.shutdown(); AssertJUnit.assertEquals(4, count.get()); }
Example 5
Source File: PatternPartitionTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testPatternPartitionQuery27() throws InterruptedException { log.info("testPatternComplex1 - OUT 2"); 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 partitionStart = "partition with (volume of Stream1,volume of Stream2) begin "; String query = "" + "@info(name = 'query1') " + "from every ( e1=Stream1[price > 20] -> e2=Stream2[price > e1.price] or e3=Stream2['IBM' == symbol]) " + " -> e4=Stream2[price > e1.price] " + "select e1.price as price1, e2.price as price2, e3.price as price3, e4.price as price4 " + "insert into OutputStream ;"; String partitionEnd = "end"; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + partitionStart + query + partitionEnd); siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() { @Override public void receive(Event[] events) { EventPrinter.print(events); for (Event event : events) { if (event.isExpired()) { removeEventCount.incrementAndGet(); } else { inEventCount.incrementAndGet(); switch (inEventCount.get()) { case 1: AssertJUnit.assertArrayEquals(new Object[]{55.6f, 55.7f, null, 57.7f}, event.getData()); break; case 2: AssertJUnit.assertArrayEquals(new Object[]{54.0f, 57.7f, null, 59.7f}, event.getData()); break; default: AssertJUnit.assertSame(1, inEventCount.get()); } } 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); stream2.send(new Object[]{"WSO2", 55.7f, 100}); Thread.sleep(100); stream2.send(new Object[]{"GOOG", 55f, 100}); Thread.sleep(100); stream1.send(new Object[]{"GOOG", 54f, 100}); Thread.sleep(100); stream2.send(new Object[]{"IBM", 57.7f, 100}); Thread.sleep(100); stream2.send(new Object[]{"IBM", 59.7f, 100}); SiddhiTestHelper.waitForEvents(100, 2, inEventCount, 60000); AssertJUnit.assertEquals("Number of success events", 2, inEventCount.get()); AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get()); AssertJUnit.assertEquals("Event arrived", true, eventArrived); siddhiAppRuntime.shutdown(); }
Example 6
Source File: PartitionTestCase1.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testPartitionQuery7() throws InterruptedException { log.info("Partition test7"); SiddhiManager siddhiManager = new SiddhiManager(); String siddhiApp = "@app:name('PartitionTest7') " + "define stream cseEventStream (symbol string, price float,volume int);" + "define stream cseEventStream1 (symbol string, price float,volume int);" + "partition with (symbol of cseEventStream)" + "begin" + "@info(name = 'query') from cseEventStream select symbol,sum(price) as price,volume insert into " + "OutStockStream ;" + "end "; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() { @Override public void receive(Event[] events) { EventPrinter.print(events); for (Event event : events) { count.incrementAndGet(); if (count.get() == 1) { AssertJUnit.assertEquals(75.0, event.getData()[1]); } else if (count.get() == 2) { AssertJUnit.assertEquals(705.0, event.getData()[1]); } else if (count.get() == 3) { AssertJUnit.assertEquals(110.0, event.getData()[1]); } else if (count.get() == 4) { AssertJUnit.assertEquals(50.0, event.getData()[1]); } eventArrived = true; } } }); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"IBM", 75f, 100}); inputHandler.send(new Object[]{"WSO2", 705f, 100}); inputHandler.send(new Object[]{"IBM", 35f, 100}); inputHandler.send(new Object[]{"ORACLE", 50.0f, 100}); SiddhiTestHelper.waitForEvents(100, 4, count, 60000); AssertJUnit.assertEquals(4, count.get()); siddhiAppRuntime.shutdown(); }
Example 7
Source File: CastFunctionExecutorTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testCastFunctionExtension() throws InterruptedException { log.info("CastFunctionExecutor TestCase"); SiddhiManager siddhiManager = new SiddhiManager(); String inStreamDefinition = "\ndefine stream inputStream (symbol string, price object, volume long);"; String query = ("@info(name = 'query1') from inputStream select symbol,price, " + "cast(price, 'double') as priceInDouble insert into outputStream;"); SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inStreamDefinition + query); siddhiAppRuntime.addCallback("query1", new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); for (Event event : inEvents) { count.incrementAndGet(); if (count.get() == 1) { AssertJUnit.assertEquals(100.3d, event.getData(2)); eventArrived = true; } if (count.get() == 2) { AssertJUnit.assertEquals(true, event.getData(2)); eventArrived = true; } if (count.get() == 3) { AssertJUnit.assertEquals(300d, event.getData(2)); eventArrived = true; } } } }); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("inputStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"IBM", 100.3, 100L}); inputHandler.send(new Object[]{"WSO2", true, 200L}); inputHandler.send(new Object[]{"XYZ", 300d, 200L}); SiddhiTestHelper.waitForEvents(100, 3, count, 60000); AssertJUnit.assertEquals(3, count.get()); AssertJUnit.assertTrue(eventArrived); siddhiAppRuntime.shutdown(); }
Example 8
Source File: SessionWindowTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(description = "This test case checks two sessions are created and processed after the allowedLatency") public void testSessionWindow14() throws InterruptedException { log.info("SessionWindow Test14: Testing session window, " + "two sessions are created and processed after the allowedLatency"); SiddhiManager siddhiManager = new SiddhiManager(); 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, 1 sec) " + "select * " + "insert all events into outputStream ;"; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(purchaseEventStream + query); siddhiAppRuntime.addCallback("query0", new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); if (inEvents != null) { inEventCount = inEventCount + inEvents.length; count.addAndGet(inEvents.length); } if (removeEvents != null) { removeEventCount = removeEventCount + removeEvents.length; count.addAndGet(removeEvents.length); if (removeEventCount == 2) { AssertJUnit.assertTrue("user0".equals(removeEvents[0].getData(0))); AssertJUnit.assertTrue("user0".equals(removeEvents[1].getData(0))); AssertJUnit.assertEquals(101, removeEvents[0].getData(1)); AssertJUnit.assertEquals(102, removeEvents[1].getData(1)); innerAssertionsPassed = true; } else if (removeEventCount == 4) { innerAssertionsPassed = false; AssertJUnit.assertTrue("user0".equals(removeEvents[0].getData(0))); AssertJUnit.assertTrue("user0".equals(removeEvents[1].getData(0))); AssertJUnit.assertEquals(103, removeEvents[0].getData(1)); AssertJUnit.assertEquals(104, removeEvents[1].getData(1)); innerAssertionsPassed = true; } } eventArrived = true; } }); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("purchaseEventStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"user0", 101, 34.4, 5}); inputHandler.send(new Object[]{"user0", 102, 24.5, 2}); Thread.sleep(2005); inputHandler.send(new Object[]{"user0", 103, 22.4, 1}); inputHandler.send(new Object[]{"user0", 104, 50.0, 3}); SiddhiTestHelper.waitForEvents(100, 8, count, 5200); AssertJUnit.assertEquals(4, inEventCount); AssertJUnit.assertEquals(4, removeEventCount); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertTrue(innerAssertionsPassed); siddhiAppRuntime.shutdown(); }
Example 9
Source File: LengthBatchWindowTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void lengthBatchWindowTest6() throws InterruptedException { SiddhiManager siddhiManager = new SiddhiManager(); String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);"; String query = "@info(name = 'query1') " + "from cseEventStream#window.lengthBatch(4) " + "select symbol,sum(price) as sumPrice,volume " + "insert all events into outputStream ;"; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query); siddhiAppRuntime.addCallback("outputStream", new StreamCallback() { @Override public void receive(Event[] events) { EventPrinter.print(events); for (Event event : events) { EventPrinter.print(toMap(event)); AssertJUnit.assertEquals("Events cannot be expired", false, event.isExpired()); inEventCount++; if (inEventCount == 1) { AssertJUnit.assertEquals(100.0, event.getData(1)); } else if (inEventCount == 2) { AssertJUnit.assertEquals(240.0, event.getData(1)); } } eventArrived = true; } }); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"IBM", 10f, 0}); inputHandler.send(new Object[]{"WSO2", 20f, 1}); inputHandler.send(new Object[]{"IBM", 30f, 0}); inputHandler.send(new Object[]{"WSO2", 40f, 1}); inputHandler.send(new Object[]{"IBM", 50f, 0}); inputHandler.send(new Object[]{"WSO2", 60f, 1}); inputHandler.send(new Object[]{"WSO2", 60f, 1}); inputHandler.send(new Object[]{"IBM", 70f, 0}); inputHandler.send(new Object[]{"WSO2", 80f, 1}); siddhiAppRuntime.shutdown(); AssertJUnit.assertEquals(2, inEventCount); AssertJUnit.assertTrue(eventArrived); }
Example 10
Source File: PartitionTestCase2.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testPartitionQuery() throws InterruptedException { log.info("Partition test1"); SiddhiManager siddhiManager = new SiddhiManager(); String siddhiApp = "@app:name('PartitionTest') " + "define stream streamA (ts long, symbol string, price int);" + "partition with (symbol of streamA) " + "begin " + " @info(name = 'query1') " + " from streamA#window.lengthBatch(2, true) " + " select symbol, sum(price) as total " + " insert all events into StockQuote ; " + "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))); int eventCount = count.addAndGet(events.length); eventArrived = true; if (eventCount == 1) { Assert.assertEquals(events[0].getData(1), 700L); } else if (eventCount == 2) { Assert.assertEquals(events[0].getData(1), 60L); } else if (eventCount == 3) { Assert.assertEquals(events[0].getData(1), 120L); } else if (eventCount == 4) { Assert.assertEquals(events[0].getData(1), 60L); } else if (eventCount == 5) { Assert.assertEquals(events[0].getData(1), 1400L); } else if (eventCount == 6) { Assert.assertEquals(events[0].getData(1), 700L); } } }; siddhiAppRuntime.addCallback("StockQuote", streamCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("streamA"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{100L, "IBM", 700}); inputHandler.send(new Object[]{101L, "WSO2", 60}); inputHandler.send(new Object[]{101L, "WSO2", 60}); inputHandler.send(new Object[]{1134L, "WSO2", 60}); inputHandler.send(new Object[]{100L, "IBM", 700}); inputHandler.send(new Object[]{1145L, "IBM", 700}); SiddhiTestHelper.waitForEvents(100, 6, count, 60000); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(6, count.get()); siddhiAppRuntime.shutdown(); }
Example 11
Source File: CastFunctionExecutorTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testCastFunctionExtension9() throws InterruptedException { log.info("CastFunctionExecutor TestCase 9"); SiddhiManager siddhiManager = new SiddhiManager(); String inStreamDefinition = "\ndefine stream inputStream (symbol string, price object, volume long);"; String query = ("@info(name = 'query1') from inputStream select symbol,price, " + "cast(price, 'int') as priceInInt insert into outputStream;"); SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inStreamDefinition + query); siddhiAppRuntime.addCallback("query1", new QueryCallback() { @Override public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timeStamp, inEvents, removeEvents); for (Event event : inEvents) { count.incrementAndGet(); if (count.get() == 1) { AssertJUnit.assertEquals(1003, event.getData(2)); eventArrived = true; } if (count.get() == 2) { AssertJUnit.assertEquals(true, event.getData(2)); eventArrived = true; } if (count.get() == 3) { AssertJUnit.assertEquals(300432, event.getData(2)); eventArrived = true; } } } }); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("inputStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"IBM", 1003, 100L}); inputHandler.send(new Object[]{"WSO2", true, 200L}); inputHandler.send(new Object[]{"XYZ", 300432, 200L}); SiddhiTestHelper.waitForEvents(100, 3, count, 60000); AssertJUnit.assertEquals(3, count.get()); AssertJUnit.assertTrue(eventArrived); siddhiAppRuntime.shutdown(); }
Example 12
Source File: LogicalPatternTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testQuery14() throws InterruptedException { log.info("testQuery14 - OUT 1"); 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') " + "from e1=Stream1[price > 20] or e2=Stream2[price >30] " + "select e1.symbol as symbol1, 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) { inEventCount = inEventCount + inEvents.length; eventArrived = true; AssertJUnit.assertArrayEquals(new Object[]{"WSO2", null}, inEvents[0].getData()); } if (removeEvents != null) { removeEventCount = removeEventCount + removeEvents.length; } eventArrived = true; } }); InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1"); InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2"); siddhiAppRuntime.start(); stream1.send(new Object[]{"WSO2", 25.0f, 100}); Thread.sleep(100); stream2.send(new Object[]{"IBM", 35.0f, 100}); Thread.sleep(100); stream2.send(new Object[]{"ORACLE", 45.0f, 100}); Thread.sleep(100); AssertJUnit.assertEquals("Number of success events", 1, inEventCount); AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount); AssertJUnit.assertEquals("Event arrived", true, eventArrived); siddhiAppRuntime.shutdown(); }
Example 13
Source File: WindowPartitionTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testWindowPartitionQuery1() throws InterruptedException { log.info("Window Partition test1"); SiddhiManager siddhiManager = new SiddhiManager(); String siddhiApp = "define stream cseEventStream (symbol string, price float,volume int);" + "partition with (symbol of cseEventStream) begin @info(name = 'query1') from cseEventStream#window" + ".length(2) select symbol,sum(price) as price,volume insert expired events into OutStockStream ; " + "end "; SiddhiAppRuntime executionRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); executionRuntime.addCallback("OutStockStream", new StreamCallback() { @Override public void receive(Event[] events) { EventPrinter.print(events); for (Event event : events) { removeEventCount++; if (removeEventCount == 1) { AssertJUnit.assertEquals(100.0, event.getData()[1]); } else if (removeEventCount == 2) { AssertJUnit.assertEquals(1000.0, event.getData()[1]); } eventArrived = true; } } }); InputHandler inputHandler = executionRuntime.getInputHandler("cseEventStream"); executionRuntime.start(); inputHandler.send(new Object[]{"IBM", 70f, 100}); inputHandler.send(new Object[]{"WSO2", 700f, 100}); inputHandler.send(new Object[]{"IBM", 100f, 100}); inputHandler.send(new Object[]{"IBM", 200f, 100}); inputHandler.send(new Object[]{"ORACLE", 75.6f, 100}); inputHandler.send(new Object[]{"WSO2", 1000f, 100}); inputHandler.send(new Object[]{"WSO2", 500f, 100}); Thread.sleep(1000); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(2, removeEventCount); executionRuntime.shutdown(); }
Example 14
Source File: LossyFrequentWindowTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void frequentUniqueWindowTest2() throws InterruptedException { log.info("LossyFrequentWindow test2"); SiddhiManager siddhiManager = new SiddhiManager(); String cseEventStream = "" + "define stream purchase (cardNo string, price float); " + "define window purchaseWindow (cardNo string, price float) lossyFrequent(0.3,0.05); "; String query = "" + "@info(name = 'query0') " + "from purchase[price >= 30] " + "insert into purchaseWindow; " + "" + "@info(name = 'query1') " + "from purchaseWindow " + "select cardNo, price " + "insert all events into PotentialFraud ;"; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query); siddhiAppRuntime.addCallback("query1", new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); if (inEvents != null) { inEventCount += inEvents.length; } if (removeEvents != null) { removeEventCount += removeEvents.length; } eventArrived = true; } }); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("purchase"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"3224-3244-2432-4124", 73.36f}); for (int i = 0; i < 25; i++) { inputHandler.send(new Object[]{"3234-3244-2432-4124", 73.36f}); inputHandler.send(new Object[]{"3234-3244-2432-4124", 78.36f}); inputHandler.send(new Object[]{"1234-3244-2432-123", 86.36f}); inputHandler.send(new Object[]{"5768-3244-2432-5646", 48.36f}); //this event will not include in to the // window during first iteration because 1+0<5*0.25 } Thread.sleep(1000); AssertJUnit.assertEquals("Event arrived", true, eventArrived); AssertJUnit.assertEquals("Out Event count", 1, removeEventCount); siddhiAppRuntime.shutdown(); }
Example 15
Source File: BatchWindowTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testBatchWindow5() throws InterruptedException { log.info("BatchWindow test5 - process batch as chunks"); SiddhiManager siddhiManager = new SiddhiManager(); String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume long); " + "define window cseEventWindow (symbol string, price float, volume long) batch(4) output all events; "; String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') " + "from cseEventWindow " + "select * " + "insert all events into outputStream ;"; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query); siddhiAppRuntime.addCallback("query1", new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); chunkCount = chunkCount + 1; if (inEvents != null) { inEventCount = inEventCount + inEvents.length; if (chunkCount != 8) { generalChunkSize = inEvents.length; } else { lastChunkSize = inEvents.length; } } if (removeEvents != null) { removeEventCount = removeEventCount + removeEvents.length; } eventArrived = true; } }); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream"); siddhiAppRuntime.start(); int length1 = 20; int length2 = 10; Event[] eventsSet1 = new Event[length1]; Event[] eventsSet2 = new Event[length2]; for (int i = 0; i < length1; i++) { eventsSet1[i] = new Event(System.currentTimeMillis(), new Object[]{"WSO2", i * 2.5f, 10L}); if (i < length2) { eventsSet2[i] = new Event(System.currentTimeMillis(), new Object[]{"IBM", i * 2.5f, 10L}); } } inputHandler.send(eventsSet1); inputHandler.send(eventsSet2); Thread.sleep(1000); AssertJUnit.assertEquals(30, inEventCount); AssertJUnit.assertEquals(20, removeEventCount); AssertJUnit.assertEquals(2, lastChunkSize); AssertJUnit.assertEquals(8, chunkCount); AssertJUnit.assertEquals(4, generalChunkSize); AssertJUnit.assertTrue(eventArrived); siddhiAppRuntime.shutdown(); }
Example 16
Source File: CountPatternTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testQuery24() throws InterruptedException { log.info("testQuery24 - OUT 3"); SiddhiManager siddhiManager = new SiddhiManager(); String app = "" + "@app:playback\n" + "define stream LoginFailure (id string, user string, type string);\n" + "define stream LoginSuccess (id string, user string, type string);\n" + "\n" + " from every (e1=LoginFailure<3:> -> e2=LoginSuccess) \n" + " select e1[0].id as id, e2.user as user\n" + " insert into BreakIn\n" + ""; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(app); siddhiAppRuntime.addCallback("BreakIn", new StreamCallback() { @Override public void receive(Event[] events) { EventPrinter.print(events); if (events != null) { for (Event event : events) { inEventCount++; switch (inEventCount) { case 1: Assert.assertEquals(event.getData(0), "id_1"); Assert.assertEquals(event.getData(1), "hans"); break; case 2: Assert.assertEquals(event.getData(0), "id_8"); Assert.assertEquals(event.getData(1), "werner"); break; case 3: Assert.assertEquals(event.getData(0), "id_17"); Assert.assertEquals(event.getData(1), "hans"); break; } } eventArrived = true; } } }); InputHandler failureInput = siddhiAppRuntime.getInputHandler("LoginFailure"); InputHandler successInput = siddhiAppRuntime.getInputHandler("LoginSuccess"); siddhiAppRuntime.start(); long now = System.currentTimeMillis(); failureInput.send(++now, new Object[]{"id_1", "hans", "failure"}); failureInput.send(++now, new Object[]{"id_2", "hans", "failure"}); failureInput.send(++now, new Object[]{"id_3", "hans", "failure"}); failureInput.send(++now, new Object[]{"id_4", "hans", "failure"}); failureInput.send(++now, new Object[]{"id_5", "hans", "failure"}); failureInput.send(++now, new Object[]{"id_6", "hans", "failure"}); successInput.send(++now, new Object[]{"id_7", "hans", "success"}); successInput.send(++now, new Object[]{"id_7_1", "hans", "success"}); failureInput.send(++now, new Object[]{"id_8", "werner", "failure"}); failureInput.send(++now, new Object[]{"id_9", "werner", "failure"}); failureInput.send(++now, new Object[]{"id_10", "werner", "failure"}); failureInput.send(++now, new Object[]{"id_11", "werner", "failure"}); failureInput.send(++now, new Object[]{"id_12", "werner", "failure"}); failureInput.send(++now, new Object[]{"id_13", "werner", "failure"}); failureInput.send(++now, new Object[]{"id_14", "werner", "failure"}); failureInput.send(++now, new Object[]{"id_15", "werner", "failure"}); successInput.send(++now, new Object[]{"id_16", "werner", "success"}); now += 3 * 1000; //add 3 sec delay failureInput.send(++now, new Object[]{"id_17", "hans", "failure"}); failureInput.send(++now, new Object[]{"id_18", "hans", "failure"}); successInput.send(++now, new Object[]{"id_18_1", "hans", "success"}); failureInput.send(++now, new Object[]{"id_19", "hans", "failure"}); failureInput.send(++now, new Object[]{"id_20", "hans", "failure"}); failureInput.send(++now, new Object[]{"id_21", "hans", "failure"}); failureInput.send(++now, new Object[]{"id_22", "hans", "failure"}); successInput.send(++now, new Object[]{"id_23", "hans", "success"}); Assert.assertTrue(eventArrived); Assert.assertEquals(inEventCount, 3); siddhiAppRuntime.shutdown(); }
Example 17
Source File: JoinTestStoreTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testTableJoinQuery6() throws InterruptedException, SQLException { log.info("testTableJoinQuery6 - OUT 1"); SiddhiManager siddhiManager = new SiddhiManager(); String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string); " + "@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#window.length(1) join StockTable as s " + " on CheckStockStream.symbol==s.symbol " + "select CheckStockStream.symbol as checkSymbol, s.symbol as symbol, s.volume as volume " + "insert into OutputStream ;"; 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++; switch (inEventCount) { case 1: Assert.assertEquals(event.getData(), new Object[]{"WSO2", "WSO2", 100L}); break; default: Assert.assertSame(inEventCount, 1); } } eventArrived = true; } if (removeEvents != null) { removeEventCount = removeEventCount + removeEvents.length; } 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", 75.6f, 10L}); checkStockStream.send(new Object[]{"WSO2"}); Thread.sleep(1000); Assert.assertEquals(inEventCount, 1, "Number of success events"); Assert.assertEquals(removeEventCount, 0, "Number of remove events"); Assert.assertEquals(eventArrived, true, "Event arrived"); siddhiAppRuntime.shutdown(); }
Example 18
Source File: FaultStreamTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(dependsOnMethods = "faultStreamTest6") public void faultStreamTest7() throws InterruptedException { log.info("faultStreamTest7-Tests fault handling when it's set to log. " + "Events would be logged and dropped during publishing failure at Sink"); SiddhiManager siddhiManager = new SiddhiManager(); String siddhiApp = "" + "@OnError(action='stream')" + "define stream cseEventStream (symbol string, price float, volume long);" + "\n" + "@sink(type='inMemory', topic='{{symbol}}', on.error='log', @map(type='passThrough')) " + "define stream outputStream (symbol string, price float, sym1 string);" + "\n" + "@info(name = 'query1') " + "from cseEventStream " + "select symbol, price , symbol as sym1 " + "insert into outputStream ;" + ""; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("outputStream", new StreamCallback() { @Override public void receive(Event[] events) { EventPrinter.print(events); Assert.assertTrue(events[0].getData(0) != null); } }); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream"); siddhiAppRuntime.start(); Logger logger = Logger.getLogger(Sink.class); UnitTestAppender appender = new UnitTestAppender(); logger.addAppender(appender); try { inputHandler.send(new Object[]{"IBM", 0f, 100L}); AssertJUnit.assertTrue(appender.getMessages().contains("Dropping event at Sink 'inMemory' at")); } catch (Exception e) { Assert.fail("Unexpected exception occurred when testing.", e); } finally { logger.removeAppender(appender); siddhiAppRuntime.shutdown(); } }
Example 19
Source File: InsertIntoTableTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void insertIntoTableTest9() throws InterruptedException { log.info("InsertIntoTableTest9"); SiddhiManager siddhiManager = new SiddhiManager(); String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream StockCheckStream (symbol string); " + "define table StockTable (symbol string, price float, volume long); "; String query = "" + "@info(name = 'query2') " + "from StockCheckStream " + "select StockCheckStream.symbol != StockTable.symbol in StockTable as abc " + "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++; switch (inEventCount) { case 1: AssertJUnit.assertArrayEquals(new Object[]{false}, event.getData()); break; default: AssertJUnit.fail(); } } eventArrived = true; } if (removeEvents != null) { removeEventCount = removeEventCount + removeEvents.length; } eventArrived = true; } }); InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream"); InputHandler stockCheckStream = siddhiAppRuntime.getInputHandler("StockCheckStream"); siddhiAppRuntime.start(); stockCheckStream.send(new Object[]{"WSO2"}); Thread.sleep(500); AssertJUnit.assertEquals("Number of success events", 1, inEventCount); AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount); AssertJUnit.assertEquals("Event arrived", true, eventArrived); siddhiAppRuntime.shutdown(); }
Example 20
Source File: ExternalTimeBatchWindowTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void testExternalTimeBatchWindow7() throws InterruptedException { log.info("ExternalTimeBatchWindow test7"); SiddhiManager siddhiManager = new SiddhiManager(); String cseEventStream = "" + "define stream LoginEvents (timestamp long, ip string); " + "define window LoginWindow (timestamp long, ip string) externalTimeBatch(timestamp, 1 sec, 0, 2 sec) " + "output all events; "; String query = "" + "@info(name = 'query0') " + "from LoginEvents " + "insert into LoginWindow; " + "" + "@info(name = 'query1') " + "from LoginWindow " + "select timestamp, ip, count() as total " + "insert all events into uniqueIps ;"; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query); siddhiAppRuntime.addCallback("query1", new QueryCallback() { @Override public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timeStamp, inEvents, removeEvents); if (inEvents != null) { inEventCount = inEventCount + inEvents.length; } if (removeEvents != null) { removeEventCount = removeEventCount + removeEvents.length; } eventArrived = true; } }); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{1366335804341L, "192.10.1.3"}); inputHandler.send(new Object[]{1366335804599L, "192.10.1.4"}); inputHandler.send(new Object[]{1366335804600L, "192.10.1.5"}); inputHandler.send(new Object[]{1366335804607L, "192.10.1.6"}); inputHandler.send(new Object[]{1366335805599L, "192.10.1.4"}); inputHandler.send(new Object[]{1366335805600L, "192.10.1.5"}); inputHandler.send(new Object[]{1366335805607L, "192.10.1.6"}); Thread.sleep(3000); inputHandler.send(new Object[]{1366335805606L, "192.10.1.7"}); inputHandler.send(new Object[]{1366335805605L, "192.10.1.8"}); Thread.sleep(3000); inputHandler.send(new Object[]{1366335806606L, "192.10.1.9"}); inputHandler.send(new Object[]{1366335806690L, "192.10.1.10"}); Thread.sleep(3000); assertEquals("Event arrived", true, eventArrived); assertEquals("In Events ", 4, inEventCount); assertEquals("Remove Events ", 0, removeEventCount); siddhiAppRuntime.shutdown(); }