use of io.siddhi.core.UnitTestAppender in project siddhi by wso2.
the class FaultStreamTestCase method faultStreamTest8.
@Test(dependsOnMethods = "faultStreamTest7")
public void faultStreamTest8() throws InterruptedException {
log.info("faultStreamTest8-Tests fault handling when it's set to wait. " + "Thread would be waiting until Sink reconnects.");
UnitTestAppender appender = new UnitTestAppender("UnitTestAppender", null);
final Logger logger = (Logger) LogManager.getRootLogger();
logger.setLevel(Level.ALL);
logger.addAppender(appender);
appender.start();
InMemoryBroker.Subscriber subscriptionIBM = new InMemoryBroker.Subscriber() {
@Override
public void onMessage(Object msg) {
EventPrinter.print(new Event[] { (Event) msg });
count.incrementAndGet();
}
@Override
public String getTopic() {
return "IBM";
}
};
SiddhiManager siddhiManager = new SiddhiManager();
String siddhiApp = "" + "@OnError(action='stream')" + "define stream cseEventStream (symbol string, price float, volume long);" + "\n" + "@sink(type='inMemory', topic='{{symbol}}', on.error='wait', @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);
countStream.incrementAndGet();
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
try {
Thread thread = new Thread() {
@Override
public void run() {
try {
inputHandler.send(new Object[] { "IBM", 5f, 100L });
} catch (InterruptedException e) {
}
}
};
thread.start();
Thread.sleep(2000);
Assert.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("error while connecting Sink 'inMemory' at 'outputStream', will retry every"));
Assert.assertEquals(count.get(), 0);
Assert.assertEquals(countStream.get(), 0);
InMemoryBroker.subscribe(subscriptionIBM);
Thread.sleep(6000);
Assert.assertEquals(count.get(), 1);
Assert.assertEquals(countStream.get(), 1);
} catch (Exception e) {
Assert.fail("Unexpected exception occurred when testing.", e);
} finally {
logger.removeAppender(appender);
siddhiAppRuntime.shutdown();
InMemoryBroker.unsubscribe(subscriptionIBM);
}
}
use of io.siddhi.core.UnitTestAppender in project siddhi by wso2.
the class FaultStreamTestCase method faultStreamTest13.
@Test(dependsOnMethods = "faultStreamTest12")
public void faultStreamTest13() throws InterruptedException {
log.info("faultStreamTest13-Tests fault handling when async set to wait. " + "Thread would be waiting until Sink reconnects.");
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();
InMemoryBroker.Subscriber subscriptionIBM = new InMemoryBroker.Subscriber() {
@Override
public void onMessage(Object msg) {
EventPrinter.print(new Event[] { (Event) msg });
count.incrementAndGet();
}
@Override
public String getTopic() {
return "IBM";
}
};
InMemoryBroker.subscribe(subscriptionIBM);
String siddhiApp = "" + "@OnError(action='stream')" + "define stream cseEventStream (symbol string, price float, volume long);" + "\n" + "@sink(type='testAsyncInMemory', topic='{{symbol}}', on.error='wait', @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);
countStream.incrementAndGet();
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
TestAsyncInMemory.fail = true;
try {
Thread thread = new Thread() {
@Override
public void run() {
try {
inputHandler.send(new Object[] { "IBM", 10f, 100L });
Thread.sleep(100);
inputHandler.send(new Object[] { "IBM", 11f, 100L });
} catch (InterruptedException e) {
}
}
};
thread.start();
Thread.sleep(2000);
Assert.assertEquals(countStream.get(), 1);
Assert.assertEquals(count.get(), 0);
Assert.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("error while connecting Sink 'testAsyncInMemory'" + " at 'outputStream', will retry every"));
TestAsyncInMemory.fail = false;
Thread.sleep(11000);
Assert.assertEquals(countStream.get(), 2);
Assert.assertEquals(count.get(), 2);
} catch (Exception e) {
Assert.fail("Unexpected exception occurred when testing.", e);
} finally {
logger.removeAppender(appender);
siddhiAppRuntime.shutdown();
InMemoryBroker.unsubscribe(subscriptionIBM);
}
}
use of io.siddhi.core.UnitTestAppender in project siddhi by wso2.
the class FaultStreamTestCase method faultStreamTest14.
@Test(dependsOnMethods = "faultStreamTest13")
public void faultStreamTest14() throws InterruptedException {
log.info("faultStreamTest14-Tests fault handling when async set to wait. " + "Thread would be waiting until Sink reconnects.");
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();
InMemoryBroker.Subscriber subscriptionIBM = new InMemoryBroker.Subscriber() {
@Override
public void onMessage(Object msg) {
EventPrinter.print(new Event[] { (Event) msg });
count.incrementAndGet();
}
@Override
public String getTopic() {
return "IBM";
}
};
InMemoryBroker.subscribe(subscriptionIBM);
String siddhiApp = "" + "@OnError(action='stream')" + "define stream cseEventStream (symbol string, price float, volume long);" + "\n" + "@sink(type='testAsyncInMemory', topic='{{symbol}}', on.error='wait', @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);
countStream.incrementAndGet();
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
TestAsyncInMemory.failOnce = true;
try {
Thread thread = new Thread() {
@Override
public void run() {
try {
inputHandler.send(new Object[] { "IBM", 6f, 100L });
inputHandler.send(new Object[] { "IBM", 7f, 100L });
} catch (InterruptedException e) {
}
}
};
thread.start();
Thread.sleep(11000);
Assert.assertTrue(((UnitTestAppender) logger.getAppenders().get("UnitTestAppender")).getMessages().contains("Connection unavailable during publishing, " + "error while connecting Sink 'testAsyncInMemory' at 'outputStream', will retry"));
Assert.assertEquals(count.get(), 2);
Assert.assertEquals(countStream.get(), 2);
} catch (Exception e) {
Assert.fail("Unexpected exception occurred when testing.", e);
} finally {
logger.removeAppender(appender);
siddhiAppRuntime.shutdown();
InMemoryBroker.unsubscribe(subscriptionIBM);
}
}
use of io.siddhi.core.UnitTestAppender 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.UnitTestAppender in project siddhi by wso2.
the class FaultStreamTestCase method faultStreamTest3.
@Test(dependsOnMethods = "faultStreamTest2")
public void faultStreamTest3() throws InterruptedException {
log.info("faultStreamTest3-Tests fault handling when it's set to stream. " + "No errors would be logged since exceptions are being gracefully handled.");
UnitTestAppender appender = new UnitTestAppender("UnitTestAppender", null);
Logger logger = (Logger) LogManager.getLogger(StreamJunction.class);
logger.addAppender(appender);
appender.start();
SiddhiManager siddhiManager = new SiddhiManager();
siddhiManager.setExtension("custom:fault", FaultFunctionExtension.class);
String siddhiApp = "" + "@OnError(action='stream')" + "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() == null);
} 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);
}
Aggregations