ShutdownEnabler implements ShutdownHook.Handler { @Override public Optional<Consumer<ReturnCode>> get() { return Optional.of((returnCode -> { ShutdownEnabler.LOGGER.debug("Running with {}.", daemonAllowedToTerminate); daemonAllowedToTerminate.release(); })); } ShutdownEnabler(); void waitUntilTriggered(); @Override Optional<Consumer<ReturnCode>> get();  }
@Test void standard() { final ShutdownEnabler se = new ShutdownEnabler(); final ExecutorService e = Executors.newFixedThreadPool(1); final Future<?> f = e.submit(se::waitUntilTriggered); assertThatThrownBy(() -> f.get(1, TimeUnit.SECONDS)) .isInstanceOf(TimeoutException.class); final Consumer<ReturnCode> c = se.get() .orElseThrow(() -> new IllegalStateException("Should have returned.")); c.accept(ReturnCode.OK); assertTimeout(Duration.ofSeconds(5), (Executable) f::get); assertThat(f).isDone(); e.shutdownNow(); }