ControlledThreadPoolExecutorServiceCloser implements AutoCloseable { @PreDestroy @Override public void close() throws InterruptedException { for (ControlledThreadPoolExecutorService executorService : executorServices) { executorService.sendShutdownSignal(); if (!closeImmediately) { executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); } executorService.shutdownNow(); } } ControlledThreadPoolExecutorServiceCloser(ControlledThreadPoolExecutorService... executorServices); void setCloseImmediately(boolean closeImmediately); @PreDestroy @Override void close();  }
@Test public void testClose() throws InterruptedException { controlledThreadPoolExecutorCloser.close(); verify(mock1).sendShutdownSignal(); verify(mock1).awaitTermination(anyLong(), any()); verify(mock1).shutdownNow(); verify(mock2).sendShutdownSignal(); verify(mock2).awaitTermination(anyLong(), any()); verify(mock2).shutdownNow(); }