Search in sources :

Example 16 with UnitTestAppender

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

the class SessionWindowTestCase method testSessionWindow16.

@Test(description = "This test case checks with the late event which " + "does not belong to the current session without an allowedLatency time period")
public void testSessionWindow16() throws InterruptedException {
    log.info("SessionWindow Test16: Testing session window, " + "late event which does not belong to the current " + "session window without an allowedLatency time period");
    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) " + "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 == 2) {
                    AssertJUnit.assertTrue("user0".equals(removeEvents[0].getData(0)));
                    AssertJUnit.assertTrue("user0".equals(removeEvents[1].getData(0)));
                    AssertJUnit.assertEquals(101, removeEvents[0].getData(1));
                    AssertJUnit.assertEquals(102, removeEvents[1].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 });
    // late event
    inputHandler.send((eventTimeStamp - 2500), new Object[] { "user0", 103, 24.5, 2 });
    AssertJUnit.assertTrue(appender.getMessages().contains("[user0, 103, 24.5, 2]"));
    SiddhiTestHelper.waitForEvents(100, 4, count, 4100);
    AssertJUnit.assertEquals(2, inEventCount);
    AssertJUnit.assertEquals(2, 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)

Example 17 with UnitTestAppender

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

the class FaultStreamTestCase method faultStreamTest9.

@Test(dependsOnMethods = "faultStreamTest8")
public void faultStreamTest9() throws InterruptedException {
    log.info("faultStreamTest9-Tests fault handling when it's set to stream at Sink but, " + "the fault stream is not configured. Events will be logged and dropped.");
    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}}', 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(0) != null);
        }
    });
    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) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("after consuming events from Stream " + "'outputStream', Subscriber for topic 'IBM' is unavailable. 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) UnitTestAppender(io.siddhi.core.UnitTestAppender) 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 18 with UnitTestAppender

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

the class FaultStreamTestCase method faultStreamTest16.

@Test(dependsOnMethods = "faultStreamTest15")
public void faultStreamTest16() 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;
    TestAsyncInMemory.failRuntime = true;
    try {
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    inputHandler.send(new Object[] { "IBM", 0f, 100L });
                    inputHandler.send(new Object[] { "IBM", 1f, 100L });
                } catch (InterruptedException e) {
                }
            }
        };
        thread.start();
        Thread.sleep(6000);
        AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("as on.error='wait' does not handle"));
        Assert.assertEquals(count.get(), 0);
        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 19 with UnitTestAppender

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

the class FaultStreamTestCase method faultStreamTest7.

@Test(dependsOnMethods = "faultStreamTest6")
public void faultStreamTest7() throws InterruptedException {
    log.info("faultStreamTest7-Tests fault handling 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='inMemory', 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 });
        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 20 with UnitTestAppender

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

the class FaultStreamTestCase method faultStreamTest1.

@Test
public void faultStreamTest1() throws InterruptedException {
    log.info("faultStreamTest1-Tests logging by default when fault handling is not configured explicitly.");
    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 = "" + "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)

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