Search in sources :

Example 66 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class TestDebugger method testDebugger2.

@Test
public void testDebugger2() throws InterruptedException {
    log.info("Siddi Debugger Test 2: Test next traversal in a query with length batch window");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "@config(async = 'true') define stream cseEventStream (symbol string, price float, " + "volume int);";
    String query = "@info(name = 'query1')" + "from cseEventStream#window.lengthBatch(3) " + "select symbol, price, volume " + "insert into OutputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            inEventCount.addAndGet(events.length);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
    siddhiDebugger.acquireBreakPoint("query1", SiddhiDebugger.QueryTerminal.IN);
    siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {

        @Override
        public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
            log.info("Query: " + queryName + ":" + queryTerminal);
            log.info(event);
            int count = debugEventCount.addAndGet(getCount(event));
            if (count == 1) {
                AssertJUnit.assertEquals("Incorrect break point", "query1IN", queryName + queryTerminal);
                AssertJUnit.assertArrayEquals("Incorrect debug event received at IN", new Object[] { "WSO2", 50f, 60 }, event.getOutputData());
            } else if (count == 2) {
                AssertJUnit.assertEquals("Incorrect break point", "query1IN", queryName + queryTerminal);
                AssertJUnit.assertArrayEquals("Incorrect debug event received at IN", new Object[] { "WSO2", 70f, 40 }, event.getOutputData());
            } else if (count == 3) {
                AssertJUnit.assertEquals("Incorrect break point", "query1IN", queryName + queryTerminal);
                AssertJUnit.assertArrayEquals("Incorrect debug event received at IN", new Object[] { "WSO2", 60f, 50 }, event.getOutputData());
            } else if (count == 4) {
                AssertJUnit.assertEquals("Incorrect break point", "query1OUT", queryName + queryTerminal);
                AssertJUnit.assertEquals("Incorrect number of events received", 3, getCount(event));
            }
            debugger.next();
        }
    });
    inputHandler.send(new Object[] { "WSO2", 50f, 60 });
    inputHandler.send(new Object[] { "WSO2", 70f, 40 });
    inputHandler.send(new Object[] { "WSO2", 60f, 50 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Invalid number of output events", 3, inEventCount.get());
    AssertJUnit.assertEquals("Invalid number of debug events", 6, debugEventCount.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 67 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class TestDebugger method testDebugger6.

@Test
public void testDebugger6() throws InterruptedException {
    log.info("Siddi Debugger Test 6: Test play traversal in a query with length batch window");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "@config(async = 'true') define stream cseEventStream (symbol string, price float, " + "volume int);";
    String query = "@info(name = 'query1')" + "from cseEventStream#window.lengthBatch(3) " + "select symbol, price, volume " + "insert into OutputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            inEventCount.addAndGet(events.length);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
    siddhiDebugger.acquireBreakPoint("query1", SiddhiDebugger.QueryTerminal.IN);
    siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {

        @Override
        public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
            log.info("Query: " + queryName + ":" + queryTerminal);
            log.info(event);
            int count = debugEventCount.addAndGet(getCount(event));
            if (count == 1) {
                AssertJUnit.assertEquals("Incorrect break point", "query1IN", queryName + queryTerminal);
                AssertJUnit.assertArrayEquals("Incorrect debug event received at IN", new Object[] { "WSO2", 50f, 60 }, event.getOutputData());
            } else if (count == 2) {
                AssertJUnit.assertEquals("Incorrect break point", "query1IN", queryName + queryTerminal);
                AssertJUnit.assertArrayEquals("Incorrect debug event received at IN", new Object[] { "WSO2", 70f, 40 }, event.getOutputData());
            } else if (count == 3) {
                AssertJUnit.assertEquals("Incorrect break point", "query1IN", queryName + queryTerminal);
                AssertJUnit.assertArrayEquals("Incorrect debug event received at IN", new Object[] { "WSO2", 60f, 50 }, event.getOutputData());
            }
            debugger.play();
        }
    });
    inputHandler.send(new Object[] { "WSO2", 50f, 60 });
    inputHandler.send(new Object[] { "WSO2", 70f, 40 });
    inputHandler.send(new Object[] { "WSO2", 60f, 50 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Invalid number of output events", 3, inEventCount.get());
    AssertJUnit.assertEquals("Invalid number of debug events", 3, debugEventCount.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 68 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class TestDebugger method testDebugger10.

@Test
public void testDebugger10() throws InterruptedException {
    log.info("Siddi Debugger Test 10: Test next traversal in a query with two consequent streams");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "@config(async = 'true') " + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream stockEventStream (symbol string, price float, volume int); ";
    final String query = "@info(name = 'query1')" + "from cseEventStream " + "select symbol, price, volume " + "insert into stockEventStream; " + "@info(name = 'query2')" + "from stockEventStream " + "select * " + "insert into OutputStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            inEventCount.addAndGet(events.length);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
    siddhiDebugger.acquireBreakPoint("query1", SiddhiDebugger.QueryTerminal.IN);
    siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {

        @Override
        public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
            log.info("Query: " + queryName + ":" + queryTerminal);
            log.info(event);
            int count = debugEventCount.addAndGet(getCount(event));
            if ((count - 1) / 4 == 0) {
                // First four events
                AssertJUnit.assertArrayEquals("Incorrect debug event received", new Object[] { "WSO2", 50f, 60 }, event.getOutputData());
            } else {
                // Next four events
                AssertJUnit.assertArrayEquals("Incorrect debug event received", new Object[] { "WSO2", 70f, 40 }, event.getOutputData());
            }
            if (count == 1 || count == 5) {
                AssertJUnit.assertEquals("Incorrect break point", "query1IN", queryName + queryTerminal);
            } else if (count == 2 || count == 6) {
                AssertJUnit.assertEquals("Incorrect break point", "query1OUT", queryName + queryTerminal);
            } else if (count == 3 || count == 7) {
                AssertJUnit.assertEquals("Incorrect break point", "query2IN", queryName + queryTerminal);
            } else {
                AssertJUnit.assertEquals("Incorrect break point", "query2OUT", queryName + queryTerminal);
            }
            debugger.next();
        }
    });
    inputHandler.send(new Object[] { "WSO2", 50f, 60 });
    inputHandler.send(new Object[] { "WSO2", 70f, 40 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Invalid number of output events", 2, inEventCount.get());
    AssertJUnit.assertEquals("Invalid number of debug events", 8, debugEventCount.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 69 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class TestDebugger method testDebugger11.

@Test
public void testDebugger11() throws InterruptedException {
    log.info("Siddi Debugger Test 11: Modify events during debug mode");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "@config(async = 'true') " + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream stockEventStream (symbol string, price float, volume int); ";
    final String query = "@info(name = 'query1')" + "from cseEventStream " + "select symbol, price, volume " + "insert into stockEventStream; " + "@info(name = 'query2')" + "from stockEventStream " + "select * " + "insert into OutputStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            inEventCount.addAndGet(events.length);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
    siddhiDebugger.acquireBreakPoint("query1", SiddhiDebugger.QueryTerminal.IN);
    siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {

        @Override
        public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
            log.info("Query: " + queryName + ":" + queryTerminal);
            log.info(event);
            int count = debugEventCount.addAndGet(getCount(event));
            if ((count - 1) / 2 == 0) {
                // WSO2 in stream 1
                AssertJUnit.assertArrayEquals("Incorrect debug event received", new Object[] { "WSO2", 50f, 60 }, event.getOutputData());
            } else {
                // IBM in stream 2
                AssertJUnit.assertArrayEquals("Incorrect debug event received", new Object[] { "IBM", 50f, 60 }, event.getOutputData());
            }
            if (count == 2) {
                // Modify the event at the end of the first stream
                event.getOutputData()[0] = "IBM";
            }
            debugger.next();
        }
    });
    inputHandler.send(new Object[] { "WSO2", 50f, 60 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Invalid number of output events", 1, inEventCount.get());
    AssertJUnit.assertEquals("Invalid number of debug events", 4, debugEventCount.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 70 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class WindowPartitionTestCase method testWindowPartitionQuery2.

@Test
public void testWindowPartitionQuery2() throws InterruptedException {
    log.info("Window Partition test2");
    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" + ".lengthBatch(2)  select symbol,sum(price) as price,volume insert all 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) {
                inEventCount++;
                eventArrived = true;
                if (inEventCount == 1) {
                    AssertJUnit.assertEquals(170.0, event.getData()[1]);
                } else if (inEventCount == 2) {
                    AssertJUnit.assertEquals(1700.0, event.getData()[1]);
                }
            }
        }
    });
    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[] { "WSO2", 1000f, 100 });
    Thread.sleep(2000);
    AssertJUnit.assertEquals(2, inEventCount);
    executionRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)218 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)213 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)213 Event (org.wso2.siddhi.core.event.Event)213 Test (org.testng.annotations.Test)207 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)202 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)17 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)14 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)6 Partition (org.wso2.siddhi.query.api.execution.partition.Partition)6 Query (org.wso2.siddhi.query.api.execution.query.Query)6 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)5 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)3 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)2 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)2