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

The following examples show how to use io.siddhi.core.SiddhiAppRuntime#start() . 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: LogicalAbsentPatternTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(dependsOnMethods = {"testQueryAbsent5_1"})
public void testQueryAbsent5_2() throws InterruptedException {
    log.info("Test the query e1 -> not e2 for 1 sec and e3 with e1 and e3 after 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); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from e1=Stream1[price>10] -> not Stream2[price>20] for 1 sec and e3=Stream3[price>30] " +
            "select e1.symbol as symbol1, 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");
    siddhiAppRuntime.start();

    Thread.sleep(1100);
    stream1.send(new Object[]{"WSO2", 15.0f, 100});
    Thread.sleep(100);
    stream3.send(new Object[]{"GOOGLE", 35.0f, 100});
    Thread.sleep(100);

    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 2
Source File: InsertIntoTableTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void insertIntoTableTest1() throws InterruptedException {
    log.info("InsertIntoTableTest1");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "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);

    siddhiAppRuntime.shutdown();

}
 
Example 3
Source File: LogicalAbsentSequenceTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(dependsOnMethods = {"testQueryAbsent37"})
public void testQueryAbsent38() throws InterruptedException {
    log.info("Test the query (not e1 for 1 sec and not e2 for 1 sec), e3 with e1 and 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 and not Stream2[price>20] for 1 sec), e3=Stream3[price>30] " +
            "select e3.symbol as symbol " +
            "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");

    siddhiAppRuntime.start();

    Thread.sleep(500);
    stream1.send(new Object[]{"IBM", 15.0f, 100});
    Thread.sleep(600);
    stream3.send(new Object[]{"WSO2", 35.0f, 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 4
Source File: LogicalAbsentPatternTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(dependsOnMethods = {"testQueryAbsent49"})
public void testQueryAbsent50() throws InterruptedException {
    log.info("Test the query every (not e1 for 1 sec and not e2 for 1 sec) -> e3 with an e3 after 2 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); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from every (not Stream1[price>10] for 1 sec and not Stream2[price>20] for 1 sec) -> " +
            "e3=Stream3[price>30] " +
            "select e3.symbol as symbol " +
            "insert into OutputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    TestUtil.TestCallback callback = TestUtil.addQueryCallback(siddhiAppRuntime, "query1", new Object[]{"WSO2"},
            new Object[]{"WSO2"});

    InputHandler stream3 = siddhiAppRuntime.getInputHandler("Stream3");
    siddhiAppRuntime.start();

    Thread.sleep(2100);
    stream3.send(new Object[]{"WSO2", 35.0f, 100});
    Thread.sleep(100);


    callback.throwAssertionErrors();
    AssertJUnit.assertEquals("Number of success events", 2, callback.getInEventCount());
    AssertJUnit.assertEquals("Number of remove events", 0, callback.getRemoveEventCount());
    AssertJUnit.assertTrue("Event arrived", callback.isEventArrived());

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

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

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("price"),
            Compare.Operator.LESS_THAN,
            Expression.value(60L))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")).select("quantity", Expression.variable("quantity")));
    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", 500f, 50d, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 60d, 2});
    inputHandler.send(new Object[]{"WSO2", 50f, 300d, 4});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example 6
Source File: UpdateFromTableTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void updateFromTableTest2() throws InterruptedException {
    log.info("updateFromTableTest2");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define stream UpdateStockStream (symbol string, price float, volume long); " +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from UpdateStockStream " +
            "update StockTable " +
            "   on StockTable.symbol==symbol ;";

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

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

    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});
    updateStockStream.send(new Object[]{"WSO2", 10f, 100L});

    Thread.sleep(500);
    siddhiAppRuntime.shutdown();

}
 
Example 7
Source File: InsertIntoTableTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void insertIntoTableTest6() throws InterruptedException {
    log.info("InsertIntoTableTest6");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define stream StockCheckStream (symbol string); " +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from StockCheckStream[StockTable.symbol==StockCheckStream.symbol in StockTable] " +
            "insert into OutStream ;";

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

    siddhiAppRuntime.addCallback("query2", 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"}, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[]{"WSO2"}, event.getData());
                            break;
                        default:
                            AssertJUnit.fail();
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }

    });

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

    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
    stockStream.send(new Object[]{"IBM", 55.6f, 100L});
    stockCheckStream.send(new Object[]{"IBM"});
    stockCheckStream.send(new Object[]{"WSO2"});

    Thread.sleep(500);

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

    siddhiAppRuntime.shutdown();
}
 
