Java Code Examples for io.siddhi.core.SiddhiAppRuntime#getInputHandler()

The following examples show how to use io.siddhi.core.SiddhiAppRuntime#getInputHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery67() throws InterruptedException {
    log.info("Filter test67");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.DOUBLE).attribute("volume", Attribute.Type.LONG);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("price"),
            Compare.Operator.LESS_THAN_EQUAL, Expression.value(60d))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")));
    query.insertInto("outputStream");

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    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.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 50d, 60L});
    inputHandler.send(new Object[]{"WSO2", 70d, 40L});
    inputHandler.send(new Object[]{"WSO2", 44d, 200L});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    siddhiAppRuntime.shutdown();

}
 
Example 2
Source File: AbsentSequenceTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(dependsOnMethods = {"testQueryAbsent14"})
public void testQueryAbsent15() throws InterruptedException {
    log.info("Test the query not e1 for 1 sec, e2, e3 with e1, e2 e3");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream Stream1 (symbol string, price float, volume int); " +
            "define stream Stream2 (symbol string, price float, volume int); " +
            "define stream Stream3 (symbol string, price float, volume int); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from not Stream1[price>10] for 1 sec, e2=Stream2[price>20], e3=Stream3[price>30] " +
            "select e2.symbol as symbol2, e3.symbol as symbol3 " +
            "insert into OutputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    TestUtil.TestCallback callback = TestUtil.addQueryCallback(siddhiAppRuntime, "query1");

    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    InputHandler stream3 = siddhiAppRuntime.getInputHandler("Stream3");

    siddhiAppRuntime.start();

    stream1.send(new Object[]{"WSO2", 15.6f, 100});
    Thread.sleep(100);
    stream2.send(new Object[]{"IBM", 28.7f, 100});
    Thread.sleep(100);
    stream3.send(new Object[]{"GOOGLE", 55.7f, 100});

    TestUtil.waitForInEvents(100, callback, 5);
    callback.throwAssertionErrors();
    AssertJUnit.assertEquals("Number of success events", 0, callback.getInEventCount());
    AssertJUnit.assertEquals("Number of remove events", 0, callback.getRemoveEventCount());
    AssertJUnit.assertFalse("Event arrived", callback.isEventArrived());

    siddhiAppRuntime.shutdown();
}
 
Example 3
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery82() throws InterruptedException {
    log.info("Filter test82");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.DOUBLE).attribute("volume", Attribute.Type.DOUBLE);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("price"),
            Compare.Operator.LESS_THAN, Expression.value(50))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")));
    query.insertInto("outputStream");

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    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.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 50d, 60d});
    inputHandler.send(new Object[]{"WSO2", 70d, 40d});
    inputHandler.send(new Object[]{"WSO2", 44d, 200d});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();
}
 
Example 4
Source File: AbsentSequenceTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryAbsent1() throws InterruptedException {
    log.info("Test the query e1, not e2 without sending e2");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream Stream1 (symbol string, price float, volume int); " +
            "define stream Stream2 (symbol string, price float, volume int); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from e1=Stream1[price>20], not Stream2[price>e1.price] for 1 sec " +
            "select e1.symbol as symbol1 " +
            "insert into OutputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    TestUtil.TestCallback callback = TestUtil.addQueryCallback(siddhiAppRuntime, "query1", new Object[]{"WSO2"});

    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    siddhiAppRuntime.start();

    stream1.send(new Object[]{"WSO2", 55.6f, 100});

    TestUtil.waitForInEvents(500, callback, 10);
    callback.throwAssertionErrors();
    AssertJUnit.assertEquals("Number of success events", 1, callback.getInEventCount());
    AssertJUnit.assertEquals("Number of remove events", 0, callback.getRemoveEventCount());
    AssertJUnit.assertTrue("Event not arrived", callback.isEventArrived());

    siddhiAppRuntime.shutdown();
}
 
