io.siddhi.core.util.EventPrinter Java Examples

The following examples show how to use io.siddhi.core.util.EventPrinter. 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: TestEventTable.java    From eagle with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    SiddhiAppRuntime runtime = new SiddhiManager().createSiddhiAppRuntime(
        "define stream expectStream (key string, src string);" +
            "define stream appearStream (key string, src string);" +
            "define table expectTable (key string, src string);" +
            "from expectStream insert into expectTable;" +
            "from appearStream[(expectTable.key==key) in expectTable] insert into outputStream;"
    );

    runtime.addCallback("outputStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
        }
    });

    runtime.start();
    runtime.getInputHandler("expectStream").send(System.currentTimeMillis(), new Object[] {"host1", "expectStream"});
    Thread.sleep(2000);
    runtime.getInputHandler("appearStream").send(System.currentTimeMillis(), new Object[] {"host2", "expectStream"});
    Thread.sleep(2000);
}
 
Example #2
Source File: TestUtil.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Override
public void receive(Event[] events) {
    // Print the events
    EventPrinter.print(events);

    if (events != null) {
        eventArrived = true;
        for (Event event : events) {
            int inEvent = inEventCount.incrementAndGet();
            if (noOfExpectedEvents > 0 && inEvent <= noOfExpectedEvents) {
                try {
                    AssertJUnit.assertArrayEquals(expected[inEvent - 1], event.getData());
                } catch (AssertionError e) {
                    assertionErrors.add(e);
                }
            }
        }
    }
}
 
Example #3
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery74() throws InterruptedException {
    log.info("Filter test74");

    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("quantity"),
            Compare.Operator.LESS_THAN_EQUAL, Expression.value(5f))));
    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, 60d, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 60d, 2});
    inputHandler.send(new Object[]{"WSO2", 60f, 300d, 4});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    siddhiAppRuntime.shutdown();


}
 
Example #4
Source File: StreamFunctionTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void pol2CartFunctionTest2() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String polarStream = "define stream PolarStream (theta double, rho double, elevation double);";
    String query = "@info(name = 'query1') " +
            "from PolarStream#pol2Cart(theta, rho, elevation) " +
            "select x, z " +
            "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(polarStream + 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.assertEquals(12, Math.round((Double) inEvents[0].getData(0)));
                AssertJUnit.assertEquals(7, Math.round((Double) inEvents[0].getData(1)));
            }
            eventArrived = true;
        }

    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("PolarStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{22.6, 13.0, 7.0});
    Thread.sleep(100);
    AssertJUnit.assertEquals(1, inEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example #5
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery89() throws InterruptedException {
    log.info("Filter test89");

    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("quantity"),
            Compare.Operator.LESS_THAN, Expression.value(50d))));
    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", 50f, 60d, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 40d, 10});
    inputHandler.send(new Object[]{"WSO2", 44f, 200d, 56});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    AssertJUnit.assertEquals(2, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example #6
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery65() throws InterruptedException {
    log.info("Filter test65");

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

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.EQUAL, Expression.value(40))));
    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", 50f, 60L});
    inputHandler.send(new Object[]{"WSO2", 70f, 40L});
    inputHandler.send(new Object[]{"WSO2", 44f, 200L});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();

}
 
Example #7
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery17() throws InterruptedException {
    log.info("Filter test17");

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

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.GREATER_THAN, Expression.value(45))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")).select("volume", Expression.variable("volume")));
    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", 50f, 60L});
    inputHandler.send(new Object[]{"WSO2", 70f, 40L});
    inputHandler.send(new Object[]{"WSO2", 44f, 200L});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    siddhiAppRuntime.shutdown();


}
 
Example #8
Source File: TriggerTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(dependsOnMethods = "testQuery4")
public void testQuery5() throws InterruptedException {
    log.info("testTrigger5 - OUT 1");

    SiddhiManager siddhiManager = new SiddhiManager();

    String plan = "" +
            "define stream cseEventStream (symbol string, price float, volume long);" +
            "define trigger triggerStream at 'start';";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(plan);

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

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count += events.length;
            eventArrived = true;
        }
    });

    siddhiAppRuntime.start();

    Thread.sleep(100);
    AssertJUnit.assertEquals(1, count);
    AssertJUnit.assertEquals(true, eventArrived);
    siddhiAppRuntime.shutdown();

}
 
