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

The following examples show how to use io.siddhi.core.SiddhiAppRuntime#shutdown() . 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: SimpleQueryValidatorTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = SiddhiAppCreationException.class)
public void testQueryWithEveryAggregation() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String tables = "define stream TradeStream (symbol string, price double, volume long, timestamp long);\n" +
            "define aggregation TradeAggregation\n" +
            "  from TradeStream\n" +
            "  select symbol, avg(price) as avgPrice, sum(price) as total\n" +
            "    group by symbol\n" +
            "    aggregate by symbol every sec ... year; " +
            "" +
            "from every TradeAggregation " +
            "select * " +
            "insert into OutputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(tables);
    siddhiAppRuntime.shutdown();
}
 
Example 2
Source File: SiddhiPolicyTest.java    From eagle with Apache License 2.0 6 votes vote down vote up
@Test
public void testStrConcat() throws Exception {
    String ql = " define stream log(timestamp long, switchLabel string, port string, message string); " +
        " from log select timestamp, str:concat(switchLabel, '===', port) as alertKey, message insert into output; ";
    SiddhiManager manager = new SiddhiManager();
    SiddhiAppRuntime runtime = manager.createSiddhiAppRuntime(ql);
    runtime.addCallback("output", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
        }
    });

    runtime.start();

    InputHandler logInput = runtime.getInputHandler("log");

    Event e = new Event();
    e.setTimestamp(System.currentTimeMillis());
    e.setData(new Object[] {System.currentTimeMillis(), "switch-ra-slc-01", "port01", "log-message...."});
    logInput.send(e);

    Thread.sleep(1000);
    runtime.shutdown();

}
 
Example 3
Source File: TestSiddhiPattern.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Test
public void testPattern() throws Exception{
    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "define stream eventStream (timeStamp long, user string, src string, cmd string);";
    String query = "@info(name = 'query1') from " +
            "every a = eventStream[cmd=='getfileinfo'] " +
            "-> b = eventStream[cmd=='append' and user==a.user and src==a.src] " +
            "-> c = eventStream[cmd=='getfileinfo'and user==a.user and src==a.src] " +
            "select a.user as user, b.cmd as cmd, a.src as src " +
            "insert into outputStreams";

    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);
        }
    });

    InputHandler inputHandler = SiddhiAppRuntime.getInputHandler("eventStream");
    SiddhiAppRuntime.start();
    long curTime = DateTimeUtil.humanDateToMilliseconds("2015-09-17 00:00:00,000");
    System.out.println("curTime : " + curTime);
    inputHandler.send(new Object[]{curTime, "user", "/tmp/private", "getfileinfo"});
    inputHandler.send(new Object[]{curTime, "user", "/tmp/private", "append"});
    inputHandler.send(new Object[]{curTime, "user", "/tmp/private1", "getfileinfo"});
    inputHandler.send(new Object[]{curTime, "user", "/tmp/private1", "open"});
    inputHandler.send(new Object[]{curTime, "user", "/tmp/private1", "append"});
    inputHandler.send(new Object[]{curTime, "user", "/tmp/private", "getfileinfo"});
    Thread.sleep(100);
    inputHandler.send(new Object[]{curTime, "user", "/tmp/private1", "getfileinfo"});
    Thread.sleep(100);
    SiddhiAppRuntime.shutdown();

}
 
