Search in sources :

Example 86 with StreamCallback

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

the class LengthBatchWindowTestCase method lengthBatchWindowTest6.

@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) {
                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 });
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(2, inEventCount);
    AssertJUnit.assertTrue(eventArrived);
}
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)

Example 87 with StreamCallback

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

the class LengthBatchWindowTestCase method lengthBatchWindowTest5.

@Test
public void lengthBatchWindowTest5() throws InterruptedException {
    final int length = 2;
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.lengthBatch(" + length + ") " + "select symbol,price,volume " + "insert expired 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) {
                count++;
                AssertJUnit.assertEquals("Remove event order", count, event.getData(2));
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 1 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
    inputHandler.send(new Object[] { "IBM", 700f, 3 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
    inputHandler.send(new Object[] { "IBM", 700f, 5 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
    Thread.sleep(500);
    AssertJUnit.assertEquals("Remove event count", 4, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.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)

Example 88 with StreamCallback

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

the class LengthBatchWindowTestCase method lengthBatchWindowTest2.

@Test
public void lengthBatchWindowTest2() throws InterruptedException {
    log.info("Testing length batch window with no of events greater than window size");
    final int length = 4;
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.lengthBatch(" + length + ") " + "select symbol,price,volume " + "insert 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) {
                count++;
                AssertJUnit.assertEquals("In event order", count, event.getData(2));
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 1 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
    inputHandler.send(new Object[] { "IBM", 700f, 3 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
    inputHandler.send(new Object[] { "IBM", 700f, 5 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
    Thread.sleep(500);
    AssertJUnit.assertEquals("Total event count", 4, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.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)

Example 89 with StreamCallback

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

the class CallbackTestCase method callbackTest1.

@Test
public void callbackTest1() throws InterruptedException {
    log.info("callback test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('callbackTest1') " + "" + "define stream StockStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from StockStream[70 > price] " + "select symbol, price " + "insert into outputStream;";
    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 = count + inEvents.length;
            eventArrived = true;
        }
    });
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            count = count + events.length;
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 100L });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 200L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(2, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.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) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 90 with StreamCallback

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

the class ExceptionHandlerTestCase method createTestExecutionRuntime.

private SiddhiAppRuntime createTestExecutionRuntime() {
    siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('callbackTest1') " + "" + "@async(buffer.size='2')" + "define stream StockStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "@Parallel " + "from StockStream[price + 0.0 > 0.0] " + "select symbol, price " + "insert into outputStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });
    return siddhiAppRuntime;
}
Also used : 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)

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