Example #9
Source File: MaxForeverAggregatorExtensionTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void maxForeverAttributeAggregatorTest7() throws InterruptedException {

    log.info("maxForeverAttributeAggregator Test 7");

    SiddhiManager siddhiManager = new SiddhiManager();

    String execPlan = "" +
            "@app:name('maxForeverAttributeAggregatorTests') " +
            "" +
            "define stream cseEventStream (price1 double, price2 double, price3 double);" +
            "" +
            "@info(name = 'query1') " +
            "from cseEventStream " +
            "select maxForever(price1) as max " +
            "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(30, inEvents[1].getData()[0]);
        }
    });

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

    execPlanRunTime.start();
    inputHandler.send(new Object[]{null, 20.0, 20.0});
    inputHandler.send(new Object[]{30.0, 30.88, 20.0});
    Thread.sleep(100);
    execPlanRunTime.shutdown();

}
 
Example #10
Source File: PartitionTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPartitionQuery32() throws InterruptedException {
    log.info("Partition test31");
    SiddhiManager siddhiManager = new SiddhiManager();

    String siddhiApp = "@app:name('PartitionTest31') " +
            "define stream cseEventStream (atr1 string,  atr2 float, atr3 int, atr4 double, " +
            "atr5 long,  atr6 long,  atr7 double,  atr8 float , atr9 bool, atr10 bool,  atr11 int);"
            + "partition with (atr1 of cseEventStream) begin @info(name = 'query1') from " +
            "cseEventStream[700>atr5 OR atr8< atr3 OR atr6 < atr2 OR atr4 " +
            "< atr6 OR atr2 < atr7 OR atr3 < atr4 OR atr4 < atr3 OR atr6 < atr3 OR atr3" +
            "< atr2 OR atr8 < atr5 OR atr6 < atr4 OR atr3 < atr6 OR atr7 < atr8 OR atr7 " +
            "< atr4 OR atr6 < atr5 OR atr11 < atr3 OR atr8 < atr2] select" +
            " atr1 as symbol, sum(atr2) as price" + " 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");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 75.6f, 100, 101.0, 500L, 200L, 102.0, 75.7f, false, true, 105});
    inputHandler.send(new Object[]{"WSO2", 75.6f, 100, 101.0, 501L, 201L, 103.0, 76.7f, false, true, 106});
    inputHandler.send(new Object[]{"IBM", 75.6f, 100, 102.0, 502L, 202L, 104.0, 77.7f, false, true, 107});
    inputHandler.send(new Object[]{"ORACLE", 75.6f, 100, 101.0, 502L, 202L, 104.0, 77.7f, false, true, 108});
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    AssertJUnit.assertEquals(4, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example #11
Source File: PartitionTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPartitionQuery16() throws InterruptedException {
    log.info("partition test16");
    SiddhiManager siddhiManager = new SiddhiManager();

    String siddhiApp = "@app:name('PartitionTest16') " +
            "define stream streamA (symbol string, price int);"
            + "partition with (symbol of streamA) begin @info(name = 'query1') from streamA select symbol,price " +
            "insert into StockQuote ;"
            + "@info(name = 'query2') from streamA select symbol,price insert into StockQuote ; end ";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("StockQuote", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            AssertJUnit.assertTrue("IBM".equals(events[0].getData(0)) || "WSO2".equals(events[0].getData(0)));
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("streamA");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 700});
    inputHandler.send(new Object[]{"WSO2", 60});
    inputHandler.send(new Object[]{"WSO2", 60});
    SiddhiTestHelper.waitForEvents(100, 6, count, 60000);
    AssertJUnit.assertEquals(6, count.get());
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example #12
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery61() throws InterruptedException {
    log.info("Filter test61");

    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("quantity"),
            Compare.Operator.EQUAL,
            Expression.value(4L))));
    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", 50f, 60d, 5});
    inputHandler.send(new Object[]{"WSO2", 70f, 60d, 2});
    inputHandler.send(new Object[]{"WSO2", 60f, 200d, 4});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();

}
 
