Search in sources :

Example 36 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class TestDebugger method testDebugger7.

@Test
public void testDebugger7() throws InterruptedException {
    log.info("Siddi Debugger Test 7: Test play traversal in a query with time batch window");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "@info(name = 'query1')" + "from cseEventStream#window.timeBatch(3 sec) " + "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 + "\t" + System.currentTimeMillis());
            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(3500);
    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(io.siddhi.core.stream.input.InputHandler) StreamCallback(io.siddhi.core.stream.output.StreamCallback) ComplexEvent(io.siddhi.core.event.ComplexEvent) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) ComplexEvent(io.siddhi.core.event.ComplexEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 37 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class TestDebugger method testDebugger4.

@Test
public void testDebugger4() throws InterruptedException {
    log.info("Siddi Debugger Test 4: Test next traversal in a query with time batch window where next call delays" + " 1 sec");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    final String query = "@info(name = 'query1')" + "from cseEventStream#window.timeBatch(1 sec) " + "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);
            AssertJUnit.assertEquals("Cannot emit all three in one time", 1, 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(event);
            int count = debugEventCount.addAndGet(getCount(event));
            if (count != 1 && queryTerminal == SiddhiDebugger.QueryTerminal.IN) {
                try {
                    Thread.sleep(1100);
                } catch (InterruptedException e) {
                }
            }
            // next call will not reach OUT since there is a window
            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(1500);
    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(io.siddhi.core.stream.input.InputHandler) StreamCallback(io.siddhi.core.stream.output.StreamCallback) ComplexEvent(io.siddhi.core.event.ComplexEvent) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) ComplexEvent(io.siddhi.core.event.ComplexEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 38 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class AsyncTestCase method asyncTest6.

@Test(dependsOnMethods = { "asyncTest5" })
public void asyncTest6() throws InterruptedException {
    log.info("async test 6");
    HashMap<String, Integer> threads = new HashMap<>();
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + " " + "@async(buffer.size='16', workers='2', batch.size.max='25')" + "define stream cseEventStream (symbol string, price float, volume int);" + "" + "@info(name = 'query1') " + "from cseEventStream[70 < price] " + "select * " + "insert into innerStream ;" + "" + "@info(name = 'query2') " + "from innerStream[volume > 90] " + "select * " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            try {
                Thread.sleep(Math.round(Math.random()) * 1000 + 1000);
            } catch (InterruptedException e) {
                log.error(e.getMessage(), e);
            }
            eventArrived = true;
            for (Event event : events) {
                count.incrementAndGet();
            }
            Assert.assertTrue(events.length <= 25);
            synchronized (threads) {
                Integer count = threads.get(Thread.currentThread().getName());
                if (count == null) {
                    threads.put(Thread.currentThread().getName(), 1);
                } else {
                    count++;
                    threads.put(Thread.currentThread().getName(), count);
                }
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    for (int i = 0; i < 20; i++) {
        Thread.sleep(100);
        inputHandler.send(new Object[] { "WSO2", 115.6f, 100 + i });
    }
    SiddhiTestHelper.waitForEvents(2000, 20, count, 10000);
    AssertJUnit.assertEquals(20, count.get());
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(eventArrived);
    log.info("Threads count:" + threads.size() + " threads:" + threads);
    Assert.assertEquals(threads.size(), 2);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) HashMap(java.util.HashMap) StreamCallback(io.siddhi.core.stream.output.StreamCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 39 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class AsyncTestCase method asyncTest2.

@Test(expectedExceptions = SiddhiAppCreationException.class, dependsOnMethods = { "asyncTest1" })
public void asyncTest2() throws InterruptedException {
    log.info("async test 2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:async(buffer.size='2')" + " " + "define stream cseEventStream (symbol string, price float, volume int);" + "define stream cseEventStream2 (symbol string, price float, volume int);" + "" + "@info(name = 'query1') " + "from cseEventStream[70 > price] " + "select * " + "insert into innerStream ;" + "" + "@info(name = 'query2') " + "from innerStream[volume > 90] " + "select * " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
}
Also used : SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 40 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class AsyncTestCase method asyncTest3.

@Test(dependsOnMethods = { "asyncTest2" })
public void asyncTest3() throws InterruptedException {
    log.info("async test 3");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + " " + "@async(buffer.size='2')" + "define stream cseEventStream (symbol string, price float, volume int);" + "" + "define stream cseEventStream2 (symbol string, price float, volume int);" + "" + "@info(name = 'query1') " + "from cseEventStream[70 > price] " + "select * " + "insert into innerStream ;" + "" + "@info(name = 'query2') " + "from innerStream[volume > 90] " + "select * " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            try {
                Thread.sleep(Math.round(Math.random()) * 1000 + 1000);
            } catch (InterruptedException e) {
                log.error(e.getMessage(), e);
            }
            eventArrived = true;
            for (Event event : events) {
                count.incrementAndGet();
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    long startTime = System.currentTimeMillis();
    inputHandler.send(new Object[] { "WSO2", 55.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 9.6f, 100 });
    inputHandler.send(new Object[] { "FB", 7.6f, 100 });
    inputHandler.send(new Object[] { "GOOG", 5.6f, 100 });
    inputHandler.send(new Object[] { "WSO2", 15.6f, 100 });
    long timeDiff = System.currentTimeMillis() - startTime;
    Thread.sleep(5000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(5, count.get());
    AssertJUnit.assertTrue(timeDiff >= 2000);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1761 SiddhiManager (io.siddhi.core.SiddhiManager)1756 Test (org.testng.annotations.Test)1744 InputHandler (io.siddhi.core.stream.input.InputHandler)1590 Event (io.siddhi.core.event.Event)1257 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)823 StreamCallback (io.siddhi.core.stream.output.StreamCallback)305 TestUtil (io.siddhi.core.TestUtil)304 SiddhiApp (io.siddhi.query.api.SiddhiApp)90 Query (io.siddhi.query.api.execution.query.Query)85 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)84 ArrayList (java.util.ArrayList)73 Logger (org.apache.logging.log4j.core.Logger)57 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)34 InMemoryBroker (io.siddhi.core.util.transport.InMemoryBroker)32 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)30 HashMap (java.util.HashMap)25 UnitTestAppender (io.siddhi.core.UnitTestAppender)23 InMemoryConfigManager (io.siddhi.core.util.config.InMemoryConfigManager)21 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)15