Example 4
Source File: LengthWindowTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = SiddhiAppCreationException.class)
public void avgAggregatorTest59() throws InterruptedException {

    SiddhiManager siddhiManager = new SiddhiManager();

    String execPlan = "" +
            "@app:name('avgAggregatorTests') " +
            "" +
            "define stream cseEventStream (weight double, deviceId string);" +
            "" +
            "@info(name = 'query1') " +
            "from cseEventStream#window.length(5) " +
            "select avg(weight,deviceId) as avgWeight " +
            "insert into outputStream;";

    SiddhiAppRuntime execPlanRunTime = siddhiManager.createSiddhiAppRuntime(execPlan);
    execPlanRunTime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {

            EventPrinter.print(timestamp, inEvents, removeEvents);
            AssertJUnit.assertEquals(26, inEvents[0].getData()[0]);
        }
    });

    InputHandler inputHandler = execPlanRunTime.getInputHandler("cseEventStream");

    execPlanRunTime.start();
    inputHandler.send(new Object[]{20.0, "Box1"});
    inputHandler.send(new Object[]{30.0, "Box2"});
    inputHandler.send(new Object[]{20.0, "Box3"});
    inputHandler.send(new Object[]{40.0, "Box4"});
    inputHandler.send(new Object[]{20.0, "Box5"});
    Thread.sleep(100);
    execPlanRunTime.shutdown();

}
 
Example 5
Source File: OnDemandQueryTableTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void test21() throws InterruptedException {
    log.info("Testing update on-demand query parser failure");

    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 symbol, sum(price) as totalPrice, avg(price) as avgPrice " +
            "group by symbol " +
            "aggregate by timestamp every sec...year ;";

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

    try {
        siddhiAppRuntime.query("from stockAggregation within 0L, 1543664151000L per " +
            "'minutes' select AGG_TIMESTAMP2, symbol, totalPrice, avgPrice ");
        Thread.sleep(100);
        Assert.fail("Expected OnDemandQueryCreationException exception");
    } catch (OnDemandQueryCreationException e) {
        String expectedCauseBy = "@ Line: 1. Position: 83, near 'AGG_TIMESTAMP2'. " +
                "No matching stream reference found for attribute 'AGG_TIMESTAMP2'";
        Assert.assertTrue(e.getCause().getMessage().endsWith(expectedCauseBy));
    }

    siddhiAppRuntime.shutdown();
}
 
Example 6
Source File: LogicalAbsentSequenceTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(dependsOnMethods = {"testQueryAbsent46"})
public void testQueryAbsent47() throws InterruptedException {
    log.info("Test the query every (not e1 for 1 sec or not e2 for 1 sec), e3 with two e3s");

    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 or 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[]{"IBM"});

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

    siddhiAppRuntime.start();

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

    TestUtil.waitForInEvents(100, callback, 5);
    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 7
Source File: DeleteFromTableTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void deleteFromTableTest3() throws InterruptedException {
    log.info("deleteFromTableTest3");

    SiddhiManager siddhiManager = new SiddhiManager();

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

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

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

    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});

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

}
 
Example 8
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testFilterQuery113() throws InterruptedException {
    log.info("Filter test113");

    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).attribute("awards", Attribute.Type.LONG);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream"));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("modPrice", Expression.mod(Expression.variable("price"), Expression.value(2))).
                    select("modVolume", Expression.mod(Expression.variable("volume"), Expression.value(2))).
                    select("modQuantity", Expression.mod(Expression.variable("quantity"), Expression.value(2))).
                    select("modAwards", Expression.mod(Expression.variable("awards"), Expression.value(2)))

    );
    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);
            AssertJUnit.assertTrue("1.5".equals(inEvents[0].getData()[1].toString()));
            AssertJUnit.assertTrue("1.0".equals(inEvents[0].getData()[2].toString()));
            AssertJUnit.assertTrue("1".equals(inEvents[0].getData()[3].toString()));
            AssertJUnit.assertTrue("1".equals(inEvents[0].getData()[4].toString()));
            count.addAndGet(inEvents.length);
        }

    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 55.5f, 101d, 5, 7L});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example 9
Source File: DeleteFromTestStoreTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void deleteFromTestStoreTest6() throws InterruptedException, SQLException {
    log.info("deleteFromTestStoreTest6");
    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=\"testStoreContainingInMemoryTable\")\n" +
            "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=='IBM' and StockTable.price > 50 and  StockTable.volume == volume  ;";

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

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

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable ");
    EventPrinter.print(events);
    try {
        AssertJUnit.assertEquals(1, events.length);
    } catch (NullPointerException ignore) {

    }

    siddhiAppRuntime.shutdown();
}
 
