Search in sources :

Example 26 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class TestDebugger method testDebugger9.

@Test
public void testDebugger9() throws InterruptedException {
    log.info("Siddi Debugger Test 9: Test state traversal in a simple query");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "@config(async = 'true') define stream cseEventStream (symbol string, price float, " + "volume int);";
    final String query = "@info(name = 'query1')" + "from cseEventStream#window.length(3) " + "select symbol, price, sum(volume) as 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 == 2) {
                Map<String, Object> queryState = debugger.getQueryState(queryName);
                log.info(queryState);
                log.info(queryState.values().toArray()[0]);
                StreamEvent streamEvent = null;
                // Order of the query state items is unpredictable
                for (Map.Entry<String, Object> entry : queryState.entrySet()) {
                    if (entry.getKey().startsWith("AbstractStreamProcessor")) {
                        streamEvent = (StreamEvent) ((Map<String, Object>) entry.getValue()).get("ExpiredEventChunk");
                        break;
                    }
                }
                AssertJUnit.assertArrayEquals(streamEvent.getOutputData(), new Object[] { "WSO2", 50.0f, null });
            }
            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", 4, debugEventCount.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) 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) Map(java.util.Map) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 27 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class PlaybackTestCase method playbackTest4.

@Test
public void playbackTest4() throws InterruptedException {
    log.info("Playback Test 4: Playback with query joining two windows");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "@app:playback(idle.time = '100 millisecond', increment = '1 sec') " + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream twitterStream (user string, tweet string, company string); ";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.timeBatch(1 sec) join twitterStream#window.timeBatch(1 sec) " + "on cseEventStream.symbol== twitterStream.company " + "select cseEventStream.symbol as symbol, twitterStream.tweet, cseEventStream.price " + "insert 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 += (inEvents.length);
                }
                if (removeEvents != null) {
                    removeEventCount += (removeEvents.length);
                }
                eventArrived = true;
            }
        });
        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
        siddhiAppRuntime.start();
        long currentTime = System.currentTimeMillis();
        cseEventStreamHandler.send(currentTime, new Object[] { "WSO2", 55.6f, 100 });
        twitterStreamHandler.send(currentTime, new Object[] { "User1", "Hello World", "WSO2" });
        cseEventStreamHandler.send(currentTime, new Object[] { "IBM", 75.6f, 100 });
        currentTime += 1500;
        cseEventStreamHandler.send(currentTime, new Object[] { "WSO2", 57.6f, 100 });
        // Anything more than 100 is enough. Used 200 to be on safe side
        Thread.sleep(200);
        AssertJUnit.assertTrue("In Events can be 1 or 2 ", inEventCount == 1 || inEventCount == 2);
        AssertJUnit.assertEquals(0, removeEventCount);
        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)

Example 28 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class PlaybackTestCase method playbackTest2.

@Test
public void playbackTest2() throws InterruptedException {
    log.info("Playback Test 2: Playback with heartbeat disabled in query with start time enabled 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(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 });
    timestamp += 5000;
    inputHandler.send(timestamp, new Object[] { "ZZ", 0.0f, 0 });
    Thread.sleep(100);
    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 29 with In

use of org.wso2.siddhi.query.api.expression.condition.In 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 30 with In

use of org.wso2.siddhi.query.api.expression.condition.In 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)

Aggregations

Test (org.testng.annotations.Test)281 Event (org.wso2.siddhi.core.event.Event)187 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)186 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)186 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)182 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)160 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)153 ArrayList (java.util.ArrayList)148 HashMap (java.util.HashMap)118 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)103 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)81 IOException (java.io.IOException)57 Map (java.util.Map)54 API (org.wso2.carbon.apimgt.core.models.API)52 SQLException (java.sql.SQLException)48 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)46 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)39 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)37 File (java.io.File)35 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)34