Example 5
Source File: SnapshotOutputRateLimitTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"testSnapshotOutputRateLimitQuery15"})
public void testSnapshotOutputRateLimitQuery16() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test16");

    SiddhiManager siddhiManager = new SiddhiManager();

    String siddhiApp = "" +
            "@app:name('SnapshotOutputRateLimitTest16') " +
            "" +
            "define stream LoginEvents (timestamp long, ip string, calls int);" +
            "" +
            "@info(name = 'query1') " +
            "from LoginEvents#window.time(1 sec) " +
            "select  ip " +
            "group by ip " +
            "output snapshot every 1 sec " +
            "insert all events into uniqueIps ;";


    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    log.info("Running : " + siddhiAppRuntime.getName());

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
            if (inEvents != null) {
                count.incrementAndGet();
                for (Event event : inEvents) {
                    AssertJUnit.assertTrue("192.10.1.5".equals(event.getData(0)) ||
                            "192.10.1.3".equals(event.getData(0)));
                }
                value += inEvents.length;
            }
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");

    siddhiAppRuntime.start();

    Thread.sleep(1100);
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.5", 3});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.3", 6});
    Thread.sleep(2200);
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.5", 2});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.3", 10});
    Thread.sleep(1200);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event bundles with inEvents", 2, count.get());
    AssertJUnit.assertEquals("Number of output event", 4, value);

    siddhiAppRuntime.shutdown();

}
 
Example 6
Source File: QueryAPITestCaseForTableWithCache.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void test2() throws InterruptedException {
    log.info("Test2 table with cache");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "@Store(type=\"testStoreDummyForCache\", @Cache(size=\"10\"))\n" +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");

    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
    stockStream.send(new Object[]{"IBM", 75.6f, 100L});
    stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
    Thread.sleep(500);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "on price > 75 " +
            "select symbol, volume ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    Object[] eventData = events[0].getData();
    AssertJUnit.assertEquals(2, events[0].getData().length);

    events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "select symbol, volume ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);
    AssertJUnit.assertEquals(2, events[0].getData().length);

    events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "on price > 5 " +
            "select symbol " +
            "group by symbol " +
            "having symbol == 'WSO2' ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);

    siddhiAppRuntime.shutdown();

}
 
Example 7
Source File: LogicalAbsentSequenceTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"testQueryAbsent60"})
public void testQueryAbsent61() throws InterruptedException {
    log.info("Test the query every (e1 and not e2 for 1 sec), e3 with e1, e3 and e2, e3");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream Stream1 (symbol string, price float, volume int); " +
            "define stream Stream2 (symbol string, price float, volume int); " +
            "define stream Stream3 (symbol string, price float, volume int); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from every (e2=Stream2[price>20] and not Stream1[price>10] for 1 sec), " +
            "e3=Stream3[price>30] " +
            "select e2.symbol as symbol2, e3.symbol as symbol3 " +
            "insert into OutputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    TestUtil.TestCallback callback = TestUtil.addQueryCallback(siddhiAppRuntime, "query1", new Object[]{"WSO2",
            "GOOGLE"});

    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    InputHandler stream3 = siddhiAppRuntime.getInputHandler("Stream3");

    siddhiAppRuntime.start();

    stream1.send(new Object[]{"ORACLE", 15.0f, 100});
    Thread.sleep(100);
    stream2.send(new Object[]{"MICROSOFT", 45.0f, 100});
    Thread.sleep(100);
    stream3.send(new Object[]{"IBM", 55.0f, 100});
    Thread.sleep(2000);
    stream2.send(new Object[]{"WSO2", 45.0f, 100});
    Thread.sleep(100);
    stream3.send(new Object[]{"GOOGLE", 55.0f, 100});

    TestUtil.waitForInEvents(100, callback, 5);
    callback.throwAssertionErrors();
    AssertJUnit.assertEquals("Number of success events", 1, callback.getInEventCount());
    AssertJUnit.assertEquals("Number of remove events", 0, callback.getRemoveEventCount());
    AssertJUnit.assertTrue("Event arrived", callback.isEventArrived());

    siddhiAppRuntime.shutdown();
}
 
