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

2

25

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?

Dump answered 4/3, 2022 at 4:28 Comment(0)
N
38

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));
Negotiation answered 27/4, 2022 at 16:58 Comment(0)
C
12

Also quite late, with the latest Awaitility (4.2.0) a timeout also needs to be defined, which needs to be larger than the pollDelay. Default timeout is 10 seconds. So, if you wait less then 10sec, not timeout definition necessary.

So, this worked for me:

Awaitility.await()
  .timeout(66, SECONDS)
  .pollDelay(65, SECONDS)
  .untilAsserted(() -> Assertions.assertTrue(true));

worked in a test without multi-threading, replacing a Thread.sleep(x).

Contretemps answered 18/7, 2023 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.