use of won.integrationtest.support.CountdownLatchAction in project webofneeds by researchstudio-sat.
the class AbstractBotBasedTest method runTestWithAsserts.
protected void runTestWithAsserts(Consumer<EventListenerContext> botInitializer, Runnable asserter) throws Exception {
logger.debug("preparing bot test in runTestWithAsserts(..)");
MutableStringHolder errorMessage = new MutableStringHolder();
bot.setInitializer(ctx -> {
bot.newCountDownLatch(1);
EventBus bus = ctx.getEventBus();
bus.clear();
// now, add a listener to the WorkDoneEvent.
// its only purpose is to trip the CyclicBarrier instance that
// the test method is waiting on
bus.subscribe(TestPassedEvent.class, new ActionOnEventListener(ctx, new CountdownLatchAction(ctx, bot.getCountDownLatch())));
bus.subscribe(TestFailedEvent.class, new ActionOnEventListener(ctx, new MultipleActions(ctx, false, new BaseEventBotAction(ctx) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
if (event instanceof TestFailedEvent) {
errorMessage.set(((TestFailedEvent) event).getMessage());
if (errorMessage.isEmpty()) {
errorMessage.set("[no error message provided]");
}
}
}
}, new CountdownLatchAction(ctx, bot.getCountDownLatch()))));
// now run test-specific initializer
if (botInitializer != null) {
botInitializer.accept(ctx);
}
});
// possibly, the bot is already running (the SpringAwareBotManager finds and
// initializes it)
// make sure we can re-initialize it here
bot.shutdown();
// re-initialize
bot.initialize();
logger.debug("bot re-initialized");
logger.debug("waiting for test to finish...");
bot.getCountDownLatch().await();
if (errorMessage.isPresent()) {
logger.debug("test failed");
Assert.fail(errorMessage.get());
}
logger.debug("executing asserts (if any)");
// execute any asserts
asserter.run();
logger.debug("test passed");
}
Aggregations