Example 8
Source File: AbsentSequenceTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"testQueryAbsent28"})
public void testQueryAbsent29() throws InterruptedException {
    log.info("Test the query e1, not e2 for 1 sec, e3 and e4 without e2 within 1 sec");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream Stream1 (symbol string, price float, volume int); " +
            "define stream Stream2 (symbol string, price float, volume int); " +
            "define stream Stream3 (symbol string, price float, volume int); " +
            "define stream Stream4 (symbol string, price float, volume int); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from e1=Stream1[price>10], not Stream2[price>20] for 1 sec, e2=Stream3[price>30] and " +
            "e3=Stream4[price>40]" +
            "select e1.symbol as symbol1, e2.symbol as symbol2, e3.symbol as symbol3 " +
            "insert into OutputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    TestUtil.TestCallback callback = TestUtil.addQueryCallback(siddhiAppRuntime, "query1");

    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream3 = siddhiAppRuntime.getInputHandler("Stream3");
    InputHandler stream4 = siddhiAppRuntime.getInputHandler("Stream4");


    siddhiAppRuntime.start();

    stream1.send(new Object[]{"IBM", 18.7f, 100});
    Thread.sleep(100);
    stream3.send(new Object[]{"WSO2", 35.0f, 100});
    Thread.sleep(100);
    stream4.send(new Object[]{"GOOGLE", 56.86f, 100});

    TestUtil.waitForInEvents(100, callback, 5);
    callback.throwAssertionErrors();
    AssertJUnit.assertEquals("Number of success events", 0, callback.getInEventCount());
    AssertJUnit.assertEquals("Number of remove events", 0, callback.getRemoveEventCount());
    AssertJUnit.assertFalse("Event not arrived", callback.isEventArrived());

    siddhiAppRuntime.shutdown();
}
 
Example 9
Source File: SequenceTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testQuery31() throws InterruptedException {
    log.info("testSequence31 - OUT 0");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream Stream1 (symbol string, price float, volume int); " +
            "define stream Stream2 (symbol string, price float, volume int); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from e1=Stream1[price>20], e2=Stream2[price>e1.price] " +
            "select e1.symbol as symbol1, e2.symbol as symbol2 " +
            "insert into OutputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + 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;
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }

    });

    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");

    siddhiAppRuntime.start();

    stream1.send(new Object[]{"WSO2", 55.6f, 100});
    Thread.sleep(100);
    stream1.send(new Object[]{"GOOG", 57.6f, 100});
    Thread.sleep(100);
    stream2.send(new Object[]{"IBM", 65.7f, 100});
    Thread.sleep(100);

    AssertJUnit.assertEquals("Number of success events", 0, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", false, eventArrived);

    siddhiAppRuntime.shutdown();
}
 
Example 10
Source File: WithinPatternTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testQuery7() throws InterruptedException {
    log.info("testPatternWithin5 - OUT 1 : Within clause for grouped pattern states(3)");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream Stream1 (symbol string, price float, volume int); ";

    String query = "" +
            "@info(name = 'query1') " +
            "from every (e1=Stream1 -> e2=Stream1[symbol == e1.symbol] -> e3=Stream1[symbol == e2.symbol]) " +
            "within 5 sec " +
            " select e1.symbol as symbol1, e1.volume as volume1, e2.symbol as symbol2, e2.volume as volume2, " +
            " e3.symbol as symbol3, e3.volume as volume3" +
            " insert into OutputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + 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;
                AssertJUnit.assertArrayEquals(new Object[]{"WSO2", 150, "WSO2", 200, "WSO2", 250},
                        inEvents[0].getData());
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }

    });

    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");

    siddhiAppRuntime.start();

    stream1.send(new Object[]{"WSO2", 55.6f, 100});
    Thread.sleep(6000);
    stream1.send(new Object[]{"WSO2", 56.6f, 150});
    stream1.send(new Object[]{"WSO2", 57.7f, 200});
    Thread.sleep(500);
    stream1.send(new Object[]{"WSO2", 58.7f, 250});
    stream1.send(new Object[]{"WSO2", 57.7f, 300});
    stream1.send(new Object[]{"WSO2", 59.7f, 350});
    Thread.sleep(500);

    AssertJUnit.assertEquals("Number of success events", 1, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);

    siddhiAppRuntime.shutdown();
}
 
