// instead of using the default executor set up for test.
final ExecutorService executorService = new ThreadPoolExecutor(1, 1,
0, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(1),
new ThreadPoolExecutor.AbortPolicy());
final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
try
{
final Engine engine = new EngineBuilder()
.setTaskExecutor(executorService)
.setTimerScheduler(scheduledExecutorService)
.build();
// First we submit two tasks that will never finish. This saturates the
// underlying executor by using its only thread and saturating its
// single slot queue.
engine.run(neverEndingBlockingTask());
engine.run(neverEndingBlockingTask());
// Now we submit another task. The execution loop for this task will fail
// during submit to the underlying executor. We expect that it will be
// cancelled.
final Task<?> task = neverEndingBlockingTask();
withDisabledLogging(new Runnable()
{
@Override
public void run()
{
engine.run(task);
try
{
assertTrue(task.await(5, TimeUnit.SECONDS));
}
catch (InterruptedException e)
{
// Ignore.
}
}
});
assertTrue(task.isFailed());
assertTrue("Expected underlying exception to be instance of RejectedExecutionException, but was: " + task.getError().getCause(),
task.getError().getCause() instanceof RejectedExecutionException);
engine.shutdown();
}
finally
{
scheduledExecutorService.shutdownNow();
executorService.shutdownNow();
}
}