Example #13
Source File: UpdateOrInsertTestStoreTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void updateOrInsertTableTest11() throws InterruptedException, SQLException {
    log.info("updateOrInsertTableTest11");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define stream UpdateStockStream (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 UpdateStockStream " +
            "update or insert into StockTable " +
            "   on StockTable.volume==volume ;";

    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[]{"GOOG", 10.6F, 100L});
    Thread.sleep(500);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);

    siddhiAppRuntime.shutdown();
}
 
Example #14
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void filterTest116() throws InterruptedException {
    log.info("filter test116");
    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume long);";
    String query = "@info(name = 'query1') from cseEventStream select symbol,price+5 as price 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);
            count.addAndGet(inEvents.length);
            eventArrived.set(true);
        }

    });

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

    inputHandler.send(new Object[]{"IBM", 700f, 100L});
    inputHandler.send(new Object[]{"WSO2", 60.5f, 200L});
    inputHandler.send(new Object[]{"IBM", 700f, 100L});
    SiddhiTestHelper.waitForEvents(10, 3, count, 100);
    AssertJUnit.assertEquals(3, count.get());
    AssertJUnit.assertTrue(eventArrived.get());
    siddhiAppRuntime.shutdown();

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

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

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.GREATER_THAN_EQUAL, Expression.value(70f))));
    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", 50f, 60d});
    inputHandler.send(new Object[]{"WSO2", 70f, 40d});
    inputHandler.send(new Object[]{"WSO2", 44f, 200d});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example #16
Source File: InMemoryTransportTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(dependsOnMethods = {"inMemorySourceSinkAndEventMappingWithSiddhiQLAndRef"})
public void inMemoryTestCase10() throws InterruptedException, SubscriberUnAvailableException {
    log.info("Test inMemory 10");

    String streams = "" +
            "@app:name('TestSiddhiApp')" +
            "@source(type='testTrpInMemory', topic='Foo', prop1='hi', prop2='7.5', " +
            "   @map(type='testTrp', @attributes(symbol='trp:symbol'," +
            "        volume='2',price='trp:price'))) " +
            "define stream FooStream (symbol string, price double, volume long); " +
            "define stream BarStream (symbol string, price double, 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);
            wso2Count.incrementAndGet();
            for (Event event : events) {
                AssertJUnit.assertArrayEquals(event.getData(), new Object[]{"hi", 7.5, 100L});
            }
        }
    });
    siddhiAppRuntime.start();
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[]{"WSO2", 5.5, 100L}));
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[]{"IBM", 5.5, 100L}));
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[]{"WSO2", 5.5, 100L}));
    Thread.sleep(100);

    //assert event count
    AssertJUnit.assertEquals("Number of events", 3, wso2Count.get());
    siddhiAppRuntime.shutdown();
}
 
Example #17
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery79() throws InterruptedException {
    log.info("Filter test79");

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

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.LESS_THAN_EQUAL,
            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, 60L, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 60L, 2});
    inputHandler.send(new Object[]{"WSO2", 60f, 300L, 4});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    siddhiAppRuntime.shutdown();
}
 
