Search in sources :

Example 11 with UnitTestAppender

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

the class FaultStreamTestCase method faultStreamTest5.

@Test(dependsOnMethods = "faultStreamTest4")
public void faultStreamTest5() throws InterruptedException {
    log.info("faultStreamTest5-Tests fault handling when it's set to stream. " + "Events would be available in the corresponding fault stream");
    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("!cseEventStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            Assert.assertTrue(events[0].getData(3) != null);
            count.addAndGet(events.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(1, count.get());
    AssertJUnit.assertTrue(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) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 12 with UnitTestAppender

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

the class FaultStreamTestCase method faultStreamTest6.

@Test(dependsOnMethods = "faultStreamTest5")
public void faultStreamTest6() throws InterruptedException {
    log.info("faultStreamTest6-Tests logging by default when fault handling is not configured " + "explicitly at sink level during publishing failures.");
    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='inMemory', topic='{{symbol}}', @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 });
        AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("Dropping event at Sink 'inMemory' 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)

Example 13 with UnitTestAppender

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

the class Aggregation1TestCase method incrementalStreamProcessorTest16.

@Test(dependsOnMethods = { "incrementalStreamProcessorTest15" })
public void incrementalStreamProcessorTest16() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest16");
    LoggerContext context = LoggerContext.getContext(false);
    Configuration config = context.getConfiguration();
    UnitTestAppender appender = config.getAppender("UnitTestAppender");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp string);";
    String query = "" + "define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, " + "(price * quantity) as lastTradeValue  " + "group by symbol " + "aggregate by timestamp every sec...year  ; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();
    Logger logger = (Logger) LogManager.getLogger(StreamJunction.class);
    logger.addAppender(appender);
    try {
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, "June 1, 2017 4:05:50 AM" });
        AssertJUnit.assertTrue(appender.getMessages().contains("'June 1, 2017 4:05:50 AM' doesn't match the " + "supported formats <yyyy>-<MM>-<dd> <HH>:<mm>:<ss> (for GMT time zone) or <yyyy>-<MM>-<dd> " + "<HH>:<mm>:<ss> <Z> (for non GMT time zone). The ISO 8601 UTC offset must be provided for <Z> " + "(ex. +05:30, -11:00). Exception on class 'io.siddhi.core.executor.incremental." + "IncrementalUnixTimeFunctionExecutor'. Hence, dropping event"));
    } 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) Configuration(org.apache.logging.log4j.core.config.Configuration) UnitTestAppender(io.siddhi.core.UnitTestAppender) StreamJunction(io.siddhi.core.stream.StreamJunction) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Logger(org.apache.logging.log4j.core.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) SiddhiManager(io.siddhi.core.SiddhiManager) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) StoreQueryCreationException(io.siddhi.core.exception.StoreQueryCreationException) SiddhiParserException(io.siddhi.query.compiler.exception.SiddhiParserException) Test(org.testng.annotations.Test)

Example 14 with UnitTestAppender

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

the class Aggregation2TestCase method incrementalStreamProcessorTest57.