Example 11
Source File: CountPatternTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testQuery4() throws InterruptedException {
    log.info("testPatternCount4 - OUT 1");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream Stream1 (symbol string, price float, volume int); " +
            "define stream Stream2 (symbol string, price float, volume int); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from e1=Stream1[price>20] <2:5> -> e2=Stream2[price>20] " +
            "select e1[0].price as price1_0, e1[1].price as price1_1, e1[2].price as price1_2, " +
            "   e1[3].price as price1_3, e2.price as price2 " +
            "insert into OutputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            AssertJUnit.fail();
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }

    });

    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");

    siddhiAppRuntime.start();

    stream1.send(new Object[]{"WSO2", 25.6f, 100});
    Thread.sleep(100);
    stream2.send(new Object[]{"IBM", 45.7f, 100});
    Thread.sleep(100);

    AssertJUnit.assertEquals("Number of success events", 0, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", false, eventArrived);

    siddhiAppRuntime.shutdown();
}
 
Example 12
Source File: SequenceTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testQuery18() throws InterruptedException {
    log.info("testSequence18 - OUT 1");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream Stream1 (symbol string, price float, volume int); " +
            "define stream Stream2 (symbol string, price float, volume int); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from every e1=Stream1[price>20], " +
            "   e2=Stream1[((e2[last].price is null) and price>=e1.price) or ((not (e2[last].price is null)) and " +
            "price>=e2[last].price)]+, " +
            "   e3=Stream1[price<e2[last].price] " +
            "select e1.price as price1, e2[0].price as price2, e2[1].price as price3, e3.price as price4 " +
            "insert into OutputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch (inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[]{25.0f, 35.6f, 57.6f, 47.6f},
                                    event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }

    });

    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");

    siddhiAppRuntime.start();

    stream1.send(new Object[]{"WSO2", 29.6f, 100});
    Thread.sleep(100);
    stream1.send(new Object[]{"WSO2", 25.0f, 100});
    Thread.sleep(100);
    stream1.send(new Object[]{"WSO2", 35.6f, 100});
    Thread.sleep(100);
    stream1.send(new Object[]{"WSO2", 57.6f, 100});
    Thread.sleep(100);
    stream1.send(new Object[]{"IBM", 47.6f, 100});
    Thread.sleep(100);

    AssertJUnit.assertEquals("Number of success events", 1, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);

    siddhiAppRuntime.shutdown();
}
 
