Search in sources :

Example 6 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class AsyncTestCase method asyncTest6.

@Test(dependsOnMethods = { "asyncTest5" })
public void asyncTest6() throws InterruptedException {
    log.info("async test 6");
    HashMap<String, Integer> threads = new HashMap<>();
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + " " + "@async(buffer.size='16', workers='2', batch.size.max='25')" + "define stream cseEventStream (symbol string, price float, volume int);" + "" + "@info(name = 'query1') " + "from cseEventStream[70 < price] " + "select * " + "insert into innerStream ;" + "" + "@info(name = 'query2') " + "from innerStream[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);
            try {
                Thread.sleep(Math.round(Math.random()) * 1000 + 1000);
            } catch (InterruptedException e) {
                log.error(e.getMessage(), e);
            }
            eventArrived = true;
            for (Event event : events) {
                count.incrementAndGet();
            }
            Assert.assertTrue(events.length <= 25);
            synchronized (threads) {
                Integer count = threads.get(Thread.currentThread().getName());
                if (count == null) {
                    threads.put(Thread.currentThread().getName(), 1);
                } else {
                    count++;
                    threads.put(Thread.currentThread().getName(), count);
                }
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    for (int i = 0; i < 20; i++) {
        Thread.sleep(100);
        inputHandler.send(new Object[] { "WSO2", 115.6f, 100 + i });
    }
    SiddhiTestHelper.waitForEvents(2000, 20, count, 10000);
    AssertJUnit.assertEquals(20, count.get());
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(eventArrived);
    log.info("Threads count:" + threads.size() + " threads:" + threads);
    Assert.assertEquals(threads.size(), 2);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) HashMap(java.util.HashMap) StreamCallback(io.siddhi.core.stream.output.StreamCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 7 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class AsyncTestCase method asyncTest3.

@Test(dependsOnMethods = { "asyncTest2" })
public void asyncTest3() throws InterruptedException {
    log.info("async test 3");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + " " + "@async(buffer.size='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 innerStream ;" + "" + "@info(name = 'query2') " + "from innerStream[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);
            try {
                Thread.sleep(Math.round(Math.random()) * 1000 + 1000);
            } catch (InterruptedException e) {
                log.error(e.getMessage(), e);
            }
            eventArrived = true;
            for (Event event : events) {
                count.incrementAndGet();
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    long startTime = System.currentTimeMillis();
    inputHandler.send(new Object[] { "WSO2", 55.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 9.6f, 100 });
    inputHandler.send(new Object[] { "FB", 7.6f, 100 });
    inputHandler.send(new Object[] { "GOOG", 5.6f, 100 });
    inputHandler.send(new Object[] { "WSO2", 15.6f, 100 });
    long timeDiff = System.currentTimeMillis() - startTime;
    Thread.sleep(5000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(5, count.get());
    AssertJUnit.assertTrue(timeDiff >= 2000);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 8 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class SandboxTestCase method sandboxTest2.

@Test
public void sandboxTest2() throws InterruptedException {
    log.info("sandbox test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String app = "" + "@source(type='foo')" + "@source(type='foo1')" + "@sink(type='foo1')" + "@source(type='inMemory', topic='myTopic')" + "define stream StockStream (symbol string, price float, vol long);\n" + "" + "@sink(type='foo1')" + "@sink(type='inMemory', topic='myTopic1')" + "define stream DeleteStockStream (symbol string, price float, vol long);\n" + "" + "@store(type='rdbms')" + "define table StockTable (symbol string, price float, volume long);\n" + "" + "define stream CountStockStream (symbol string);\n" + "" + "@info(name = 'query1') " + "from StockStream " + "select symbol, price, vol as volume " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from DeleteStockStream[vol>=100] " + "delete StockTable " + "   on StockTable.symbol==symbol ;" + "" + "@info(name = 'query3') " + "from CountStockStream#window.length(0) join StockTable" + " on CountStockStream.symbol==StockTable.symbol " + "select CountStockStream.symbol as symbol " + "insert into CountResultsStream ;";
    SiddhiApp siddhiApp = SiddhiCompiler.parse(app);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSandboxSiddhiAppRuntime(siddhiApp);
    Assert.assertEquals(siddhiAppRuntime.getSources().size(), 1);
    Assert.assertEquals(siddhiAppRuntime.getSinks().size(), 1);
    Assert.assertEquals(siddhiAppRuntime.getTables().size(), 1);
    for (List<Source> sources : siddhiAppRuntime.getSources()) {
        for (Source source : sources) {
            Assert.assertTrue(source.getType().equalsIgnoreCase("inMemory"));
        }
    }
    for (List<Sink> sinks : siddhiAppRuntime.getSinks()) {
        for (Sink sink : sinks) {
            Assert.assertTrue(sink.getType().equalsIgnoreCase("inMemory"));
        }
    }
    for (Table table : siddhiAppRuntime.getTables()) {
        Assert.assertTrue(table instanceof InMemoryTable);
    }
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler deleteStockStream = siddhiAppRuntime.getInputHandler("DeleteStockStream");
    InputHandler countStockStream = siddhiAppRuntime.getInputHandler("CountStockStream");
    siddhiAppRuntime.addCallback("CountResultsStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
        }
    });
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 75.6f, 100L });
    stockStream.send(new Object[] { "WSO2", 57.6f, 100L });
    deleteStockStream.send(new Object[] { "IBM", 57.6f, 100L });
    countStockStream.send(new Object[] { "WSO2" });
    Thread.sleep(500);
    Assert.assertEquals(count.get(), 2);
    siddhiAppRuntime.shutdown();
}
Also used : InMemoryTable(io.siddhi.core.table.InMemoryTable) InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiApp(io.siddhi.query.api.SiddhiApp) Table(io.siddhi.core.table.Table) InMemoryTable(io.siddhi.core.table.InMemoryTable) Source(io.siddhi.core.stream.input.source.Source) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Sink(io.siddhi.core.stream.output.sink.Sink) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 9 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class StartStopTestCase method startStopTest2.

@Test()
public void startStopTest2() throws InterruptedException {
    log.info("startStop test 2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "define stream cseEventStream (symbol string, price float, volume int);" + "define stream cseEventStream2 (symbol string, price float, volume int);" + "" + "@info(name = 'query1') " + "from cseEventStream " + "select 1 as eventFrom " + "insert into outputStream ;" + "" + "@info(name = 'query2') " + "from cseEventStream2 " + "select 2 as eventFrom " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            Assert.assertEquals(events[0].getData(0), 1);
            eventArrived = true;
            count.incrementAndGet();
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream2");
    inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime.start();
    inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    inputHandler.send(new Object[] { "WSO2", 55.6f, 100 });
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(1, count.get());
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 10 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

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" });
    Thread.sleep(2200);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event value", 1, count.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

StreamCallback (io.siddhi.core.stream.output.StreamCallback)311 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)305 SiddhiManager (io.siddhi.core.SiddhiManager)305 Event (io.siddhi.core.event.Event)303 Test (org.testng.annotations.Test)295 InputHandler (io.siddhi.core.stream.input.InputHandler)289 StreamEvent (io.siddhi.core.event.stream.StreamEvent)16 ComplexEvent (io.siddhi.core.event.ComplexEvent)13 UnitTestAppender (io.siddhi.core.UnitTestAppender)12 Logger (org.apache.logging.log4j.core.Logger)12 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)10 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)9 SiddhiApp (io.siddhi.query.api.SiddhiApp)9 Partition (io.siddhi.query.api.execution.partition.Partition)8 Query (io.siddhi.query.api.execution.query.Query)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 PrintStream (java.io.PrintStream)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)7 InMemoryBroker (io.siddhi.core.util.transport.InMemoryBroker)7