Search in sources :

Example 71 with StreamCallback

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

the class TablePartitionTestCase method testPartitionQuery1.

@Test
public void testPartitionQuery1() throws InterruptedException {
    log.info("Table Partition test 1");
    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;  " + "" + "   @info(name = 'query2') " + "   from streamB[(symbol==tableA.symbol) in tableA] " + "   select symbol " + "   insert into outputStream;  " + "end ";
    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);
}
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 72 with StreamCallback

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

the class WindowPartitionTestCase method testWindowPartitionQuery3.

@Test
public void testWindowPartitionQuery3() throws InterruptedException {
    log.info("Window Partition test3");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "define stream cseEventStream (symbol string, price float,volume int);" + "" + "partition with (symbol of cseEventStream) " + "begin " + "" + "@info(name = 'query1') " + "from cseEventStream#window.time(1 sec)  " + "select symbol, default( sum(price), 0.0) as price, volume " + "insert all events into OutStockStream ;  " + "end ";
    SiddhiAppRuntime executionRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    executionRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            for (Event event : events) {
                if (event.getData()[0].equals("WSO2")) {
                    inEventCount++;
                    if (inEventCount == 1) {
                        AssertJUnit.assertEquals(700.0, event.getData()[1]);
                    } else if (inEventCount == 2) {
                        AssertJUnit.assertEquals(0.0, event.getData()[1]);
                    } else if (inEventCount == 3) {
                        AssertJUnit.assertEquals(1000.0, event.getData()[1]);
                    } else if (inEventCount == 4) {
                        AssertJUnit.assertEquals(0.0, event.getData()[1]);
                    }
                } else {
                    removeEventCount++;
                    if (removeEventCount == 1) {
                        AssertJUnit.assertEquals(70.0, event.getData()[1]);
                    } else if (removeEventCount == 2) {
                        AssertJUnit.assertEquals(170.0, event.getData()[1]);
                    } else if (removeEventCount == 3) {
                        AssertJUnit.assertEquals(100.0, event.getData()[1]);
                    } else if (removeEventCount == 4) {
                        AssertJUnit.assertEquals(0.0, event.getData()[1]);
                    } else if (removeEventCount == 5) {
                        AssertJUnit.assertEquals(200.0, event.getData()[1]);
                    } else if (removeEventCount == 6) {
                        AssertJUnit.assertEquals(0.0, event.getData()[1]);
                    }
                }
            }
        }
    });
    InputHandler inputHandler = executionRuntime.getInputHandler("cseEventStream");
    executionRuntime.start();
    inputHandler.send(new Object[] { "IBM", 70f, 100 });
    inputHandler.send(new Object[] { "WSO2", 700f, 100 });
    inputHandler.send(new Object[] { "IBM", 100f, 200 });
    Thread.sleep(3000);
    inputHandler.send(new Object[] { "IBM", 200f, 300 });
    inputHandler.send(new Object[] { "WSO2", 1000f, 100 });
    Thread.sleep(2000);
    executionRuntime.shutdown();
    AssertJUnit.assertTrue(inEventCount == 4);
    AssertJUnit.assertTrue(removeEventCount == 6);
    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 73 with StreamCallback

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

the class WindowPartitionTestCase method testWindowPartitionQuery2.

@Test
public void testWindowPartitionQuery2() throws InterruptedException {
    log.info("Window Partition test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "define stream cseEventStream (symbol string, price float,volume int);" + "partition with (symbol of cseEventStream) begin @info(name = 'query1') from cseEventStream#window" + ".lengthBatch(2)  select symbol,sum(price) as price,volume insert all events into OutStockStream ;  " + "end ";
    SiddhiAppRuntime executionRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    executionRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                inEventCount++;
                eventArrived = true;
                if (inEventCount == 1) {
                    AssertJUnit.assertEquals(170.0, event.getData()[1]);
                } else if (inEventCount == 2) {
                    AssertJUnit.assertEquals(1700.0, event.getData()[1]);
                }
            }
        }
    });
    InputHandler inputHandler = executionRuntime.getInputHandler("cseEventStream");
    executionRuntime.start();
    inputHandler.send(new Object[] { "IBM", 70f, 100 });
    inputHandler.send(new Object[] { "WSO2", 700f, 100 });
    inputHandler.send(new Object[] { "IBM", 100f, 100 });
    inputHandler.send(new Object[] { "IBM", 200f, 100 });
    inputHandler.send(new Object[] { "WSO2", 1000f, 100 });
    Thread.sleep(2000);
    AssertJUnit.assertEquals(2, inEventCount);
    executionRuntime.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 74 with StreamCallback

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