Example 8
Source File: MaximumFunctionExtensionTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testMaxFunctionExtension9() throws InterruptedException {
    log.info("MaximumFunctionExecutor TestCase 9");
    SiddhiManager siddhiManager = new SiddhiManager();

    String inStreamDefinition = "define stream inputStream (price1 float,price2 float, price3 float);";
    String query = ("@info(name = 'query1') from inputStream " +
            "select maximum(price1, price2, price3) as max " +
            "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.75f, event.getData(0));
                        break;
                    case 2:
                        AssertJUnit.assertEquals(38.12f, event.getData(0));
                        break;
                    case 3:
                        AssertJUnit.assertEquals(39.25f, event.getData(0));
                        break;
                    case 4:
                        AssertJUnit.assertEquals(37.75f, event.getData(0));
                        break;
                    case 5:
                        AssertJUnit.assertEquals(38.12f, event.getData(0));
                        break;
                    case 6:
                        AssertJUnit.assertEquals(40.0f, event.getData(0));
                        break;
                    default:
                        AssertJUnit.fail();
                }
            }
        }
    });

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

    inputHandler.send(new Object[]{36f, 36.75f, 35.75f});
    inputHandler.send(new Object[]{37.88f, 38.12f, 37.62f});
    inputHandler.send(new Object[]{39.00f, 39.25f, 38.62f});
    inputHandler.send(new Object[]{36.88f, 37.75f, 36.75f});
    inputHandler.send(new Object[]{38.12f, 38.12f, 37.75f});
    inputHandler.send(new Object[]{38.12f, 40f, 37.75f});

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

}
 
Example 9
Source File: SnapshotOutputRateLimitTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"testSnapshotOutputRateLimitQuery18"})
public void testSnapshotOutputRateLimitQuery19() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test19");

    SiddhiManager siddhiManager = new SiddhiManager();

    String siddhiApp = "" +
            "@app:name('SnapshotOutputRateLimitTest19') " +
            "" +
            "define stream LoginEvents (timestamp long, ip string, calls int);" +
            "" +
            "@info(name = 'query1') " +
            "from LoginEvents#window.time(5 sec) " +
            "select  ip, sum(calls) as totalCalls " +
            "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();
                if (count.get() == 1 || count.get() == 2) {
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(1) == 9L);
                    AssertJUnit.assertTrue((Long) inEvents[1].getData(1) == 9L);
                } else if (count.get() == 3 || count.get() == 4 || count.get() == 5) {
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(1) == 21L);
                    AssertJUnit.assertTrue((Long) inEvents[1].getData(1) == 21L);
                    AssertJUnit.assertTrue((Long) inEvents[2].getData(1) == 21L);
                    AssertJUnit.assertTrue((Long) inEvents[3].getData(1) == 21L);
                } else if (count.get() == 6 || count.get() == 7) {
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(1) == 12L);
                    AssertJUnit.assertTrue((Long) inEvents[1].getData(1) == 12L);
                }
            }
        }

    });

    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(7100);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event value", 7, count.get());

    siddhiAppRuntime.shutdown();

}
 
Example 10
Source File: EveryAbsentSequenceTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryAbsent18() throws InterruptedException {
    log.info("Test the query every (not e1 and e2), e3 with e2 and e3 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); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from every (not Stream1[price>10] and 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", new Object[]{"WSO2",
            "GOOGLE"});

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


    siddhiAppRuntime.start();

    stream2.send(new Object[]{"IBM", 25.0f, 100});
    Thread.sleep(100);
    stream2.send(new Object[]{"WSO2", 26.0f, 100});
    Thread.sleep(100);
    stream3.send(new Object[]{"GOOGLE", 35.0f, 100});
    Thread.sleep(100);

    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 11
Source File: OnDemandQueryTableTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void test11() throws InterruptedException {
    log.info("Test10 table");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long);" +
            "@PrimaryKey('symbol') " +
            "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);

    String onDemandQuery = "" +
            "from StockTable " +
            "on volume > 10 " +
            "select symbol, price, sum(volume) as totalVolume " +
            "group by symbol ";
    Event[] events = siddhiAppRuntime.query(onDemandQuery);
    EventPrinter.print(events);
    AssertJUnit.assertEquals(2, events.length);
    AssertJUnit.assertEquals(100L, events[0].getData()[2]);
    AssertJUnit.assertEquals(100L, events[1].getData()[2]);

    events = siddhiAppRuntime.query(onDemandQuery);
    EventPrinter.print(events);
    AssertJUnit.assertEquals(2, events.length);
    AssertJUnit.assertEquals(100L, events[0].getData()[2]);
    AssertJUnit.assertEquals(100L, events[1].getData()[2]);
}
 
