Sleeper { void sleep(final Duration duration) throws CancellationException { try { Thread.sleep(duration.toMillis()); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); throw new CancellationException(); } }    }
@Test void sleep() { final Clock clock = Clock.systemUTC(); final Instant start = clock.instant(); unit.sleep(Duration.ofSeconds(1)); final Instant end = clock.instant(); assertThat(Duration.between(start, end), is(greaterThanOrEqualTo(Duration.ofSeconds(1)))); }
@Test void shouldInterrupt() throws InterruptedException { final AtomicReference<CancellationException> exception = new AtomicReference<>(); final Thread thread = new Thread(() -> exception.set(assertThrows(CancellationException.class, () -> unit.sleep(Duration.ofSeconds(1))))); thread.start(); thread.interrupt(); thread.join(); assertThat(exception.get(), is(instanceOf(CancellationException.class))); }