Example #18
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery92() throws InterruptedException {
    log.info("Filter test92");

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

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.LESS_THAN, Expression.value(100d))));
    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", 50f, 60L, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 40L, 10});
    inputHandler.send(new Object[]{"WSO2", 44f, 200L, 56});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    AssertJUnit.assertEquals(2, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example #19
Source File: QuerySyncTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void querySyncTest3() throws InterruptedException {
    log.info("querySync test3");

    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream cseEventStream (symbol string, price float, volume int); " +
            "define stream twitterStream (user string, tweet string, company string); ";
    String query = "" +
            "@info(name = 'query1') " +
            "@synchronized('true') " +
            "from cseEventStream#window.time(1 sec) as a join twitterStream#window.time(1 sec) as b " +
            "on a.symbol== b.company " +
            "select a.symbol as symbol, b.tweet, a.price " +
            "insert all events into outputStream ;";

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

        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
        siddhiAppRuntime.start();
        cseEventStreamHandler.send(new Object[]{"WSO2", 55.6f, 100});
        twitterStreamHandler.send(new Object[]{"User1", "Hello World", "WSO2"});
        cseEventStreamHandler.send(new Object[]{"IBM", 75.6f, 100});
        Thread.sleep(500);
        cseEventStreamHandler.send(new Object[]{"WSO2", 57.6f, 100});

        SiddhiTestHelper.waitForEvents(100, 2, inEventCount, 60000);
        SiddhiTestHelper.waitForEvents(100, 2, removeEventCount, 60000);
        AssertJUnit.assertEquals(2, inEventCount.get());
        AssertJUnit.assertEquals(2, removeEventCount.get());
        AssertJUnit.assertTrue(eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
 
Example #20
Source File: LossyFrequentWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testLossyFrequentUniqueWindow1() throws InterruptedException {
    log.info("LossyFrequentWindow test1");

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "" +
            "define stream purchase (cardNo string, price float); " +
            "define window purchaseWindow (cardNo string, price float) lossyFrequent(0.1,0.01); ";
    String query = "" +
            "@info(name = 'query0') " +
            "from purchase[price >= 30] " +
            "insert into purchaseWindow; " +
            "" +
            "@info(name = 'query1') " +
            "from purchaseWindow " +
            "select cardNo, price " +
            "insert all events into PotentialFraud ;";

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

    });

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

    for (int i = 0; i < 25; i++) {
        inputHandler.send(new Object[]{"3234-3244-2432-4124", 73.36f});
        inputHandler.send(new Object[]{"1234-3244-2432-123", 46.36f});
        inputHandler.send(new Object[]{"5768-3244-2432-5646", 48.36f});
        inputHandler.send(new Object[]{"9853-3244-2432-4125", 78.36f});
    }
    inputHandler.send(new Object[]{"1124-3244-2432-4126", 78.36f});     // these events will not be picked
    inputHandler.send(new Object[]{"1124-3244-2432-4126", 78.36f});

    Thread.sleep(1000);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("In Event count", 100, inEventCount);
    AssertJUnit.assertEquals("Out Event count", 0, removeEventCount);

    siddhiAppRuntime.shutdown();

}
 
Example #21
Source File: QueryAPITestCaseForTableWithCache.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void test1() throws InterruptedException {
    log.info("Test1 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[]{"WSO3", 57.6f, 100L});
    Thread.sleep(500);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);

    events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "on price > 75 ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);

    events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "on price > volume*3/4  ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);

    siddhiAppRuntime.shutdown();

}
 
Example #22
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testFilterQuery20() throws InterruptedException {
    log.info("Filter test20");

    SiddhiManager siddhiManager = new SiddhiManager();

    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.LONG);

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

    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.assertEquals(10L, ((Long) inEvents[0].getData(2)).longValue());
            count.addAndGet(inEvents.length);
        }

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

    inputHandler.send(new Object[]{"WSO2", 55.6f, 103L});
    inputHandler.send(new Object[]{"WSO2", 57.6f, 10L});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();


}
 
Example #23
Source File: PatternPartitionTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testPatternPartitionQuery12() 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 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>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 ;";
    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();
                }
                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.get());
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
    AssertJUnit.assertEquals("Event arrived", false, eventArrived);

    siddhiAppRuntime.shutdown();
}
 
Example #24
Source File: PartitionTestCase1.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public void testPartitionQuery43() throws InterruptedException {
    log.info("Partition test43");

    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiApp siddhiApp = new SiddhiApp("plan43");

    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);

    siddhiApp.defineStream(streamDefinition);

    Partition partition = Partition.partition().
            with("cseEventStream", Expression.variable("symbol"));

    Query query = Query.query();
    query.from(InputStream.stream("cseEventStream"));
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("price", Expression.variable("price")).
                    select("volume", Expression.variable("volume"))
    );
    query.insertIntoInner("StockStream", OutputStream.OutputEventType.CURRENT_EVENTS);

    Query query1 = Query.query();
    query1.from(InputStream.innerStream("StockStream"));
    query1.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("price", Expression.variable("price")).
                    select("volume", Expression.variable("volume"))
    );
    query1.insertInto("OutStockStream", OutputStream.OutputEventType.CURRENT_EVENTS);

    partition.addQuery(query);
    partition.addQuery(query1);

    siddhiApp.addPartition(partition);

    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");
    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});
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(4, count.get());
}
 