Example 12
Source File: JoinTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void joinTest3() throws InterruptedException {
    log.info("Join test3");

    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream cseEventStream (symbol string, price float, volume int); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from cseEventStream#window.time(500 milliseconds) as a " +
            "join cseEventStream#window.time(500 milliseconds) as b " +
            "on a.symbol== b.symbol " +
            "select a.symbol as symbol, a.price as priceA, b.price as priceB " +
            "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.getAndAdd(removeEvents.length);
                }
                eventArrived = true;
            }
        });

        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        siddhiAppRuntime.start();
        cseEventStreamHandler.send(new Object[]{"IBM", 75.6f, 100});
        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();
    }
}
 
Example 13
Source File: PrimaryKeyTableTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void primaryKeyTableTest24() throws InterruptedException {
    log.info("primaryKeyTableTest24");

    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define stream CheckStockStream (symbol string, volume long); " +
            "@PrimaryKey('volume') " +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from CheckStockStream[(volume < StockTable.volume) in StockTable] " +
            "insert into OutStream;";

    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) {
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
                if (removeEvents != null) {
                    removeEventCount = removeEventCount + removeEvents.length;
                }
                eventArrived = true;
            }
        });

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

        siddhiAppRuntime.start();
        stockStream.send(new Object[]{"WSO2", 55.6f, 200L});
        stockStream.send(new Object[]{"BAR", 55.6f, 150L});
        stockStream.send(new Object[]{"IBM", 55.6f, 100L});
        checkStockStream.send(new Object[]{"FOO", 170L});
        checkStockStream.send(new Object[]{"FOO", 500L});

        List<Object[]> expected = new ArrayList<Object[]>();
        expected.add(new Object[]{"FOO", 170L});

        SiddhiTestHelper.waitForEvents(100, 1, inEventCount, 60000);
        AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isUnsortedEventsMatch(inEventsList,
                expected));
        AssertJUnit.assertEquals("Number of success events", 1, inEventCount.get());
        AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
        AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
 
