javamultithreadingthread-sleepawaitility

How can I await at least specified amount of time with Awaitility?


In my test class, I really need to sleep for some amount of time. It's an integration test involving periodic remote call.

for (int i = 0; i < 16; i++) {
    // sleep some... should sleep some...
    Thread.sleep((int) TimeUnit.MINUTES.toMillis(4L)); // means as it means.
    // call remote api and check the response.
}

And what is the equivalent expression using Awaitility?

I tried...

// Let's sleep for 4 minutes, no matter what happen!
Awaitility.await()
        .atLeast(Duration.ofMinutes(4L)) // what the hell does this mean, anyway?
        .untilTrue(new AtomicBoolean(false));

It seems the timeout fired just after the default polling interval.

Shouldn't I use the Awaitillity at the first time in this case?


Solution

  • Probably too late of an answer but there are several ways to do it.

    This tells awaitility to have a poll delay of 4 minutes. So, wait 4 minutes before doing the assertion.

    Awaitility
        .await()
        .pollDelay(4, TimeUnit.MINUTES)
        .untilAsserted(() -> Assert.assertTrue(true));