Example #25
Source File: ExternalTimeBatchWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testExternalTimeBatchWindow17() throws InterruptedException {
    log.info("ExternalTimeBatchWindow test17");

    SiddhiManager siddhiManager = new SiddhiManager();

    String inputStream = "define stream inputStream(currentTime long,value int); " +
            "define window inputWindow(currentTime long,value int) externalTimeBatch(currentTime,5 sec,1200); ";
    String query = " " +
            "@info(name = 'query0') " +
            "from inputStream " +
            "insert into inputWindow; " +
            "" +
            "@info(name='query') " +
            "from inputWindow " +
            "select value " +
            "insert into outputStream; ";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inputStream + query);
    siddhiAppRuntime.addCallback("query", new QueryCallback() {
        int count = 0;

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (count == 0) {
                assertEquals(0L, inEvents[0].getData(0));
                assertEquals(11L, inEvents[inEvents.length - 1].getData(0));
            }
            if (count == 1) {
                assertEquals(12L, inEvents[0].getData(0));
            }
            count += 1;

        }
    });

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

    for (long i = 0; i < 10000; i += 100) {
        inputHandler.send(new Object[]{i + 10000, i / 100});
        Thread.sleep(200);
    }

}
 
Example #26
Source File: LengthBatchWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void lengthBatchWindowTest9() throws InterruptedException {

    log.info("LengthBatchWindow Test9");

    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream cseEventStream (symbol string, price float, volume int); " +
            "define stream twitterStream (user string, tweet string, company string); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from cseEventStream#window.lengthBatch(2) join twitterStream#window.lengthBatch(2) " +
            "on cseEventStream.symbol== twitterStream.company " +
            "select cseEventStream.symbol as symbol, twitterStream.tweet, cseEventStream.price " +
            "insert into outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {
            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {

                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    inEventCount += (inEvents.length);
                }
                if (removeEvents != null) {
                    removeEventCount += (removeEvents.length);
                }
                eventArrived = true;
            }
        });
        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
        siddhiAppRuntime.start();
        cseEventStreamHandler.send(new Object[]{"WSO2", 55.6f, 100});
        cseEventStreamHandler.send(new Object[]{"IBM", 59.6f, 100});
        twitterStreamHandler.send(new Object[]{"User1", "Hello World", "WSO2"});
        twitterStreamHandler.send(new Object[]{"User2", "Hello World2", "WSO2"});
        cseEventStreamHandler.send(new Object[]{"IBM", 75.6f, 100});
        cseEventStreamHandler.send(new Object[]{"WSO2", 57.6f, 100});
        AssertJUnit.assertEquals(4, inEventCount);
        AssertJUnit.assertEquals(0, removeEventCount);
        AssertJUnit.assertTrue(eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
 
Example #27
Source File: PartitionTestCase1.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testPartitionQuery25() throws InterruptedException {
    log.info("Partition test");
    SiddhiManager siddhiManager = new SiddhiManager();

    String siddhiApp = "@app:name('PartitionTest') " +
            "define stream streamA (symbol string,  price int);" +
            "" +
            "from streamA#window.lengthBatch(3) " +
            "insert into streamB;" +
            "" +
            "partition with (symbol of streamB) " +
            "begin " +
            "@info(name = 'query1') " +
            "from streamB  " +
            "select symbol, price insert into StockQuote ;  " +
            "end ";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    StreamCallback streamCallback = new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            AssertJUnit.assertTrue("IBM".equals(events[0].getData(0)) || "WSO2".equals(events[0].getData(0)));
            count.addAndGet(events.length);
            eventArrived = true;
        }
    };
    siddhiAppRuntime.addCallback("StockQuote", streamCallback);

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("streamA");
    siddhiAppRuntime.start();
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"IBM", 700}));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"WSO2", 60}));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"WSO2", 60}));
    SiddhiTestHelper.waitForEvents(100, 3, count, 60000);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(3, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example #28
Source File: PrimaryKeyTableTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void primaryKeyTableTest17() throws InterruptedException {
    log.info("primaryKeyTableTest17");

    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define stream CheckStockStream (symbol string, volume long); " +
            "define stream DeleteStockStream (symbol string, price float, 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 DeleteStockStream " +
            "delete StockTable " +
            "   on StockTable.volume>volume;" +
            "" +
            "@info(name = 'query3') " +
            "from CheckStockStream join StockTable " +
            "select StockTable.symbol, StockTable.volume " +
            "insert into OutStream;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query3", 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");
        InputHandler deleteStockStream = siddhiAppRuntime.getInputHandler("DeleteStockStream");

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

        List<Object[]> expected1 = Arrays.asList(
                new Object[]{"IBM", 100L},
                new Object[]{"WSO2", 200L}
        );

        List<Object[]> expected2 = new ArrayList<Object[]>();
        expected2.add(new Object[]{"IBM", 100L});

        SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 60000);
        AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isUnsortedEventsMatch(inEventsList
                .subList(0, 2), expected1));
        AssertJUnit.assertEquals("In events matched", true,
                SiddhiTestHelper.isEventsMatch(inEventsList.subList(2, 3), expected2));
        AssertJUnit.assertEquals("Number of success events", 3, inEventCount.get());
        AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
        AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
 