Example 14
Source File: CacheFIFOTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(description = "cacheFIFOTestCase0") // using query api
public void cacheFIFOTestCase0() throws InterruptedException, SQLException {
    final TestAppenderToValidateLogsForCachingTests appender = new TestAppenderToValidateLogsForCachingTests();
    final Logger logger = Logger.getRootLogger();
    logger.setLevel(Level.DEBUG);
    logger.addAppender(appender);
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define stream DeleteStockStream (symbol string, price float, volume long); " +
            "@Store(type=\"testStoreForCacheMiss\", @Cache(size=\"2\", cache.policy=\"FIFO\"))\n" +
            "@PrimaryKey('symbol') " +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from DeleteStockStream " +
            "delete StockTable " +
            "   on StockTable.symbol == symbol AND StockTable.price == price AND StockTable.volume == volume;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler deleteStockStream = siddhiAppRuntime.getInputHandler("DeleteStockStream");
    siddhiAppRuntime.start();

    deleteStockStream.send(new Object[]{"WSO2", 55.6f, 1L});
    deleteStockStream.send(new Object[]{"IBM", 75.6f, 2L});
    stockStream.send(new Object[]{"CISCO", 75.6f, 3L});
    Thread.sleep(10);
    stockStream.send(new Object[]{"APPLE", 75.6f, 4L});
    Thread.sleep(1000);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "on symbol == \"WSO2\" ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);

    events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "on symbol == \"CISCO\" ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);

    final List<LoggingEvent> log = appender.getLog();
    List<String> logMessages = new ArrayList<>();
    for (LoggingEvent logEvent : log) {
        String message = String.valueOf(logEvent.getMessage());
        if (message.contains(":")) {
            message = message.split(": ")[1];
        }
        logMessages.add(message);
    }
    Assert.assertEquals(logMessages.
            contains("store table size is smaller than max cache. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("store table size is bigger than cache."), true);
    Assert.assertEquals(Collections.frequency(logMessages, "store table size is bigger than cache."), 2);
    Assert.assertEquals(logMessages.contains("cache constraints satisfied. Checking cache"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "cache constraints satisfied. Checking cache"), 2);
    Assert.assertEquals(logMessages.contains("cache hit. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("cache miss. Loading from store"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "cache miss. Loading from store"), 2);
    Assert.assertEquals(logMessages.contains("store also miss. sending null"), false);
    Assert.assertEquals(logMessages.contains("sending results from cache after loading from store"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "sending results from cache after loading from store"),
            2);
    Assert.assertEquals(logMessages.contains("sending results from store"), false);

    siddhiAppRuntime.shutdown();
}
 
Example 15
Source File: FaultStreamTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = "faultStreamTest5")
public void faultStreamTest6() throws InterruptedException {
    log.info("faultStreamTest6-Tests logging by default when fault handling is not configured "
            + "explicitly at sink level during publishing failures.");

    SiddhiManager siddhiManager = new SiddhiManager();

    String siddhiApp = "" +
            "@OnError(action='stream')" +
            "define stream cseEventStream (symbol string, price float, volume long);" +
            "\n" +
            "@sink(type='inMemory', topic='{{symbol}}', @map(type='passThrough')) " +
            "define stream outputStream (symbol string, price float, sym1 string);" +
            "\n" +
            "@info(name = 'query1') " +
            "from cseEventStream " +
            "select symbol, price , symbol as sym1 " +
            "insert into outputStream ;" +
            "";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            Assert.assertTrue(events[0].getData(0) != null);
        }
    });

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

    Logger logger = Logger.getLogger(Sink.class);
    UnitTestAppender appender = new UnitTestAppender();
    logger.addAppender(appender);
    try {
        inputHandler.send(new Object[]{"IBM", 0f, 100L});
        AssertJUnit.assertTrue(appender.getMessages().contains("Dropping event at Sink 'inMemory' at"));
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        logger.removeAppender(appender);
        siddhiAppRuntime.shutdown();
    }

}
 
Example 16
Source File: PatternPartitionTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testPatternPartitionQuery14() throws InterruptedException {
    log.info("testPatternCount6 - 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 partitionStart = "partition with (volume of Stream1,volume of Stream2) begin ";
    String query = "" +
            "@info(name = 'query1') " +
            "from e1=Stream1[price>20] <2:5> -> e2=Stream2[price>e1[1].price] " +
            "select e1[0].price as price1_0, e1[1].price as price1_1, e2.price as price2 " +
            "insert into OutputStream ;";
    String partitionEnd = "end";

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

    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                if (event.isExpired()) {
                    removeEventCount.incrementAndGet();
                } else {
                    inEventCount.incrementAndGet();
                    AssertJUnit.assertArrayEquals(new Object[]{25.6f, 47.6f, 55.7f}, event.getData());
                }
                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);
    stream1.send(new Object[]{"GOOG", 47.6f, 100});
    Thread.sleep(100);
    stream2.send(new Object[]{"IBM", 45.7f, 100});
    Thread.sleep(100);
    stream2.send(new Object[]{"IBM", 55.7f, 100});
    SiddhiTestHelper.waitForEvents(100, 1, inEventCount, 60000);

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

    siddhiAppRuntime.shutdown();
}
 
Example 17
Source File: AbsentPatternTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"testQueryAbsent17"})
public void testQueryAbsent18() throws InterruptedException {
    log.info("Test the query not e1 for 1 sec -> e2 -> e3 with e1, e2 after 1 sec and 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", new Object[]{"IBM", "GOOGLE"});

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

    siddhiAppRuntime.start();

    stream1.send(new Object[]{"WSO2", 25.6f, 100});
    Thread.sleep(1100);
    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", 1, callback.getInEventCount());
    AssertJUnit.assertEquals("Number of remove events", 0, callback.getRemoveEventCount());
    AssertJUnit.assertTrue("Event arrived", callback.isEventArrived());

    siddhiAppRuntime.shutdown();
}
 
