Search in sources :

Example 16 with StreamCallback

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

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery8.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery7" })
public void testSnapshotOutputRateLimitQuery8() throws InterruptedException {
    log.info("SnapshotOutputRateLimit test8");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('SnapshotOutputRateLimitTest8') " + "" + "define stream LoginEvents (timestamp long, ip string, calls int);" + "" + "@info(name = 'query1') " + "from LoginEvents#window.time(1 sec) " + "select sum(calls) as totalCalls " + "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();
            if (count.get() == 1) {
                AssertJUnit.assertTrue((Long) events[0].getData(0) == 9L);
            } else if (count.get() == 2) {
                AssertJUnit.assertTrue((Long) events[0].getData(0) == 12L);
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    Thread.sleep(1100);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.5", 3 });
    Thread.sleep(100);
    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 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { System.currentTimeMillis(), "192.10.1.3", 10 });
    Thread.sleep(1200);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Number of output event value", 2, 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)

Example 17 with StreamCallback

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

the class SnapshotOutputRateLimitTestCase method testSnapshotOutputRateLimitQuery6.

@Test(dependsOnMethods = { "testSnapshotOutputRateLimitQuery5_1" })
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 });
    Thread.sleep(1200);
    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(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 18 with StreamCallback

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

the class FaultStreamTestCase method faultStreamTest8.

@Test(dependsOnMethods = "faultStreamTest7")
public void faultStreamTest8() throws InterruptedException {
    log.info("faultStreamTest8-Tests fault handling when it's set to wait. " + "Thread would be waiting until Sink reconnects.");
    UnitTestAppender appender = new UnitTestAppender("UnitTestAppender", null);
    final Logger logger = (Logger) LogManager.getRootLogger();
    logger.setLevel(Level.ALL);
    logger.addAppender(appender);
    appender.start();
    InMemoryBroker.Subscriber subscriptionIBM = new InMemoryBroker.Subscriber() {

        @Override
        public void onMessage(Object msg) {
            EventPrinter.print(new Event[] { (Event) msg });
            count.incrementAndGet();
        }

        @Override
        public String getTopic() {
            return "IBM";
        }
    };
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@OnError(action='stream')" + "define stream cseEventStream (symbol string, price float, volume long);" + "\n" + "@sink(type='inMemory', topic='{{symbol}}', on.error='wait', @map(type='passThrough')) " + "define stream outputStream (symbol string, price float, sym1 string);" + "\n" + "@info(name = 'query1') " + "from cseEventStream " + "select symbol, price , symbol as sym1 " + "insert into outputStream ;" + "";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            Assert.assertTrue(events[0].getData(0) != null);
            countStream.incrementAndGet();
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    try {
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    inputHandler.send(new Object[] { "IBM", 5f, 100L });
                } catch (InterruptedException e) {
                }
            }
        };
        thread.start();
        Thread.sleep(2000);
        Assert.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("error while connecting Sink 'inMemory' at 'outputStream', will retry every"));
        Assert.assertEquals(count.get(), 0);
        Assert.assertEquals(countStream.get(), 0);
        InMemoryBroker.subscribe(subscriptionIBM);
        Thread.sleep(6000);
        Assert.assertEquals(count.get(), 1);
        Assert.assertEquals(countStream.get(), 1);
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        logger.removeAppender(appender);
        siddhiAppRuntime.shutdown();
        InMemoryBroker.unsubscribe(subscriptionIBM);
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) UnitTestAppender(io.siddhi.core.UnitTestAppender) InMemoryBroker(io.siddhi.core.util.transport.InMemoryBroker) Logger(org.apache.logging.log4j.core.Logger) StreamCallback(io.siddhi.core.stream.output.StreamCallback) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 19 with StreamCallback

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

the class FaultStreamTestCase method faultStreamTest13.

