Threads { static long sleepTime(Duration duration) { long milliseconds = duration.toMillis(); double times = Randoms.nextDouble(0.8, 1.2); return (long) (milliseconds * times); }  static void sleepRoughly(Duration duration);  }
@Test void sleepTime() { long sleepTime = Threads.sleepTime(Duration.ofMillis(10)); assertThat(sleepTime).isGreaterThanOrEqualTo(8).isLessThanOrEqualTo(12); sleepTime = Threads.sleepTime(Duration.ofMillis(100)); assertThat(sleepTime).isGreaterThanOrEqualTo(80).isLessThanOrEqualTo(120); sleepTime = Threads.sleepTime(Duration.ofSeconds(10)); assertThat(sleepTime).isGreaterThanOrEqualTo(8000).isLessThanOrEqualTo(12000); }