Search in sources :

Example 11 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class LenghtBatchWindowTestCase method testLengthBatchWindow6.

@Test
public void testLengthBatchWindow6() throws InterruptedException {
    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(4); ";
    String query = "@info(name = 'query1') from cseEventStream select symbol,price,volume insert into cseWindow; " + "@info(name = 'query2') from cseWindow " + "select symbol,sum(price) as sumPrice,volume " + "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) {
                AssertJUnit.assertEquals("Events cannot be expired", false, event.isExpired());
                inEventCount++;
                if (inEventCount == 1) {
                    AssertJUnit.assertEquals(100.0, event.getData(1));
                } else if (inEventCount == 2) {
                    AssertJUnit.assertEquals(240.0, event.getData(1));
                }
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 10f, 0 });
    inputHandler.send(new Object[] { "WSO2", 20f, 1 });
    inputHandler.send(new Object[] { "IBM", 30f, 0 });
    inputHandler.send(new Object[] { "WSO2", 40f, 1 });
    inputHandler.send(new Object[] { "IBM", 50f, 0 });
    inputHandler.send(new Object[] { "WSO2", 60f, 1 });
    inputHandler.send(new Object[] { "WSO2", 60f, 1 });
    inputHandler.send(new Object[] { "IBM", 70f, 0 });
    inputHandler.send(new Object[] { "WSO2", 80f, 1 });
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(2, inEventCount);
    AssertJUnit.assertTrue(eventArrived);
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 12 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery10.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery9" })
public void testSnapshotOutputRateLimitQuery10() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test10");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest10') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(2 sec) " + "select  ip " + "output snapshot every 2 sec " + "insert all events into uniqueIps ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    log.info("Running : " + siddhiAppRuntime.getName());
    siddhiAppRuntime.addCallback("uniqueIps", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            for (Event event : events) {
                if (event.isExpired()) {
                    AssertJUnit.fail("Remove events emitted");
                } else {
                    count.incrementAndGet();
                    AssertJUnit.assertTrue("192.10.1.5".equals(event.getData(0)) || "192.10.1.3".equals(event.getData(0)));
                }
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3" });
    Awaitility.await().atMost(60, TimeUnit.SECONDS).until(() -> eventArrived && count.get() == 2);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event value", 2, count.get());
    siddhiAppRuntime.shutdown();
    Thread.sleep(2000);
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 13 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery12.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery11" })
public void testSnapshotOutputRateLimitQuery12() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test12");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest12') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(1 sec) " + "select  ip " + "output snapshot every 1 sec " + "insert all events into uniqueIps ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    log.info("Running : " + siddhiAppRuntime.getName());
    siddhiAppRuntime.addCallback("uniqueIps", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.incrementAndGet();
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    Thread.sleep(100);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3" });
    Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> eventArrived && count.get() == 1);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event value", 1, count.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 14 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery6.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery5" })
public void testSnapshotOutputRateLimitQuery6() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test6");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest6') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(1 sec) " + "select sum(calls) as totalCalls " + "group by ip " + "output snapshot every 1 sec " + "insert all events into uniqueIps ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    log.info("Running : " + siddhiAppRuntime.getName());
    siddhiAppRuntime.addCallback("uniqueIps", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            count.incrementAndGet();
            value += events.length;
            if (count.get() == 1) {
                AssertJUnit.assertTrue((Long) events[0].getData(0) == 3L || (Long) events[1].getData(0) == 6L);
            } else if (count.get() == 2) {
                AssertJUnit.assertTrue((Long) events[0].getData(0) == 2L || (Long) events[1].getData(0) == 10L);
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    Thread.sleep(1100);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5", 3 });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3", 6 });
    Thread.sleep(2200);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5", 2 });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3", 10 });
    Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> eventArrived && count.get() == 2 && value == 4);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event bundles", 2, count.get());
    AssertJUnit.assertTrue("Number of output events", 4 == value);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 15 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery14.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery13" })
public void testSnapshotOutputRateLimitQuery14() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test14");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest14') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(2 sec) " + "select  ip " + "output snapshot every 2 sec " + "insert all events into uniqueIps ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    log.info("Running : " + siddhiAppRuntime.getName());
    siddhiAppRuntime.addCallback("uniqueIps", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            for (Event event : events) {
                if (event.isExpired()) {
                    AssertJUnit.fail("Remove events emitted");
                } else {
                    count.incrementAndGet();
                    AssertJUnit.assertTrue("192.10.1.3".equals(event.getData(0)) || "192.10.1.5".equals(event.getData(0)));
                }
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5" });
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3" });
    Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> eventArrived && count.get() == 2);
    AssertJUnit.assertTrue("Event arrived", (eventArrived && count.get() == 2));
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)209 Test (org.testng.annotations.Test)204 Event (org.ballerinalang.siddhi.core.event.Event)203 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)201 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)201 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)190 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)13 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)6 Partition (org.ballerinalang.siddhi.query.api.execution.partition.Partition)6 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)6 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)5 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CannotRestoreSiddhiAppStateException (org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException)2 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)1 ProducerType (com.lmax.disruptor.dsl.ProducerType)1 Constructor (java.lang.reflect.Constructor)1