The following test works when I use Thread.sleep().
@Test
public void closeableTimer() throws InterruptedException {
Timer timer = new DefaultTimer(TimeUnit.NANOSECONDS);
Assertions.assertThat(timer.count()).isEqualTo(0);
Assertions.assertThat(timer.totalDuration()).isEqualTo(Duration.ZERO);
try (Timer.Timed sample = timer.start()) {
Thread.sleep(500L);
}
Assertions.assertThat(timer.count()).isEqualTo(1);
Assertions.assertThat(timer.totalDuration()).isGreaterThan(Duration.ZERO);
}
But if I use Awaitility instead for same test, it throws following error. Why? The code using Awaitility is using atLeast(500 ms).
Condition was evaluated in 108 milliseconds which is earlier than expected minimum timeout 500 milliseconds
org.awaitility.core.ConditionTimeoutException
@Test
public void closeableTimer() {
Timer timer = new DefaultTimer(TimeUnit.NANOSECONDS);
Assertions.assertThat(timer.count()).isEqualTo(0);
Assertions.assertThat(timer.totalDuration()).isEqualTo(Duration.ZERO);
try (Timer.Timed ignored = timer.start()) {
await().atLeast(Duration.ofMillis(500L)).until(() -> true);
}
Assertions.assertThat(timer.count()).isEqualTo(1);
Assertions.assertThat(timer.totalDuration()).isGreaterThan(Duration.ZERO);
}