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