Example 10
Source File: LengthBatchWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testLengthBatchWindow2() throws InterruptedException {
    log.info("Testing length batch window with no of events greater than window size");

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

    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int); " +
            "define window cseWindow (symbol string, price float, volume int) lengthBatch(" + length + "); ";
    String query = "@info(name = 'query1') from cseEventStream select symbol,price,volume insert into cseWindow ;" +
            "@info(name = 'query2') from cseWindow insert into outputStream ;";


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

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

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count++;
                AssertJUnit.assertEquals("In event order", count, event.getData(2));
            }
            eventArrived = true;
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 700f, 1});
    inputHandler.send(new Object[]{"WSO2", 60.5f, 2});
    inputHandler.send(new Object[]{"IBM", 700f, 3});
    inputHandler.send(new Object[]{"WSO2", 60.5f, 4});
    inputHandler.send(new Object[]{"IBM", 700f, 5});
    inputHandler.send(new Object[]{"WSO2", 60.5f, 6});
    Thread.sleep(500);
    AssertJUnit.assertEquals("Total event count", 4, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();

}
 
Example 11
Source File: TablePartitionTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testPartitionQuery2() throws InterruptedException {
    log.info("Table Partition test 2");
    SiddhiManager siddhiManager = new SiddhiManager();

    String siddhiApp = "" +
            "@app:name('PartitionTest') " +
            "define stream streamA (symbol string, price int);" +
            "define stream streamB (symbol string);" +
            "define table tableA (symbol string, price int);" +
            "partition with (symbol of streamA, symbol of streamB) " +
            "begin " +
            "   @info(name = 'query1') " +
            "   from streamA " +
            "   select symbol, price " +
            "   insert into tableA;  " +
            "" +
            "end ;" +
            "" +
            "@info(name = 'query2') " +
            "from streamB[(symbol==tableA.symbol) in tableA] " +
            "select symbol " +
            "insert into outputStream;  " +
            "";


    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
            }
            eventArrived = true;
        }
    });

    InputHandler streamAInputHandler = siddhiAppRuntime.getInputHandler("streamA");
    InputHandler streamBInputHandler = siddhiAppRuntime.getInputHandler("streamB");
    siddhiAppRuntime.start();
    streamAInputHandler.send(new Object[]{"IBM", 700});
    streamAInputHandler.send(new Object[]{"WSO2", 60});
    streamAInputHandler.send(new Object[]{"WSO2", 60});
    streamBInputHandler.send(new Object[]{"WSO2"});
    streamBInputHandler.send(new Object[]{"FB"});
    streamBInputHandler.send(new Object[]{"IBM"});
    SiddhiTestHelper.waitForEvents(100, 2, count, 60000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(2, count.get());
    AssertJUnit.assertEquals(true, eventArrived);
}
 
Example 12
Source File: SortWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void sortWindowTest1() throws InterruptedException {
    log.info("sortWindow test1");

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "" +
            "define stream cseEventStream (symbol string, price float, volume long);";
    String query = "" +
            "@info(name = 'query1') " +
            "from cseEventStream#window.sort(2,volume, 'asc') " +
            "select volume " +
            "insert all events into outputStream ;";

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

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

    });


    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 55.6f, 100L});
    inputHandler.send(new Object[]{"IBM", 75.6f, 300L});
    inputHandler.send(new Object[]{"WSO2", 57.6f, 200L});
    inputHandler.send(new Object[]{"WSO2", 55.6f, 20L});
    inputHandler.send(new Object[]{"WSO2", 57.6f, 40L});
    Thread.sleep(1000);
    AssertJUnit.assertEquals(5, inEventCount);
    AssertJUnit.assertEquals(3, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();

}
 