Example 13
Source File: ExpressionWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void expressionWindowTest11() throws InterruptedException {
    log.info("Testing expression window 11");

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int, expr string);";
    String query = "@info(name = 'query1') " +
            "from cseEventStream#window.expression(expr) " +
            "select symbol, price " +
            "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);
            inEventCount = inEventCount + inEvents.length;
            AssertJUnit.assertEquals(1, inEvents.length);
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
                AssertJUnit.assertTrue(1 == removeEvents.length || 3 == removeEvents.length);
            }
            eventArrived = true;
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    String expr = "eventTimestamp(last) - eventTimestamp(first) < 4";
    inputHandler.send(0, new Object[]{"WSO2", 60.5f, 0, expr});
    inputHandler.send(1, new Object[]{"WSO2", 61.5f, 1, expr});
    inputHandler.send(2, new Object[]{"WSO2", 62.5f, 2, expr});
    inputHandler.send(3, new Object[]{"WSO2", 63.5f, 3, expr});
    inputHandler.send(4, new Object[]{"WSO2", 64.5f, 4, expr});
    expr = "eventTimestamp(last) - eventTimestamp(first) < 2";
    inputHandler.send(5, new Object[]{"WSO2", 65.5f, 5, expr});
    inputHandler.send(6, new Object[]{"WSO2", 66.5f, 6, expr});
    inputHandler.send(7, new Object[]{"WSO2", 67.5f, 7, expr});
    inputHandler.send(8, new Object[]{"WSO2", 68.5f, 8, expr});

    AssertJUnit.assertEquals(9, inEventCount);
    AssertJUnit.assertEquals(7, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example 14
Source File: MinForeverAggregatorExtensionTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testMinForeverAggregatorExtension2() throws InterruptedException {

    log.info("minForeverAggregator TestCase 2");
    SiddhiManager siddhiManager = new SiddhiManager();

    String inStreamDefinition = "define stream inputStream (price1 int,price2 int, price3 int);";
    String query = ("@info(name = 'query1') from inputStream " +
            "select minForever(price1) as minForeverValue " +
            "insert into outputStream;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inStreamDefinition +
            query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {

            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
            for (Event event : inEvents) {
                count++;
                switch (count) {
                    case 1:
                        AssertJUnit.assertEquals(36, event.getData(0));
                        break;
                    case 2:
                        AssertJUnit.assertEquals(36, event.getData(0));
                        break;
                    case 3:
                        AssertJUnit.assertEquals(9, event.getData(0));
                        break;
                    default:
                        org.testng.AssertJUnit.fail();
                }
            }
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("inputStream");
    siddhiAppRuntime.start();

    inputHandler.send(new Object[]{36, 38, 74});
    inputHandler.send(new Object[]{78, 38, 37});
    inputHandler.send(new Object[]{9, 39, 38});

    Thread.sleep(300);
    AssertJUnit.assertEquals(3, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();

}
 
Example 15
Source File: DelayWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(description = "Check if events are persisted when using delay window")
public void delayWindowTest7() throws InterruptedException {
    log.info("DelayWindow Test7: Testing persistence ");

    PersistenceStore persistenceStore = new InMemoryPersistenceStore();

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);

    String cseEventStream = "" +
            "define window delayWindow(symbol string, volume int) delay(1450);" +
            "define stream inputStream(symbol string, volume int);" +
            "define window timeWindow(symbol string) time(2 sec);";

    String query = "" +
            "@info(name='query1') " +
            "from inputStream " +
            "select symbol, volume " +
            "insert into delayWindow; " +
            "" +
            "@info(name = 'query2') " +
            "from delayWindow " +
            "select symbol, sum(volume) as totalVolume " +
            "insert into analyzeStream; " +
            "" +
            "@info(name='query3') " +
            "from inputStream " +
            "select symbol " +
            "insert into timeWindow; " +
            "" +
            "@info(name='query4') " +
            "from analyzeStream join timeWindow " +
            "on analyzeStream.symbol == timeWindow.symbol " +
            "select analyzeStream.symbol, analyzeStream.totalVolume " +
            "insert into outputStream; ";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);

    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
            for (Event event : events) {
                AssertJUnit.assertTrue(("IBM".equals(event.getData(0)) || "WSO2".equals(event.getData(0))));
            }
            lastValue = (Long) events[0].getData(1);
        }
    });

    InputHandler input = siddhiAppRuntime.getInputHandler("inputStream");
    siddhiAppRuntime.start();

    input.send(new Object[]{"IBM", 700});
    input.send(new Object[]{"WSO2", 750});

    SiddhiTestHelper.waitForEvents(100, 2, count, 4000);

    siddhiAppRuntime.persist();
    siddhiAppRuntime.shutdown();

    input = siddhiAppRuntime.getInputHandler("inputStream");
    siddhiAppRuntime.start();

    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }

    input.send(new Object[]{"WSO2", 600});
    SiddhiTestHelper.waitForEvents(100, 3, count, 4000);

    AssertJUnit.assertEquals(Long.valueOf(2050), lastValue);
    siddhiAppRuntime.shutdown();
}
 
