Search in sources :

Example 6 with UnitTestAppender

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

the class FaultStreamTestCase method faultStreamTest15.

@Test(dependsOnMethods = "faultStreamTest14")
public void faultStreamTest15() throws InterruptedException {
    log.info("faultStreamTest15-Tests fault handling when async 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='testAsyncInMemory', 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;
            countStream.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(), 0);
    Assert.assertEquals(countStream.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 7 with UnitTestAppender

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

the class FaultStreamTestCase method faultStreamTest2.

@Test(dependsOnMethods = "faultStreamTest1")
public void faultStreamTest2() throws InterruptedException {
    log.info("faultStreamTest2-Tests logging when fault handling is set to log.");
    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();
    siddhiManager.setExtension("custom:fault", FaultFunctionExtension.class);
    String siddhiApp = "" + "@OnError(action='log')" + "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().contains("Error when running faultAdd(). Exception on " + "class 'io.siddhi.core.stream.FaultFunctionExtension'"));
    } 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) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) Logger(org.apache.logging.log4j.core.Logger) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 8 with UnitTestAppender

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

the class LogSinkTest method testWithAllOptions.

@Test
public void testWithAllOptions() throws Exception {
    LoggerContext context = LoggerContext.getContext(false);
    Configuration config = context.getConfiguration();
    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();
    String inputStream = "@App:name(\"HelloWorldApp\")\n" + "define stream CargoStream (weight int);";
    String outputStream = "@sink(type='log', prefix='My Log',  priority='info')\n" + "define stream OutputStream(weight int, totalWeight long);";
    String query = ("@info(name='HelloWorldQuery') " + "from CargoStream " + "select weight, sum(weight) as totalWeight " + "insert into OutputStream;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inputStream + outputStream + query);
    siddhiAppRuntime.start();
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("CargoStream");
    try {
        inputHandler.send(new Object[] { 2 });
        AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("My Log"));
        AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("data=[2, 2]"));
        inputHandler.send(new Object[] { 3 });
        AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("My Log"));
        AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("data=[3, 5]"));
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing with all options", e);
    } finally {
        logger.removeAppender(appender);
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) Configuration(org.apache.logging.log4j.core.config.Configuration) UnitTestAppender(io.siddhi.core.UnitTestAppender) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Logger(org.apache.logging.log4j.core.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 9 with UnitTestAppender

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

the class LogSinkTest method testWithDefaultOptions.

@Test
public void testWithDefaultOptions() throws Exception {
    LoggerContext context = LoggerContext.getContext(false);
    Configuration config = context.getConfiguration();
    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();
    String inputStream = "@App:name(\"HelloWorldApp\")\n" + "define stream CargoStream (weight int);";
    String outputStream = "@sink(type='log')\n" + "define stream OutputStream(weight int, totalWeight long);";
    String query = ("@info(name='HelloWorldQuery') " + "from CargoStream " + "select weight, sum(weight) as totalWeight " + "insert into OutputStream;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inputStream + outputStream + query);
    siddhiAppRuntime.start();
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("CargoStream");
    try {
        inputHandler.send(new Object[] { 2 });
        AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("HelloWorldApp : OutputStream"));
        AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("data=[2, 2]"));
        inputHandler.send(new Object[] { 3 });
        AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("HelloWorldApp : OutputStream"));
        AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("data=[3, 5]"));
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing with default options", e);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) Configuration(org.apache.logging.log4j.core.config.Configuration) UnitTestAppender(io.siddhi.core.UnitTestAppender) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Logger(org.apache.logging.log4j.core.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 10 with UnitTestAppender

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

the class FaultStreamTestCase method faultStreamTest12.

@Test(dependsOnMethods = "faultStreamTest11")
public void faultStreamTest12() throws InterruptedException {
    log.info("faultStreamTest12-Tests fault handling for async when it's set to log. " + "Events would be logged and dropped during publishing failure at Sink");
    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();
    String siddhiApp = "" + "@OnError(action='stream')" + "define stream cseEventStream (symbol string, price float, volume long);" + "\n" + "@sink(type='testAsyncInMemory', topic='{{symbol}}', on.error='log', @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);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    try {
        inputHandler.send(new Object[] { "IBM", 0f, 100L });
        Thread.sleep(6000);
        AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("Dropping event at Sink 'testAsyncInMemory' at"));
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        logger.removeAppender(appender);
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) UnitTestAppender(io.siddhi.core.UnitTestAppender) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) Logger(org.apache.logging.log4j.core.Logger) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) 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