Example 18
Source File: LengthBatchWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void lengthBatchWindowTest21() throws InterruptedException {
    log.info("LengthBatchWindow Test21");

    final int length = 3;
    SiddhiManager siddhiManager = new SiddhiManager();

    String siddhiApp = "" +
            "define stream cseEventStream (symbol string, price float, volume int);" +
            "" +
            "@info(name = 'query1') " +
            "from cseEventStream#window.lengthBatch(" + length + ", true) " +
            "select symbol, price, count() as volumes " +
            "insert all events into outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {

            EventPrinter.print(events);
            eventArrived = true;
            if (events.length == 1) {
                inEventCount++;
            } else {
                AssertJUnit.assertFalse("Event batch with unexpected number of events " + events.length,
                        false);
            }
            for (Event event : events) {
                AssertJUnit.assertTrue("Count values", ((Long) event.getData(2) == 1
                        || (Long) event.getData(2) == 2 || ((Long) event.getData(2)) == 3));
                count++;
            }
        }
    });

    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});
    inputHandler.send(new Object[]{"WSO2", 60.5f, 4});
    inputHandler.send(new Object[]{"IBM", 700f, 5});
    inputHandler.send(new Object[]{"WSO2", 60.5f, 6});
    AssertJUnit.assertEquals("Total events", 9, count);
    AssertJUnit.assertEquals("1 event batch", 9, inEventCount);
    AssertJUnit.assertTrue(eventArrived);
    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: SandboxTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void sandboxTest1() throws InterruptedException {
    log.info("sandbox test1");

    SiddhiManager siddhiManager = new SiddhiManager();

    String app = "" +
            "@source(type='foo')" +
            "@source(type='foo1')" +
            "@sink(type='foo1')" +
            "@source(type='inMemory', topic='myTopic')" +
            "define stream StockStream (symbol string, price float, vol long);\n" +
            "" +
            "@sink(type='foo1')" +
            "@sink(type='inMemory', topic='myTopic1')" +
            "define stream DeleteStockStream (symbol string, price float, vol long);\n" +
            "" +
            "@store(type='rdbms')" +
            "define table StockTable (symbol string, price float, volume long);\n" +
            "" +
            "define stream CountStockStream (symbol string);\n" +
            "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "select symbol, price, vol as volume " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from DeleteStockStream[vol>=100] " +
            "delete StockTable " +
            "   on StockTable.symbol==symbol ;" +
            "" +
            "@info(name = 'query3') " +
            "from CountStockStream#window.length(0) join StockTable" +
            " on CountStockStream.symbol==StockTable.symbol " +
            "select CountStockStream.symbol as symbol " +
            "insert into CountResultsStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSandboxSiddhiAppRuntime(app);

    Assert.assertEquals(siddhiAppRuntime.getSources().size(), 1);
    Assert.assertEquals(siddhiAppRuntime.getSinks().size(), 1);
    Assert.assertEquals(siddhiAppRuntime.getTables().size(), 1);

    for (List<Source> sources : siddhiAppRuntime.getSources()) {
        for (Source source : sources) {
            Assert.assertTrue(source.getType().equalsIgnoreCase("inMemory"));
        }
    }

    for (List<Sink> sinks : siddhiAppRuntime.getSinks()) {
        for (Sink sink : sinks) {
            Assert.assertTrue(sink.getType().equalsIgnoreCase("inMemory"));
        }
    }

    for (Table table : siddhiAppRuntime.getTables()) {
        Assert.assertTrue(table instanceof InMemoryTable);
    }

    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler deleteStockStream = siddhiAppRuntime.getInputHandler("DeleteStockStream");
    InputHandler countStockStream = siddhiAppRuntime.getInputHandler("CountStockStream");

    siddhiAppRuntime.addCallback("CountResultsStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
        }
    });
    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});
    deleteStockStream.send(new Object[]{"IBM", 57.6f, 100L});
    countStockStream.send(new Object[]{"WSO2"});

    Thread.sleep(500);
    Assert.assertEquals(count.get(), 2);
    siddhiAppRuntime.shutdown();

}