ExecutorProxy implements Shutdownable { public boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException { logger.info(String.format("Awaiting completion up to %d %s.", timeout, unit)); if (!executor.awaitTermination(timeout, unit)) { logger.warn(String.format("Executor failed to terminate in %d %s.", timeout, unit)); return false; } logger.info("Terminated."); return true; } ExecutorProxy(final ExecutorService executor); @Override void shutdown(); List<Runnable> shutdownNow(); boolean awaitTermination(final long timeout, final TimeUnit unit);  }
@Test public void testAwaitTermination() throws ExecutionException, InterruptedException, TimeoutException { final ExecutorProxyStub proxy = new ExecutorProxyStub(); final Future<Boolean> result = proxy.getExecutor().submit(()-> { Thread.sleep(1000); return true; }); proxy.shutdown(); Assert.assertTrue(proxy.awaitTermination(2, TimeUnit.SECONDS)); Assert.assertTrue(result.get(0, TimeUnit.MILLISECONDS)); Assert.assertTrue(proxy.awaitTermination(0, TimeUnit.SECONDS)); }