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);
}
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();
}
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();
}
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();
}
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();
}
Aggregations