Search in sources :

Example 21 with StreamCallback

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

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

the class FaultStreamTestCase method faultStreamTest11.

@Test(dependsOnMethods = "faultStreamTest10")
public void faultStreamTest11() throws Exception {
    log.info("faultStreamTest11-Tests capturing runtime exceptions by registering an exception " + "listener to SiddhiAppRuntime");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "define stream cseEventStream (symbol string, price float, volume long);" + "\n" + "define stream outputStream (symbol string, price float);" + "\n" + "@PrimaryKey('symbol')" + "define table cseStoreTable (symbol string, price float);" + "\n" + "@info(name = 'query1') " + "from cseEventStream " + "select symbol, price " + "insert into cseStoreTable ;" + "\n" + "@info(name = 'query2') " + "from cseEventStream " + "select symbol, price " + "insert into outputStream ;" + "";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.handleRuntimeExceptionWith(new ExceptionListener() {

        @Override
        public void exceptionThrown(Exception e) {
            failedCaught = true;
            failedCount.incrementAndGet();
        }
    });
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            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 });
                    inputHandler.send(new Object[] { "IBM", 1f, 200L });
                } catch (InterruptedException e) {
                }
            }
        };
        thread.start();
        Thread.sleep(500);
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        siddhiAppRuntime.shutdown();
    }
    Assert.assertTrue(eventArrived);
    Assert.assertTrue(failedCaught);
    Assert.assertEquals(count.get(), 2);
    Assert.assertEquals(failedCount.get(), 1);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) ExceptionListener(java.beans.ExceptionListener) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 23 with StreamCallback

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

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

the class ExceptionHandlerTestCase method createTestExecutionRuntime.

private SiddhiAppRuntime createTestExecutionRuntime() {
    siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('callbackTest1') " + "" + "@async(buffer.size='2')" + "define stream StockStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "@Parallel " + "from StockStream[price + 0.0 > 0.0] " + "select symbol, price " + "insert into outputStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });
    return siddhiAppRuntime;
}
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)

Example 25 with StreamCallback

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

the class JunctionTestCase method oneToOneTest.

@Test
public void oneToOneTest() throws InterruptedException {
    log.info("one to one");
    StreamDefinition streamA = StreamDefinition.id("streamA").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("parallel"));
    StreamJunction streamJunctionA = new StreamJunction(streamA, executorService, 1024, null, siddhiAppContext);
    StreamJunction.Publisher streamPublisherA = streamJunctionA.constructPublisher();
    StreamDefinition streamB = StreamDefinition.id("streamB").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("parallel"));
    StreamJunction streamJunctionB = new StreamJunction(streamB, executorService, 1024, null, siddhiAppContext);
    final StreamJunction.Publisher streamPublisherB = streamJunctionB.constructPublisher();
    StreamCallback streamCallbackA = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = new StreamEvent(2, 2, 2);
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                innerStreamEvent.setOutputData(streamEvent.getData());
                streamPublisherB.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackB = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            count += streamEvents.length;
            eventArrived = true;
            for (Event streamEvent : streamEvents) {
                AssertJUnit.assertTrue(streamEvent.getData()[0].equals("IBM") || (streamEvent.getData()[0].equals("WSO2")));
            }
        }
    };
    streamJunctionA.subscribe(streamCallbackA);
    streamJunctionA.startProcessing();
    streamJunctionB.subscribe(streamCallbackB);
    streamJunctionB.startProcessing();
    // Thread.sleep(100);
    StreamEvent streamEvent1 = new StreamEvent(2, 2, 2);
    streamEvent1.setTimestamp(System.currentTimeMillis());
    streamEvent1.setOutputData(new Object[] { "IBM", 12 });
    StreamEvent streamEvent2 = new StreamEvent(2, 2, 2);
    streamEvent2.setTimestamp(System.currentTimeMillis());
    streamEvent2.setOutputData(new Object[] { "WSO2", 112 });
    streamPublisherA.send(streamEvent1);
    streamPublisherA.send(streamEvent2);
    Thread.sleep(100);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(2, count);
    streamJunctionA.stopProcessing();
    streamJunctionB.stopProcessing();
}
Also used : StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) StreamEvent(io.siddhi.core.event.stream.StreamEvent) Event(io.siddhi.core.event.Event) StreamEvent(io.siddhi.core.event.stream.StreamEvent) 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