Search in sources :

Example 91 with OutputStream

use of org.wso2.siddhi.query.api.execution.query.output.stream.OutputStream in project siddhi by wso2.

the class PlaybackTestCase method playbackTest3.

@Test
public void playbackTest3() throws InterruptedException {
    log.info("Playback Test 3: Playback with heartbeat enabled");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "@app:playback(idle.time = '100 millisecond', increment = '2 sec') " + "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.timeBatch(2 sec , 0) " + "select symbol, sum(price) as sumPrice, volume " + "insert 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);
            if (inEventCount == 0) {
                AssertJUnit.assertTrue("Remove Events will only arrive after the second time period. ", removeEvents == null);
            }
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            } else if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    // Start sending events in the beginning of a cycle
    long timestamp = 0;
    inputHandler.send(timestamp, new Object[] { "IBM", 700f, 0 });
    inputHandler.send(timestamp, new Object[] { "WSO2", 60.5f, 1 });
    timestamp += 8500;
    inputHandler.send(timestamp, new Object[] { "WSO2", 60.5f, 1 });
    inputHandler.send(timestamp, new Object[] { "II", 60.5f, 1 });
    timestamp += 13000;
    inputHandler.send(timestamp, new Object[] { "TT", 60.5f, 1 });
    inputHandler.send(timestamp, new Object[] { "YY", 60.5f, 1 });
    // Anything more than 100 is enough. Used 200 to be on safe side
    Thread.sleep(200);
    AssertJUnit.assertEquals(3, inEventCount);
    AssertJUnit.assertEquals(0, removeEventCount);
    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) Test(org.testng.annotations.Test)

Example 92 with OutputStream

use of org.wso2.siddhi.query.api.execution.query.output.stream.OutputStream in project siddhi by wso2.

the class PlaybackTestCase method playbackTest7.

@Test
public void playbackTest7() throws InterruptedException {
    log.info("Playback Test 7: Testing playback without heartbeat");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "@app:playback " + "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.time(2 sec) " + "select symbol,price,volume " + "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);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                AssertJUnit.assertTrue("InEvents arrived before RemoveEvents", inEventCount > removeEventCount);
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    long timestamp = System.currentTimeMillis();
    inputHandler.send(timestamp, new Object[] { "IBM", 700f, 0 });
    inputHandler.send(timestamp, new Object[] { "WSO2", 60.5f, 1 });
    timestamp += 2000;
    inputHandler.send(timestamp, new Object[] { "GOOGLE", 0.0f, 1 });
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, inEventCount);
    AssertJUnit.assertEquals(2, removeEventCount);
    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) Test(org.testng.annotations.Test)

Example 93 with OutputStream

use of org.wso2.siddhi.query.api.execution.query.output.stream.OutputStream in project siddhi by wso2.

the class PlaybackTestCase method playbackTest1.

@Test
public void playbackTest1() throws InterruptedException {
    log.info("Playback Test 1: Playback with heartbeat disabled in query containing regular time batch window");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "@app:playback " + "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.timeBatch(1 sec) " + "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);
            if (inEventCount == 0) {
                AssertJUnit.assertTrue("Remove Events will only arrive after the second time period. ", removeEvents == null);
            }
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    long timestamp = System.currentTimeMillis();
    inputHandler.send(timestamp, new Object[] { "IBM", 700f, 0 });
    timestamp += 500;
    inputHandler.send(timestamp, new Object[] { "WSO2", 60.5f, 1 });
    // 1 sec passed
    timestamp += 500;
    inputHandler.send(timestamp, new Object[] { "GOOGLE", 85.0f, 1 });
    // Another 1 sec passed
    timestamp += 1000;
    inputHandler.send(timestamp, new Object[] { "ORACLE", 90.5f, 1 });
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, inEventCount);
    AssertJUnit.assertEquals(2, removeEventCount);
    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) Test(org.testng.annotations.Test)

Example 94 with OutputStream

use of org.wso2.siddhi.query.api.execution.query.output.stream.OutputStream in project siddhi by wso2.

the class QuerySyncTestCase method querySyncTest1.

@Test
public void querySyncTest1() throws InterruptedException {
    log.info("querySync test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" + "@info(name = 'query1') " + "@synchronized('true') " + "from cseEventStream#window.time(2 sec) " + "select symbol,price,volume " + "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);
            if (inEvents != null) {
                inEventCount.addAndGet(inEvents.length);
            }
            if (removeEvents != null) {
                AssertJUnit.assertTrue("InEvents arrived before RemoveEvents", inEventCount.get() > removeEventCount.get());
                removeEventCount.addAndGet(removeEvents.length);
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 0 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 1 });
    Thread.sleep(4000);
    AssertJUnit.assertEquals(2, inEventCount.get());
    AssertJUnit.assertEquals(2, removeEventCount.get());
    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) Test(org.testng.annotations.Test)

Example 95 with OutputStream

use of org.wso2.siddhi.query.api.execution.query.output.stream.OutputStream in project siddhi by wso2.

the class QuerySyncTestCase method querySyncTest3.

@Test
public void querySyncTest3() throws InterruptedException {
    log.info("querySync test3");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream twitterStream (user string, tweet string, company string); ";
    String query = "" + "@info(name = 'query1') " + "@synchronized('true') " + "from cseEventStream#window.time(1 sec) as a join twitterStream#window.time(1 sec) as b " + "on a.symbol== b.company " + "select a.symbol as symbol, b.tweet, a.price " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    inEventCount.addAndGet(inEvents.length);
                }
                if (removeEvents != null) {
                    removeEventCount.addAndGet(removeEvents.length);
                }
                eventArrived = true;
            }
        });
        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
        siddhiAppRuntime.start();
        cseEventStreamHandler.send(new Object[] { "WSO2", 55.6f, 100 });
        twitterStreamHandler.send(new Object[] { "User1", "Hello World", "WSO2" });
        cseEventStreamHandler.send(new Object[] { "IBM", 75.6f, 100 });
        Thread.sleep(500);
        cseEventStreamHandler.send(new Object[] { "WSO2", 57.6f, 100 });
        SiddhiTestHelper.waitForEvents(100, 2, inEventCount, 60000);
        SiddhiTestHelper.waitForEvents(100, 2, removeEventCount, 60000);
        AssertJUnit.assertEquals(2, inEventCount.get());
        AssertJUnit.assertEquals(2, removeEventCount.get());
        AssertJUnit.assertTrue(eventArrived);
    } finally {
        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) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)845 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)841 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)818 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)777 Event (org.wso2.siddhi.core.event.Event)473 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)377 TestUtil (org.wso2.siddhi.core.TestUtil)300 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)100 Query (org.wso2.siddhi.query.api.execution.query.Query)80 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)67 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)65 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IOException (java.io.IOException)14 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)11 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)11 RestVariable (org.wso2.carbon.bpmn.rest.engine.variable.RestVariable)10 Response (javax.ws.rs.core.Response)9 DataResponse (org.wso2.carbon.bpmn.rest.model.common.DataResponse)8 ObjectOutputStream (java.io.ObjectOutputStream)7 OutputStream (java.io.OutputStream)6