Example #29
Source File: TimeLengthWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void timeLengthWindowTest6() throws InterruptedException {
    log.info("Testing timeLength window with no of events greater than window length and time period greater than" +
            " window time");

    SiddhiManager siddhiManager = new SiddhiManager();

    String sensorStream = "define stream sensorStream (id string, sensorValue int);";
    String query = "" +
            "@info(name = 'query1') " +
            "from sensorStream#window.timeLength(3 sec, 6) " +
            "select id, sum(sensorValue) as sum " +
            "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(sensorStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);

            if (inEvents != null) {
                if (inEvents[0].getData(0).toString().equals("id6")) {
                    AssertJUnit.assertEquals("6", inEvents[0].getData(1).toString());
                }
                if (inEvents[0].getData(0).toString().equals("id7")) {
                    AssertJUnit.assertEquals("6", inEvents[0].getData(1).toString());
                }
                if (inEvents[0].getData(0).toString().equals("id8")) {
                    AssertJUnit.assertEquals("6", inEvents[0].getData(1).toString());
                }
                inEventCount++;
            }

            if (removeEvents != null) {
                if (removeEvents[0].getData(0).toString().equals("id1")) {
                    AssertJUnit.assertEquals("5", removeEvents[0].getData(1).toString());
                }
                if (removeEvents[0].getData(0).toString().equals("id2")) {
                    AssertJUnit.assertEquals("5", removeEvents[0].getData(1).toString());
                }
                if (removeEvents[0].getData(0).toString().equals("id3")) {
                    AssertJUnit.assertEquals("5", removeEvents[0].getData(1).toString());
                }
                removeEventCount++;
            }
            eventArrived = true;
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("sensorStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"id1", 1});
    Thread.sleep(520);
    inputHandler.send(new Object[]{"id2", 1});
    Thread.sleep(520);
    inputHandler.send(new Object[]{"id3", 1});
    Thread.sleep(520);
    inputHandler.send(new Object[]{"id4", 1});
    Thread.sleep(520);
    inputHandler.send(new Object[]{"id5", 1});
    Thread.sleep(520);
    inputHandler.send(new Object[]{"id6", 1});
    Thread.sleep(520);
    inputHandler.send(new Object[]{"id7", 1});
    Thread.sleep(520);
    inputHandler.send(new Object[]{"id8", 1});

    Thread.sleep(500);

    AssertJUnit.assertEquals(8, inEventCount);
    AssertJUnit.assertEquals(3, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example #30
Source File: TimeBatchWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = SiddhiAppCreationException.class)
public void timeWindowBatchTest13() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" +
            "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" +
            "@info(name = 'query1') " +
            "from cseEventStream#window.timeBatch(2 sec , 'string') " +
            "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();
}