Example 16
Source File: TimeBatchWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void timeWindowBatchTest6() throws InterruptedException {
    log.info("timeWindowBatch Test6");

    SiddhiManager siddhiManager = new SiddhiManager();


    String streams = "" +
            "define stream cseEventStream (symbol string, price float, volume int); " +
            "define stream twitterStream (user string, tweet string, company string); " +
            "define window cseEventWindow (symbol string, price float, volume int) timeBatch(1 sec) output " +
            "current events; " +
            "define window twitterWindow (user string, tweet string, company string) timeBatch(1 sec) output " +
            "current events; ";

    String query = "" +
            "@info(name = 'query0') " +
            "from cseEventStream " +
            "insert into cseEventWindow; " +
            "" +
            "@info(name = 'query1') " +
            "from twitterStream " +
            "insert into twitterWindow; " +
            "" +
            "@info(name = 'query2') " +
            "from cseEventWindow join twitterWindow " +
            "on cseEventWindow.symbol== twitterWindow.company " +
            "select cseEventWindow.symbol as symbol, twitterWindow.tweet, cseEventWindow.price " +
            "insert into outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query2", 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();
        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(1500);
        cseEventStreamHandler.send(new Object[]{"WSO2", 57.6f, 100});
        Thread.sleep(1000);
        AssertJUnit.assertTrue("In Events can be 1 or 2 ", inEventCount == 1 || inEventCount == 2);
        AssertJUnit.assertEquals(0, removeEventCount);
        AssertJUnit.assertTrue(eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
 
Example 17
Source File: Aggregation1TestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"incrementalStreamProcessorTest43"})
public void incrementalStreamProcessorTest44() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest44");
    SiddhiManager siddhiManager = new SiddhiManager();

    String stockStream =
            "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " +
                    "quantity int, timestamp long);";
    String query =
            "define aggregation stockAggregation " +
                    "from stockStream " +
                    "select avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) as " +
                    "lastTradeValue, " +
                    "count() as count " +
                    "aggregate by timestamp every sec...year ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);

    try {
        InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
        siddhiAppRuntime.start();

        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[]{"WSO2", 50f, 60f, 90L, 6, 1496289950000L});
        stockStreamInputHandler.send(new Object[]{"WSO2", 70f, null, 40L, 10, 1496289950000L});

        // Thursday, June 1, 2017 4:05:51 AM
        stockStreamInputHandler.send(new Object[]{"WSO2", 60f, 44f, 200L, 56, 1496289951000L});
        stockStreamInputHandler.send(new Object[]{"WSO2", 100f, null, 200L, 16, 1496289951011L});

        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[]{"IBM", 400f, null, 200L, 9, 1496289952000L});

        // Thursday, June 1, 2017 4:05:49 AM
        stockStreamInputHandler.send(new Object[]{"IBM", 900f, null, 200L, 60, 1496289949000L});
        stockStreamInputHandler.send(new Object[]{"IBM", 500f, null, 200L, 7, 1496289949000L});

        // Thursday, June 1, 2017 4:05:53 AM
        stockStreamInputHandler.send(new Object[]{"IBM", 100f, null, 200L, 26, 1496289953000L});
        stockStreamInputHandler.send(new Object[]{"IBM", 100f, null, 200L, 96, 1496289953000L});

        Thread.sleep(2000);


        siddhiAppRuntime.query("from stockAggregation within 1496289949000L, 1496289950000L per " +
                "'days' select AGG_TIMESTAMP, avgPrice");

        siddhiAppRuntime.query("from stockAggregation within 1496289949000L, 1496289950000L per " +
                "'days' select AGG_TIMESTAMP, avgPrice");

    } finally {
        siddhiAppRuntime.shutdown();
    }
}
 