Example 13
Source File: JoinTestStoreTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testTableJoinQuery6() throws InterruptedException, SQLException {
    log.info("testTableJoinQuery6 - OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define stream CheckStockStream (symbol string); " +
            "@Store(type=\"testStoreContainingInMemoryTable\")\n" +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from CheckStockStream#window.length(1) join StockTable as s " +
            " on CheckStockStream.symbol==s.symbol " +
            "select CheckStockStream.symbol as checkSymbol, s.symbol as symbol, s.volume as volume  " +
            "insert into OutputStream ;";

    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:
                            Assert.assertEquals(event.getData(), new Object[]{"WSO2", "WSO2", 100L});
                            break;
                        default:
                            Assert.assertSame(inEventCount, 1);
                    }
                }
                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, 100L});
    stockStream.send(new Object[]{"IBM", 75.6f, 10L});
    checkStockStream.send(new Object[]{"WSO2"});
    Thread.sleep(1000);

    Assert.assertEquals(inEventCount, 1, "Number of success events");
    Assert.assertEquals(removeEventCount, 0, "Number of remove events");
    Assert.assertEquals(eventArrived, true, "Event arrived");
    siddhiAppRuntime.shutdown();
}
 
Example 14
Source File: UpdateTableWithCacheTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void updateFromTableTest5() throws InterruptedException, SQLException {
    log.info("updateFromTableTest5");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define stream CheckStockStream (symbol string, volume long); " +
            "@Store(type=\"testStoreContainingInMemoryTable\", @Cache(size=\"10\"))\n" +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from CheckStockStream[(StockTable.symbol==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++;
                }
                eventArrived = true;
            }

        }

    });

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

    stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
    stockStream.send(new Object[]{"IBM", 55.6f, 100L});
    checkStockStream.send(new Object[]{"BSD", 100L});
    checkStockStream.send(new Object[]{"WSO2", 100L});
    checkStockStream.send(new Object[]{"IBM", 100L});
    Thread.sleep(1000);

    AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example 15
Source File: InMemoryTransportTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"inMemoryTestCase18"})
public void inMemoryTestCase19() throws InterruptedException, SubscriberUnAvailableException {
    log.info("Test inMemoryTestCase19");

    InMemoryBroker.Subscriber subscriptionWSO2 = new InMemoryBroker.Subscriber() {
        @Override
        public void onMessage(Object msg) {
            wso2Count.incrementAndGet();
        }

        @Override
        public String getTopic() {
            return "WSO2";
        }
    };

    String streams = "" +
            "@app:name('TestSiddhiApp')" +
            "@source(type='inMemory', topic='WSO2', @map(type='testSourceOption')) " +
            "define stream FooStream (symbol string, price float, volume long); " +
            "define stream BarStream (symbol string, price float, volume long); ";

    String query = "" +
            "from FooStream " +
            "select * " +
            "insert into BarStream; ";

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

    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                switch (wso2Count.incrementAndGet()) {
                    case 1:
                        Assert.assertEquals(event.getData(), new Object[]{"WSO2", "inMemory", 100L});
                        break;
                    case 2:
                        Assert.assertEquals(event.getData(), new Object[]{"WSO2", "inMemory", 101L});
                        break;
                    default:
                        org.testng.AssertJUnit.fail();
                }
            }
        }
    });

    siddhiAppRuntime.start();

    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[]{"WSO2", 55.6f, 100L}));
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[]{"WSO2", 57.6f, 101L}));
    Thread.sleep(100);

    //assert event count
    AssertJUnit.assertEquals("Number of WSO2 events", 2, wso2Count.get());
    siddhiAppRuntime.shutdown();

    InMemoryBroker.unsubscribe(subscriptionWSO2);

}
 
Example 16
Source File: AbsentPatternTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"testQueryAbsent18"})
public void testQueryAbsent19() throws InterruptedException {
    log.info("Test the query e1 -> e2 -> e3 -> not e4 for 1 sec with e1, e2, e3 and not e4");

    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] -> e2=Stream2[price>20] -> e3=Stream3[price>30] -> not Stream4[price>40] " +
            "for 1 sec  " +
            "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",
            new Object[]{"WSO2", "IBM", "GOOGLE"});

    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", 35.7f, 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 arrived", callback.isEventArrived());

    siddhiAppRuntime.shutdown();
}
 
