Search in sources :

Example 1 with UnitTestAppender

use of io.siddhi.core.UnitTestAppender 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 2 with UnitTestAppender

use of io.siddhi.core.UnitTestAppender 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 3 with UnitTestAppender

use of io.siddhi.core.UnitTestAppender 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)

Example 4 with UnitTestAppender

use of io.siddhi.core.UnitTestAppender in project siddhi by wso2.

the class FaultStreamTestCase method faultStreamTest10.

@Test(dependsOnMethods = "faultStreamTest9")
public void faultStreamTest10() throws InterruptedException {
    log.info("faultStreamTest10-Tests fault handling when it's set to stream at Sink. " + "The events will be available in the corresponding fault stream.");
    UnitTestAppender appender = new UnitTestAppender("UnitTestAppender", null);
    Logger loggerSink = (Logger) LogManager.getLogger(Sink.class);
    Logger loggerStreamJunction = (Logger) LogManager.getLogger(StreamJunction.class);
    loggerSink.addAppender(appender);
    loggerStreamJunction.addAppender(appender);
    appender.start();
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@OnError(action='stream')" + "define stream cseEventStream (symbol string, price float, volume long);" + "\n" + "@OnError(action='stream')" + "@sink(type='inMemory', topic='{{symbol}}', on.error='stream', @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(3) != null);
            eventArrived = true;
            count.incrementAndGet();
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    try {
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    inputHandler.send(new Object[] { "IBM", 0f, 100L });
                } catch (InterruptedException e) {
                }
            }
        };
        thread.start();
        Thread.sleep(500);
        AssertJUnit.assertTrue(((UnitTestAppender) loggerSink.getAppenders().get("UnitTestAppender")).getMessages() == null && ((UnitTestAppender) loggerStreamJunction.getAppenders().get("UnitTestAppender")).getMessages() == null);
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        loggerSink.removeAppender(appender);
        loggerStreamJunction.removeAppender(appender);
        siddhiAppRuntime.shutdown();
    }
    Assert.assertTrue(eventArrived);
    Assert.assertEquals(count.get(), 1);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) UnitTestAppender(io.siddhi.core.UnitTestAppender) Logger(org.apache.logging.log4j.core.Logger) 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 5 with UnitTestAppender

use of io.siddhi.core.UnitTestAppender in project siddhi by wso2.

the class FaultStreamTestCase method faultStreamTest3.

@Test(dependsOnMethods = "faultStreamTest2")
public void faultStreamTest3() throws InterruptedException {
    log.info("faultStreamTest3-Tests fault handling when it's set to stream. " + "No errors would be logged since exceptions are being gracefully handled.");
    UnitTestAppender appender = new UnitTestAppender("UnitTestAppender", null);
    Logger logger = (Logger) LogManager.getLogger(StreamJunction.class);
    logger.addAppender(appender);
    appender.start();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:fault", FaultFunctionExtension.class);
    String siddhiApp = "" + "@OnError(action='stream')" + "define stream cseEventStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from cseEventStream[custom:fault() > volume] " + "select symbol, price, symbol as sym1 " + "insert into outputStream ;";
    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);
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    try {
        inputHandler.send(new Object[] { "IBM", 0f, 100L });
        AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages() == null);
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        logger.removeAppender(appender);
        siddhiAppRuntime.shutdown();
    }
    AssertJUnit.assertEquals(0, count.get());
    AssertJUnit.assertFalse(eventArrived);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) UnitTestAppender(io.siddhi.core.UnitTestAppender) Logger(org.apache.logging.log4j.core.Logger) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)23 SiddhiManager (io.siddhi.core.SiddhiManager)23 UnitTestAppender (io.siddhi.core.UnitTestAppender)23 InputHandler (io.siddhi.core.stream.input.InputHandler)23 Logger (org.apache.logging.log4j.core.Logger)23 Test (org.testng.annotations.Test)23 Event (io.siddhi.core.event.Event)18 StreamCallback (io.siddhi.core.stream.output.StreamCallback)12 LoggerContext (org.apache.logging.log4j.core.LoggerContext)8 Configuration (org.apache.logging.log4j.core.config.Configuration)8 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)5 InMemoryBroker (io.siddhi.core.util.transport.InMemoryBroker)4 SessionWindowProcessor (io.siddhi.core.query.processor.stream.window.SessionWindowProcessor)2 Sink (io.siddhi.core.stream.output.sink.Sink)2 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)1 StoreQueryCreationException (io.siddhi.core.exception.StoreQueryCreationException)1 StreamJunction (io.siddhi.core.stream.StreamJunction)1 Scheduler (io.siddhi.core.util.Scheduler)1 SiddhiParserException (io.siddhi.query.compiler.exception.SiddhiParserException)1