Example 18
Source File: EventOutputRateLimitTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testEventOutputRateLimitQuery14() throws InterruptedException {
    log.info("EventOutputRateLimit test14");

    SiddhiManager siddhiManager = new SiddhiManager();

    String siddhiApp = "" +
            "@app:name('EventOutputRateLimitTest14') " +
            "" +
            "define stream LoginEvents (timestamp long, ip string);" +
            "" +
            "@info(name = 'query1') " +
            "from LoginEvents#window.lengthBatch(4) " +
            "select  ip , count() as total " +
            "output last every 2 events " +
            "insert expired events into uniqueIps ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    log.info("Running : " + siddhiAppRuntime.getName());

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (removeEvents != null) {
                count += removeEvents.length;
            } else {
                AssertJUnit.fail("InEvents emitted");
            }
            eventArrived = true;
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");

    siddhiAppRuntime.start();

    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.5"});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.3"});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.3"});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.9"});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.3"});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.4"});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.4"});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.4"});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.30"});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.31"});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.32"});
    inputHandler.send(new Object[]{System.currentTimeMillis(), "192.10.1.33"});
    Thread.sleep(1000);

    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event value", 1, count);

    siddhiAppRuntime.shutdown();
}
 
Example 19
Source File: CountPatternTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testQuery11() throws InterruptedException {
    log.info("testPatternCount11 - OUT 1");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream EventStream (symbol string, price float, volume int); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from e1 = EventStream [price >= 50 and volume > 100] -> e2 = EventStream [price <= 40] <:5> " +
            "   -> e3 = EventStream [volume <= 70] " +
            "select e1.symbol as symbol1, e2[last].symbol as symbol2, e3.symbol as symbol3 " +
            "insert into StockQuote;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch (inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[]{"IBM", null, "GOOG"}, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount);
                    }
                }
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }

    });

    InputHandler eventStream = siddhiAppRuntime.getInputHandler("EventStream");

    siddhiAppRuntime.start();

    eventStream.send(new Object[]{"IBM", 75.6f, 105});
    Thread.sleep(100);
    eventStream.send(new Object[]{"GOOG", 21f, 61});
    eventStream.send(new Object[]{"WSO2", 21f, 61});
    Thread.sleep(100);

    AssertJUnit.assertEquals("Number of success events", 1, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);

    siddhiAppRuntime.shutdown();
}
 
Example 20
Source File: TimeBatchWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void timeWindowBatchTest8() throws InterruptedException {

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "" +
            "define stream cseEventStream (symbol string, price float, volume int); " +
            "define window cseEventWindow (symbol string, price float, volume int) timeBatch(2 sec , 0); ";
    String query = "" +
            "@info(name = 'query0') " +
            "from cseEventStream " +
            "insert into cseEventWindow; " +
            "" +
            "@info(name = 'query1') " +
            "from cseEventWindow " +
            "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
    while (System.currentTimeMillis() % 2000 != 0) {

    }
    inputHandler.send(new Object[]{"IBM", 700f, 0});
    inputHandler.send(new Object[]{"WSO2", 60.5f, 1});
    Thread.sleep(8500);
    inputHandler.send(new Object[]{"WSO2", 60.5f, 1});
    inputHandler.send(new Object[]{"II", 60.5f, 1});
    Thread.sleep(13000);
    inputHandler.send(new Object[]{"TT", 60.5f, 1});
    inputHandler.send(new Object[]{"YY", 60.5f, 1});
    Thread.sleep(5000);
    AssertJUnit.assertEquals(3, inEventCount);
    AssertJUnit.assertEquals(0, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();

}