Search in sources :

Example 31 with StreamCallback

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

the class InMemoryTransportTestCase method inMemoryTestCase19.

@Test(dependsOnMethods = { "inMemoryTestCase18" })
public void inMemoryTestCase19() throws InterruptedException, SubscriberUnAvailableException {
    log.info("Test inMemoryTestCase19");
    InMemoryBroker.Subscriber subscriptionWSO2 = new InMemoryBroker.Subscriber() {

        @Override
        public void onMessage(Object msg) {
            wso2Count.incrementAndGet();
        }

        @Override
        public String getTopic() {
            return "WSO2";
        }
    };
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='inMemory', topic='WSO2', @map(type='testSourceOption')) " + "define stream FooStream (symbol string, price float, volume long); " + "define stream BarStream (symbol string, price float, 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);
            for (Event event : events) {
                switch(wso2Count.incrementAndGet()) {
                    case 1:
                        Assert.assertEquals(event.getData(), new Object[] { "WSO2", "inMemory", 100L });
                        break;
                    case 2:
                        Assert.assertEquals(event.getData(), new Object[] { "WSO2", "inMemory", 101L });
                        break;
                    default:
                        org.testng.AssertJUnit.fail();
                }
            }
        }
    });
    siddhiAppRuntime.start();
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 101L }));
    Thread.sleep(100);
    // assert event count
    AssertJUnit.assertEquals("Number of WSO2 events", 2, wso2Count.get());
    siddhiAppRuntime.shutdown();
    InMemoryBroker.unsubscribe(subscriptionWSO2);
}
Also used : InMemoryBroker(io.siddhi.core.util.transport.InMemoryBroker) 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 32 with StreamCallback

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

the class InMemoryTransportTestCase method inMemoryTestCase10.

@Test(dependsOnMethods = { "inMemorySourceSinkAndEventMappingWithSiddhiQLAndRef" })
public void inMemoryTestCase10() throws InterruptedException, SubscriberUnAvailableException {
    log.info("Test inMemory 10");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='testTrpInMemory', topic='Foo', prop1='hi', prop2='7.5', " + "   @map(type='testTrp', @attributes(symbol='trp:symbol'," + "        volume='2',price='trp:price'))) " + "define stream FooStream (symbol string, price double, volume long); " + "define stream BarStream (symbol string, price double, 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", 7.5, 100L });
            }
        }
    });
    siddhiAppRuntime.start();
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 5.5, 100L }));
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[] { "IBM", 5.5, 100L }));
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 5.5, 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) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 33 with StreamCallback

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

the class InMemoryTransportTestCase method inMemoryTestCase22.

@Test(dependsOnMethods = { "inMemoryTestCase21" })
public void inMemoryTestCase22() throws InterruptedException {
    log.info("Test inMemoryTestCase22");
    SiddhiManager siddhiManager = new SiddhiManager();
    String publisherApp = "" + "define stream CheckStockStream (symbol1 string, totalPrice double); " + "@sink(type='inMemory', topic='OutputStream', @map(type='passThrough')) " + "define stream OutputStream (symbol1 string, totalPrice double); " + "" + "from CheckStockStream " + "select * " + "insert into OutputStream; ";
    String consumerApp = "" + "@source(type='inMemory', topic='OutputStream', @map(type='passThrough')) " + "define stream InputStream (symbol1 string, totalPrice double); ";
    SiddhiAppRuntime publisherRuntime = siddhiManager.createSiddhiAppRuntime(publisherApp);
    SiddhiAppRuntime consumerRuntime = siddhiManager.createSiddhiAppRuntime(consumerApp);
    consumerRuntime.addCallback("InputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            wso2Count.incrementAndGet();
        }
    });
    InputHandler stockStream = publisherRuntime.getInputHandler("CheckStockStream");
    publisherRuntime.start();
    consumerRuntime.start();
    stockStream.send(new Object[] { "WSO2", 50.0f });
    stockStream.send(new Object[] { "WSO2", 70.0f });
    Executors.newSingleThreadExecutor().execute(new Runnable() {

        @Override
        public void run() {
            consumerRuntime.getSources().iterator().next().get(0).pause();
        }
    });
    Executors.newSingleThreadExecutor().execute(new Runnable() {

        @Override
        public void run() {
            try {
                stockStream.send(new Object[] { "WSO2", 90f });
            } catch (InterruptedException ignored) {
            }
        }
    });
    Thread.sleep(2000);
    consumerRuntime.getSources().iterator().next().get(0).resume();
    Thread.sleep(2000);
    Assert.assertEquals(wso2Count.get(), 3);
    siddhiManager.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 34 with StreamCallback

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

the class InMemoryTransportTestCase method inMemoryTestCase11.

@Test(dependsOnMethods = { "inMemoryTestCase10" })
public void inMemoryTestCase11() throws InterruptedException, SubscriberUnAvailableException {
    log.info("Test inMemory 11");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='testTrpInMemory', topic='Foo', prop1='hi', prop2='test', " + "   @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 serviceDeploymentInfos = siddhiAppRuntime.getSources().iterator().next().get(0).getServiceDeploymentInfo();
    Assert.assertNull(serviceDeploymentInfos);
    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 35 with StreamCallback

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

the class InMemoryTransportTestCase method inMemoryWithFailingSource.

@Test(dependsOnMethods = { "inMemoryWithFailingSink1" })
public void inMemoryWithFailingSource() throws InterruptedException {
    log.info("Test failing inMemorySource");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='testFailingInMemory', topic='WSO2', @map(type='passThrough')) " + "define stream FooStream (symbol string, price float, volume long); " + "define stream BarStream (symbol string, price float, 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);
            for (Event event : events) {
                wso2Count.incrementAndGet();
            }
        }
    });
    siddhiAppRuntime.start();
    try {
        InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    } catch (SubscriberUnAvailableException e) {
        AssertJUnit.fail();
    }
    TestFailingInMemorySource.fail = true;
    TestFailingInMemorySource.connectionCallback.onError(new ConnectionUnavailableException("Connection Lost"));
    Thread.sleep(6000);
    try {
        InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
        AssertJUnit.fail();
    } catch (Throwable e) {
        AssertJUnit.assertTrue(e instanceof SubscriberUnAvailableException);
    }
    TestFailingInMemorySource.fail = false;
    Thread.sleep(10000);
    try {
        InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    } catch (SubscriberUnAvailableException e) {
        AssertJUnit.fail();
    }
    // assert event count
    AssertJUnit.assertEquals("Number of WSO2 events", 2, wso2Count.get());
    AssertJUnit.assertEquals("Number of errors", 1, TestFailingInMemorySource.numberOfErrorOccurred);
    siddhiAppRuntime.shutdown();
}
Also used : SubscriberUnAvailableException(io.siddhi.core.util.transport.SubscriberUnAvailableException) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException) 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