Search in sources :

Example 36 with StreamCallback

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

the class InMemoryTransportTestCase method inMemoryTestCase7.

@Test(dependsOnMethods = { "inMemoryTestCase6" })
public void inMemoryTestCase7() throws InterruptedException, SubscriberUnAvailableException {
    log.info("Test inMemory 7");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='testTrpInMemory', topic='Foo', prop1='hi', prop2='test', fail='true', " + "   @map(type='passThrough', @attributes(symbol='trp:symbol'," + "        volume='volume',price='trp:price'))) " + "define stream FooStream (symbol string, price string, volume long); " + "define stream BarStream (symbol string, price string, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            wso2Count.incrementAndGet();
            for (Event event : events) {
                AssertJUnit.assertArrayEquals(event.getData(), new Object[] { "hi", "test", 100L });
            }
        }
    });
    siddhiAppRuntime.start();
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[] { "WSO2", "in", 100L }));
    Thread.sleep(100);
    // assert event count
    AssertJUnit.assertEquals("Number of events", 0, wso2Count.get());
    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 37 with StreamCallback

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

the class InMemoryTransportTestCase method inMemoryTestCase12.

@Test(dependsOnMethods = { "inMemoryTestCase11" })
public void inMemoryTestCase12() throws InterruptedException, SubscriberUnAvailableException {
    log.info("Test inMemory 12");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='testDepInMemory', topic='Foo', prop1='hi', prop2='test', dep:prop3='foo', " + "   @map(type='testTrp', @attributes('trp:symbol'," + "        'trp:price', '2'))) " + "define stream FooStream (symbol string, price string, volume long); " + "define stream BarStream (symbol string, price string, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    ServiceDeploymentInfo serviceDeploymentInfo = siddhiAppRuntime.getSources().iterator().next().get(0).getServiceDeploymentInfo();
    Assert.assertNotNull(serviceDeploymentInfo);
    Assert.assertTrue(serviceDeploymentInfo.isSecured());
    Assert.assertTrue(serviceDeploymentInfo.getServiceProtocol() == ServiceDeploymentInfo.ServiceProtocol.UDP);
    Assert.assertTrue(serviceDeploymentInfo.getPort() == 9000);
    Assert.assertTrue(serviceDeploymentInfo.getDeploymentProperties().get("prop3").equals("foo"));
    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            wso2Count.incrementAndGet();
            for (Event event : events) {
                AssertJUnit.assertArrayEquals(event.getData(), new Object[] { "hi", "test", 100L });
            }
        }
    });
    siddhiAppRuntime.start();
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[] { "WSO2", "in", 100L }));
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[] { "IBM", "in", 100L }));
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[] { "WSO2", "in", 100L }));
    Thread.sleep(100);
    // assert event count
    AssertJUnit.assertEquals("Number of events", 3, wso2Count.get());
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) ServiceDeploymentInfo(io.siddhi.core.stream.ServiceDeploymentInfo) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 38 with StreamCallback

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

the class CallbackTestCase method callbackTest4.

@Test
public void callbackTest4() throws InterruptedException {
    log.info("callback test4");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('callbackTest1') " + "" + "define stream StockStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from StockStream[70 > price] " + "select symbol, price " + "insert into OutputStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("StockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            Map<String, Object>[] eventMap = toMap(events);
            Assert.assertEquals(eventMap.length, events.length);
            Assert.assertEquals(eventMap[0].get("_timestamp"), events[0].getTimestamp());
            Assert.assertEquals(eventMap[1].get("_timestamp"), events[1].getTimestamp());
            Assert.assertEquals(eventMap[1].get("volume"), events[1].getData(2));
            count = count + events.length;
            eventArrived = true;
        }
    });
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            Map<String, Object>[] eventMap = toMap(events);
            Assert.assertEquals(eventMap.length, events.length);
            Assert.assertEquals(eventMap[0].get("_timestamp"), events[0].getTimestamp());
            Assert.assertEquals(eventMap[0].get("price"), events[0].getData(1));
            Assert.assertEquals(eventMap[0].get("symbol"), events[0].getData(0));
            Assert.assertNull(eventMap[0].get("volume"));
            count = count + events.length;
            eventArrived2 = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Event[] { new Event(System.currentTimeMillis(), new Object[] { "IBM", 700f, 100L }), new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60.5f, 200L }) });
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, count);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertTrue(eventArrived2);
    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 39 with StreamCallback

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

the class CallbackTestCase method callbackTest1.

@Test
public void callbackTest1() throws InterruptedException {
    log.info("callback test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('callbackTest1') " + "" + "define stream StockStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from StockStream[70 > price] " + "select symbol, price " + "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 = count + inEvents.length;
            eventArrived = true;
        }
    });
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            count = count + events.length;
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 100L });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 200L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(2, count);
    AssertJUnit.assertTrue(eventArrived);
    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) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 40 with StreamCallback

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

the class CallbackTestCase method callbackTest3.

@Test(expectedExceptions = DefinitionNotExistException.class)
public void callbackTest3() throws InterruptedException {
    log.info("callback test3");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('callbackTest1') " + "" + "define stream StockStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from StockStream[70 > price] " + "select symbol, price " + "insert into outputStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream2", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
        }
    });
    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)

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