@Test(dependsOnMethods = "faultStreamTest12")
public void faultStreamTest13() throws InterruptedException {
    log.info("faultStreamTest13-Tests fault handling when async set to wait. " + "Thread would be waiting until Sink reconnects.");
    UnitTestAppender appender = new UnitTestAppender("UnitTestAppender", null);
    final Logger logger = (Logger) LogManager.getRootLogger();
    logger.setLevel(Level.ALL);
    logger.addAppender(appender);
    appender.start();
    SiddhiManager siddhiManager = new SiddhiManager();
    InMemoryBroker.Subscriber subscriptionIBM = new InMemoryBroker.Subscriber() {

        @Override
        public void onMessage(Object msg) {
            EventPrinter.print(new Event[] { (Event) msg });
            count.incrementAndGet();
        }

        @Override
        public String getTopic() {
            return "IBM";
        }
    };
    InMemoryBroker.subscribe(subscriptionIBM);
    String siddhiApp = "" + "@OnError(action='stream')" + "define stream cseEventStream (symbol string, price float, volume long);" + "\n" + "@sink(type='testAsyncInMemory', topic='{{symbol}}', on.error='wait', @map(type='passThrough')) " + "define stream outputStream (symbol string, price float, sym1 string);" + "\n" + "@info(name = 'query1') " + "from cseEventStream " + "select symbol, price, symbol as sym1 " + "insert into outputStream ;" + "";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            countStream.incrementAndGet();
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    TestAsyncInMemory.fail = true;
    try {
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    inputHandler.send(new Object[] { "IBM", 10f, 100L });
                    Thread.sleep(100);
                    inputHandler.send(new Object[] { "IBM", 11f, 100L });
                } catch (InterruptedException e) {
                }
            }
        };
        thread.start();
        Thread.sleep(2000);
        Assert.assertEquals(countStream.get(), 1);
        Assert.assertEquals(count.get(), 0);
        Assert.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("error while connecting Sink 'testAsyncInMemory'" + " at 'outputStream', will retry every"));
        TestAsyncInMemory.fail = false;
        Thread.sleep(11000);
        Assert.assertEquals(countStream.get(), 2);
        Assert.assertEquals(count.get(), 2);
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        logger.removeAppender(appender);
        siddhiAppRuntime.shutdown();
        InMemoryBroker.unsubscribe(subscriptionIBM);
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) UnitTestAppender(io.siddhi.core.UnitTestAppender) InMemoryBroker(io.siddhi.core.util.transport.InMemoryBroker) Logger(org.apache.logging.log4j.core.Logger) StreamCallback(io.siddhi.core.stream.output.StreamCallback) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 20 with StreamCallback

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

the class FaultStreamTestCase method faultStreamTest14.

@Test(dependsOnMethods = "faultStreamTest13")
public void faultStreamTest14() throws InterruptedException {
    log.info("faultStreamTest14-Tests fault handling when async set to wait. " + "Thread would be waiting until Sink reconnects.");
    UnitTestAppender appender = new UnitTestAppender("UnitTestAppender", null);
    final Logger logger = (Logger) LogManager.getRootLogger();
    logger.setLevel(Level.ALL);
    logger.addAppender(appender);
    appender.start();
    SiddhiManager siddhiManager = new SiddhiManager();
    InMemoryBroker.Subscriber subscriptionIBM = new InMemoryBroker.Subscriber() {

        @Override
        public void onMessage(Object msg) {
            EventPrinter.print(new Event[] { (Event) msg });
            count.incrementAndGet();
        }

        @Override
        public String getTopic() {
            return "IBM";
        }
    };
    InMemoryBroker.subscribe(subscriptionIBM);
    String siddhiApp = "" + "@OnError(action='stream')" + "define stream cseEventStream (symbol string, price float, volume long);" + "\n" + "@sink(type='testAsyncInMemory', topic='{{symbol}}', on.error='wait', @map(type='passThrough')) " + "define stream outputStream (symbol string, price float, sym1 string);" + "\n" + "@info(name = 'query1') " + "from cseEventStream " + "select symbol, price, symbol as sym1 " + "insert into outputStream ;" + "";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            countStream.incrementAndGet();
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    TestAsyncInMemory.failOnce = true;
    try {
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    inputHandler.send(new Object[] { "IBM", 6f, 100L });
                    inputHandler.send(new Object[] { "IBM", 7f, 100L });
                } catch (InterruptedException e) {
                }
            }
        };
        thread.start();
        Thread.sleep(11000);
        Assert.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("Connection unavailable during publishing, " + "error while connecting Sink 'testAsyncInMemory' at 'outputStream', will retry"));
        Assert.assertEquals(count.get(), 2);
        Assert.assertEquals(countStream.get(), 2);
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        logger.removeAppender(appender);
        siddhiAppRuntime.shutdown();
        InMemoryBroker.unsubscribe(subscriptionIBM);
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) UnitTestAppender(io.siddhi.core.UnitTestAppender) InMemoryBroker(io.siddhi.core.util.transport.InMemoryBroker) Logger(org.apache.logging.log4j.core.Logger) StreamCallback(io.siddhi.core.stream.output.StreamCallback) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) 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