use of io.siddhi.core.UnitTestAppender 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.UnitTestAppender in project siddhi by wso2.
the class FaultStreamTestCase method faultStreamTest2.
@Test(dependsOnMethods = "faultStreamTest1")
public void faultStreamTest2() throws InterruptedException {
log.info("faultStreamTest2-Tests logging when fault handling is set to log.");
UnitTestAppender appender = new UnitTestAppender("UnitTestAppender", null);
final Logger logger = (Logger) LogManager.getRootLogger();
logger.setLevel(Level.ALL);
logger.addAppender(appender);
appender.start();
SiddhiManager siddhiManager = new SiddhiManager();
siddhiManager.setExtension("custom:fault", FaultFunctionExtension.class);
String siddhiApp = "" + "@OnError(action='log')" + "define stream cseEventStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from cseEventStream[custom:fault() > volume] " + "select symbol, price , symbol as sym1 " + "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.addAndGet(inEvents.length);
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
try {
inputHandler.send(new Object[] { "IBM", 0f, 100L });
AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("Error when running faultAdd(). Exception on " + "class 'io.siddhi.core.stream.FaultFunctionExtension'"));
} catch (Exception e) {
Assert.fail("Unexpected exception occurred when testing.", e);
} finally {
logger.removeAppender(appender);
siddhiAppRuntime.shutdown();
}
AssertJUnit.assertEquals(0, count.get());
AssertJUnit.assertFalse(eventArrived);
}
use of io.siddhi.core.UnitTestAppender in project siddhi by wso2.
the class LogSinkTest method testWithAllOptions.
@Test
public void testWithAllOptions() throws Exception {
LoggerContext context = LoggerContext.getContext(false);
Configuration config = context.getConfiguration();
UnitTestAppender appender = new UnitTestAppender("UnitTestAppender", null);
final Logger logger = (Logger) LogManager.getRootLogger();
logger.setLevel(Level.ALL);
logger.addAppender(appender);
appender.start();
SiddhiManager siddhiManager = new SiddhiManager();
String inputStream = "@App:name(\"HelloWorldApp\")\n" + "define stream CargoStream (weight int);";
String outputStream = "@sink(type='log', prefix='My Log', priority='info')\n" + "define stream OutputStream(weight int, totalWeight long);";
String query = ("@info(name='HelloWorldQuery') " + "from CargoStream " + "select weight, sum(weight) as totalWeight " + "insert into OutputStream;");
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inputStream + outputStream + query);
siddhiAppRuntime.start();
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("CargoStream");
try {
inputHandler.send(new Object[] { 2 });
AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("My Log"));
AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("data=[2, 2]"));
inputHandler.send(new Object[] { 3 });
AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("My Log"));
AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("data=[3, 5]"));
} catch (Exception e) {
Assert.fail("Unexpected exception occurred when testing with all options", e);
} finally {
logger.removeAppender(appender);
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.UnitTestAppender in project siddhi by wso2.
the class LogSinkTest method testWithDefaultOptions.
@Test
public void testWithDefaultOptions() throws Exception {
LoggerContext context = LoggerContext.getContext(false);
Configuration config = context.getConfiguration();
UnitTestAppender appender = new UnitTestAppender("UnitTestAppender", null);
final Logger logger = (Logger) LogManager.getRootLogger();
logger.setLevel(Level.ALL);
logger.addAppender(appender);
appender.start();
SiddhiManager siddhiManager = new SiddhiManager();
String inputStream = "@App:name(\"HelloWorldApp\")\n" + "define stream CargoStream (weight int);";
String outputStream = "@sink(type='log')\n" + "define stream OutputStream(weight int, totalWeight long);";
String query = ("@info(name='HelloWorldQuery') " + "from CargoStream " + "select weight, sum(weight) as totalWeight " + "insert into OutputStream;");
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inputStream + outputStream + query);
siddhiAppRuntime.start();
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("CargoStream");
try {
inputHandler.send(new Object[] { 2 });
AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("HelloWorldApp : OutputStream"));
AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("data=[2, 2]"));
inputHandler.send(new Object[] { 3 });
AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("HelloWorldApp : OutputStream"));
AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("data=[3, 5]"));
} catch (Exception e) {
Assert.fail("Unexpected exception occurred when testing with default options", e);
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.UnitTestAppender in project siddhi by wso2.
the class FaultStreamTestCase method faultStreamTest12.
@Test(dependsOnMethods = "faultStreamTest11")
public void faultStreamTest12() throws InterruptedException {
log.info("faultStreamTest12-Tests fault handling for async when it's set to log. " + "Events would be logged and dropped during publishing failure at Sink");
UnitTestAppender appender = new UnitTestAppender("UnitTestAppender", null);
final Logger logger = (Logger) LogManager.getRootLogger();
logger.setLevel(Level.ALL);
logger.addAppender(appender);
appender.start();
SiddhiManager siddhiManager = new SiddhiManager();
String siddhiApp = "" + "@OnError(action='stream')" + "define stream cseEventStream (symbol string, price float, volume long);" + "\n" + "@sink(type='testAsyncInMemory', topic='{{symbol}}', on.error='log', @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(0) != null);
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
try {
inputHandler.send(new Object[] { "IBM", 0f, 100L });
Thread.sleep(6000);
AssertJUnit.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("Dropping event at Sink 'testAsyncInMemory' at"));
} catch (Exception e) {
Assert.fail("Unexpected exception occurred when testing.", e);
} finally {
logger.removeAppender(appender);
siddhiAppRuntime.shutdown();
}
}
Aggregations