Search in sources :

Example 41 with StreamCallback

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

the class CallbackTestCase method callbackTest5.

@Test
public void callbackTest5() throws InterruptedException {
    log.info("callback test5");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "define stream cseEventStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from cseEventStream " + "select symbol, price , symbol as sym1 " + "insert into OutputStream ;";
    StreamCallback streamCallback = new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            if (events != null) {
                count += events.length;
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutputStream", streamCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 0f, 100L });
    siddhiAppRuntime.removeCallback(streamCallback);
    inputHandler.send(new Object[] { "WSO2", 0f, 100L });
    SiddhiTestHelper.waitForEvents(10, count, 1, 100);
    AssertJUnit.assertEquals(1, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 42 with StreamCallback

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

the class CustomJoinWindowTestCase method testMultipleStreamsToWindow.

@Test
public void testMultipleStreamsToWindow() throws InterruptedException {
    log.info("Test sending events from multiple streams into a single window");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume long); " + "define stream Stream2 (symbol string, price float, volume long); " + "define stream Stream3 (symbol string, price float, volume long); " + "define stream Stream4 (symbol string, price float, volume long); " + "define stream Stream5 (symbol string, price float, volume long); " + "define stream Stream6 (symbol string, price float, volume long); " + "define window StockWindow (symbol string, price float, volume long) lengthBatch(5); ";
    String query = "" + "@info(name = 'query0') " + "from Stream1 " + "insert into StockWindow; " + "from Stream2 " + "insert into StockWindow; " + "from Stream3 " + "insert into StockWindow; " + "from Stream4 " + "insert into StockWindow; " + "from Stream5 " + "insert into StockWindow; " + "from Stream6 " + "insert into StockWindow; " + "" + "@info(name = 'query1') " + "from StockWindow " + "select symbol, sum(price) as totalPrice, sum(volume) as volumes " + "insert into OutputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            assertEquals("Invalid number of output events", 1, events.length);
            assertArrayEquals(new Object[] { "WSO2", 150.0, 5L }, events[0].getData());
        }
    });
    siddhiAppRuntime.start();
    for (int i = 1; i <= 6; i++) {
        siddhiAppRuntime.getInputHandler("Stream" + i).send(new Object[] { "WSO2", (i * 10.0f), 1L });
    }
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
}
Also used : 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 43 with StreamCallback

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

the class CustomJoinWindowTestCase method testJoinWindowWithWindow.

@Test
public void testJoinWindowWithWindow() throws InterruptedException {
    log.info("Test join window with another window");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream TempStream(deviceID long, roomNo int, temp double); " + "define stream RegulatorStream(deviceID long, roomNo int, isOn bool); " + "define window TempWindow(deviceID long, roomNo int, temp double) time(1 min); " + "define window RegulatorWindow(deviceID long, roomNo int, isOn bool) length(1); ";
    String query = "" + "@info(name = 'query1') " + "from TempStream[temp > 30.0] " + "insert into TempWindow; " + "" + "@info(name = 'query2') " + "from RegulatorStream[isOn == false] " + "insert into RegulatorWindow; " + "" + "@info(name = 'query3') " + "from TempWindow " + "join RegulatorWindow " + "on TempWindow.roomNo == RegulatorWindow.roomNo " + "select TempWindow.roomNo, RegulatorWindow.deviceID, 'start' as action " + "insert into RegulatorActionStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("RegulatorActionStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            inEventCount++;
        }
    });
    InputHandler tempStream = siddhiAppRuntime.getInputHandler("TempStream");
    InputHandler regulatorStream = siddhiAppRuntime.getInputHandler("RegulatorStream");
    siddhiAppRuntime.start();
    tempStream.send(new Object[] { 100L, 1, 20.0 });
    tempStream.send(new Object[] { 100L, 2, 25.0 });
    tempStream.send(new Object[] { 100L, 3, 30.0 });
    tempStream.send(new Object[] { 100L, 4, 35.0 });
    tempStream.send(new Object[] { 100L, 5, 40.0 });
    regulatorStream.send(new Object[] { 100L, 1, false });
    regulatorStream.send(new Object[] { 100L, 2, false });
    regulatorStream.send(new Object[] { 100L, 3, false });
    regulatorStream.send(new Object[] { 100L, 4, false });
    regulatorStream.send(new Object[] { 100L, 5, false });
    Thread.sleep(500);
    assertEquals("Number of success events", 2, inEventCount);
    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 44 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback 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)

Example 45 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback 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)

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