Example 17
Source File: PartitionTestCase1.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testPartitionQuery6() throws InterruptedException {
    log.info("Partition test6");
    SiddhiManager siddhiManager = new SiddhiManager();

    String siddhiApp = "@app:name('PartitionTest6') " +
            "define stream cseEventStream (symbol string, price float,volume int);"
            + "define stream cseEventStream1 (symbol string, price float,volume int);"
            + "partition with (symbol of cseEventStream , symbol of cseEventStream1) begin @info(name = 'query') " +
            "from cseEventStream select symbol,price,volume insert into #StockStream ;"
            + "@info(name = 'query1') from #StockStream select symbol,price,volume insert into OutStockStream ;"
            + "@info(name = 'query2') from cseEventStream1 select symbol,price,volume insert into  #StockStream1 ;"
            + "@info(name = 'query3') from #StockStream1 select symbol,price,volume insert into OutStockStream ; " +
            "end ";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);


    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });


    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    InputHandler inputHandler2 = siddhiAppRuntime.getInputHandler("cseEventStream1");

    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 75.6f, 100});
    inputHandler.send(new Object[]{"WSO2", 75.6f, 100});
    inputHandler.send(new Object[]{"IBM", 75.6f, 100});
    inputHandler.send(new Object[]{"ORACLE", 75.6f, 100});

    inputHandler2.send(new Object[]{"IBM", 75.6f, 100});
    inputHandler2.send(new Object[]{"WSO21", 75.6f, 100});
    inputHandler2.send(new Object[]{"IBM1", 75.6f, 100});
    inputHandler2.send(new Object[]{"ORACLE1", 75.6f, 100});

    SiddhiTestHelper.waitForEvents(100, 8, count, 60000);
    AssertJUnit.assertEquals(8, count.get());

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

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

    String inStreamDefinition = "define stream inputStream (price1 double,price2 double, price3 double);";
    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.0, event.getData(0));
                        break;
                    case 2:
                        AssertJUnit.assertEquals(36.0, event.getData(0));
                        break;
                    case 3:
                        AssertJUnit.assertEquals(36.0, event.getData(0));
                        break;
                    case 4:
                        AssertJUnit.assertEquals(36.0, event.getData(0));
                        break;
                    case 5:
                        AssertJUnit.assertEquals(36.0, event.getData(0));
                        break;
                    case 6:
                        AssertJUnit.assertEquals(36.0, event.getData(0));
                        break;
                    default:
                        org.testng.AssertJUnit.fail();
                }
            }
        }
    });

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

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

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

}
 
Example 19
Source File: ExtensionSample.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {

        // Creating Siddhi Manager
        SiddhiManager siddhiManager = new SiddhiManager();

        //Register the extension to Siddhi Manager
        siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);

        //Siddhi Application
        String siddhiApp = "" +
                "define stream StockStream (symbol string, price long, volume long);" +
                "" +
                "@info(name = 'query1') " +
                "from StockStream " +
                "select symbol , custom:plus(price, volume) as totalCount " +
                "insert into Output;";

        //Generating runtime
        SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

        //Adding callback to retrieve output events from query
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {
            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
            }
        });

        //Retrieving InputHandler to push events into Siddhi
        InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");

        //Starting event processing
        siddhiAppRuntime.start();

        //Sending events to Siddhi
        inputHandler.send(new Object[]{"IBM", 700L, 100L});
        inputHandler.send(new Object[]{"WSO2", 600L, 200L});
        inputHandler.send(new Object[]{"GOOG", 60L, 200L});
        Thread.sleep(500);

        //Shutting down the runtime
        siddhiAppRuntime.shutdown();

        //Shutting down Siddhi
        siddhiManager.shutdown();

    }
 
Example 20
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();
}