@Test(dependsOnMethods = { "incrementalStreamProcessorTest56" })
public void incrementalStreamProcessorTest57() throws InterruptedException {
    LOG.info("Check interrupted exception being thrown when SiddhiRuntime has already been shutdown");
    Logger logger = (Logger) LogManager.getLogger(Scheduler.class);
    LoggerContext context = LoggerContext.getContext(false);
    Configuration config = context.getConfiguration();
    UnitTestAppender appender = config.getAppender("UnitTestAppender");
    logger.addAppender(appender);
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
    String query = " define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue  " + "group by symbol " + "aggregate by timestamp every sec...hour ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();
    Thread eventSenderThread = new Thread(() -> {
        try {
            stockStreamInputHandler.send(new Event[] { new Event(System.currentTimeMillis(), new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L }), new Event(System.currentTimeMillis(), new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L }), new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L }), new Event(System.currentTimeMillis(), new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L }), new Event(System.currentTimeMillis(), new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L }), new Event(System.currentTimeMillis(), new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L }) });
            stockStreamInputHandler.send(new Event[] { new Event(System.currentTimeMillis(), new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L }), new Event(System.currentTimeMillis(), new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L }), new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L }), new Event(System.currentTimeMillis(), new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L }), new Event(System.currentTimeMillis(), new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L }), new Event(System.currentTimeMillis(), new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L }) });
            stockStreamInputHandler.send(new Event[] { new Event(System.currentTimeMillis(), new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L }), new Event(System.currentTimeMillis(), new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L }), new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L }), new Event(System.currentTimeMillis(), new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L }), new Event(System.currentTimeMillis(), new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L }), new Event(System.currentTimeMillis(), new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L }) });
            Thread.sleep(500L);
        } catch (InterruptedException ignored) {
        }
    }, "EventSenderThread");
    eventSenderThread.start();
    Thread siddhiRuntimeShutdownThread = new Thread(siddhiAppRuntime::shutdown, "SiddhiRuntimeShutdownThread");
    siddhiRuntimeShutdownThread.start();
    Thread.sleep(10000L);
    if (appender.getMessages() != null) {
        AssertJUnit.assertFalse(appender.getMessages().contains("Error when adding time:"));
    }
    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) Scheduler(io.siddhi.core.util.Scheduler) Logger(org.apache.logging.log4j.core.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 15 with UnitTestAppender

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

the class SessionWindowTestCase method testSessionWindow17.

@Test(description = "This test case covers when a late event belongs " + "to the current session and other belongs to the previous session and another " + "does not belong to the previous session with allowedLatency time period")
public void testSessionWindow17() throws InterruptedException {
    log.info("SessionWindow Test17: Testing session window, " + "three late events are coming, one belongs to the current session" + "and the another belongs to the previous session and the other " + "does not belong to the previous session");
    LoggerContext context = LoggerContext.getContext(false);
    Configuration config = context.getConfiguration();
    UnitTestAppender appender = config.getAppender("UnitTestAppender");
    SiddhiManager siddhiManager = new SiddhiManager();
    Logger logger = (Logger) LogManager.getLogger(SessionWindowProcessor.class);
    logger.addAppender(appender);
    String purchaseEventStream = "" + "define stream purchaseEventStream (user string, item_number int, price float, quantity int); ";
    String query = "" + "@info(name = 'query0') " + "from purchaseEventStream#window.session(2 sec, user, 1 sec) " + "select * " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(purchaseEventStream + query);
    siddhiAppRuntime.addCallback("query0", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
                count.addAndGet(inEvents.length);
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
                count.addAndGet(removeEvents.length);
                if (removeEventCount == 6) {
                    AssertJUnit.assertTrue("user0".equals(removeEvents[0].getData(0)));
                    AssertJUnit.assertTrue("user0".equals(removeEvents[1].getData(0)));
                    AssertJUnit.assertTrue("user0".equals(removeEvents[2].getData(0)));
                    AssertJUnit.assertTrue("user0".equals(removeEvents[3].getData(0)));
                    AssertJUnit.assertTrue("user0".equals(removeEvents[4].getData(0)));
                    AssertJUnit.assertTrue("user0".equals(removeEvents[5].getData(0)));
                    AssertJUnit.assertEquals(104, removeEvents[0].getData(1));
                    AssertJUnit.assertEquals(101, removeEvents[1].getData(1));
                    AssertJUnit.assertEquals(102, removeEvents[2].getData(1));
                    AssertJUnit.assertEquals(105, removeEvents[3].getData(1));
                    AssertJUnit.assertEquals(103, removeEvents[4].getData(1));
                    AssertJUnit.assertEquals(106, removeEvents[5].getData(1));
                    innerAssertionsPassed = true;
                }
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("purchaseEventStream");
    siddhiAppRuntime.start();
    eventTimeStamp = System.currentTimeMillis();
    inputHandler.send(eventTimeStamp, new Object[] { "user0", 101, 34.4, 5 });
    inputHandler.send((eventTimeStamp + 10), new Object[] { "user0", 102, 24.5, 2 });
    Thread.sleep(2020);
    eventTimeStamp = System.currentTimeMillis();
    inputHandler.send(eventTimeStamp, new Object[] { "user0", 103, 24.5, 2 });
    inputHandler.send((eventTimeStamp - 100), new Object[] { "user0", 104, 54.5, 12 });
    inputHandler.send((eventTimeStamp - 2500), new Object[] { "user0", 105, 24.5, 6 });
    inputHandler.send((eventTimeStamp - 2400), new Object[] { "user0", 106, 24.5, 2 });
    // this late event later than the previous session
    inputHandler.send((eventTimeStamp - 5200), new Object[] { "user0", 107, 24.5, 2 });
    AssertJUnit.assertTrue(appender.getMessages().contains("[user0, 107, 24.5, 2]"));
    SiddhiTestHelper.waitForEvents(100, 10, count, 5200);
    AssertJUnit.assertEquals(6, inEventCount);
    AssertJUnit.assertEquals(6, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertTrue(innerAssertionsPassed);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) Configuration(org.apache.logging.log4j.core.config.Configuration) UnitTestAppender(io.siddhi.core.UnitTestAppender) Logger(org.apache.logging.log4j.core.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) SessionWindowProcessor(io.siddhi.core.query.processor.stream.window.SessionWindowProcessor) 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