the class WindowPartitionTestCase method testWindowPartitionQuery5.

@Test
public void testWindowPartitionQuery5() throws InterruptedException {
    log.info("Window Partition test5");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "define stream cseEventStream (symbol string, price double,volume int);" + "" + "partition with (symbol of cseEventStream) " + "begin " + "   @info(name = 'query1') " + "   from cseEventStream#window.timeBatch(5 sec)  " + "   select symbol, sum(price) as price, volume " + "   insert into OutStockStream ;  " + "end ";
    SiddhiAppRuntime executionRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    executionRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                if (event.isExpired()) {
                    removeEventCount++;
                } else {
                    inEventCount++;
                    if ("IBM".equals(event.getData()[0])) {
                        AssertJUnit.assertEquals(370.0, event.getData()[1]);
                    } else if ("WSO2".equals(event.getData()[0])) {
                        AssertJUnit.assertEquals(2200.0, event.getData()[1]);
                    } else if ("ORACLE".equals(event.getData()[0])) {
                        AssertJUnit.assertEquals(75.6, event.getData()[1]);
                    }
                }
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = executionRuntime.getInputHandler("cseEventStream");
    executionRuntime.start();
    inputHandler.send(new Object[] { "IBM", 70.0, 100 });
    inputHandler.send(new Object[] { "WSO2", 700.0, 100 });
    inputHandler.send(new Object[] { "IBM", 100.0, 100 });
    inputHandler.send(new Object[] { "IBM", 200.0, 100 });
    inputHandler.send(new Object[] { "ORACLE", 75.6, 100 });
    inputHandler.send(new Object[] { "WSO2", 1000.0, 100 });
    inputHandler.send(new Object[] { "WSO2", 500.0, 100 });
    Thread.sleep(7000);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertTrue(7 >= inEventCount);
    AssertJUnit.assertEquals(0, removeEventCount);
    executionRuntime.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 75 with StreamCallback

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

the class StatisticsTestCase method statisticsTest1.

@Test
public void statisticsTest1() throws InterruptedException {
    log.info("statistics test 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:statistics(reporter = 'console', interval = '2' )" + " " + "define stream cseEventStream (symbol string, price float, volume int);" + "define stream cseEventStream2 (symbol string, price float, volume int);" + "" + "@info(name = 'query1') " + "from cseEventStream[70 > price] " + "select * " + "insert into outputStream ;" + "" + "@info(name = 'query2') " + "from cseEventStream[volume > 90] " + "select * " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            for (Event event : events) {
                count++;
                AssertJUnit.assertTrue("IBM".equals(event.getData(0)) || "WSO2".equals(event.getData(0)));
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    PrintStream old = System.out;
    System.setOut(ps);
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 55.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(3010);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(3, count);
    System.out.flush();
    String output = baos.toString();
    AssertJUnit.assertTrue(output.contains("Gauges"));
    AssertJUnit.assertTrue(output.contains("org.ballerinalang.siddhi." + SiddhiConstants.METRIC_INFIX_SIDDHI_APPS));
    AssertJUnit.assertTrue(output.contains("query1.memory"));
    AssertJUnit.assertTrue(output.contains("Meters"));
    AssertJUnit.assertTrue(output.contains(SiddhiConstants.METRIC_INFIX_SIDDHI + SiddhiConstants.METRIC_DELIMITER + SiddhiConstants.METRIC_INFIX_STREAMS + SiddhiConstants.METRIC_DELIMITER + "cseEventStream"));
    AssertJUnit.assertTrue(output.contains("Timers"));
    AssertJUnit.assertTrue(output.contains("query1.latency"));
    log.info(output);
    System.setOut(old);
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) PrintStream(java.io.PrintStream) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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