Florian
Florian

Reputation: 5071

How can I get the remaining time in an Awaitility poll?

Using Awaitility 4.0.1, I put together the following poll that polls my server for data for 5 minutes, in intervals of 10 seconds, and runs perfectly fine:

AtomicReference<Response> response = null;
with().pollInterval(10, TimeUnit.SECONDS)
    .and().timeout(5, TimeUnit.MINUTES)
    .await("Retrieving data from server")
    .until(() -> {
        response = restClient.performGetRequest();
        boolean isFilled = !response.readEntity(String.class).isEmpty();
        return isFilled;
    });
return response;

Is there a way to get from Awaitility - within the until lambda - the attempt of retry and the remaining number of retries that it will perform before failing?

Upvotes: 1

Views: 1569

Answers (1)

Johan
Johan

Reputation: 40568

No, you cannot get it from within until. But you can use a Condition Evaluation Listener, but it won't give you the number of tries or tries that are left but it does give you elapsed and remaining time. For example:

with().
         conditionEvaluationListener(condition -> System.out.printf("%s (elapsed time %dms, remaining time %dms)\n", condition.getDescription(), condition.getElapsedTimeInMS(), condition.getRemainingTimeInMS())).
         await().atMost(Duration.TEN_SECONDS).until(new CountDown(5), is(equalTo(0)));

will log:

    org.awaitility.AwaitilityJava8Test$CountDown expected (<0> or a value less than <0>) but was <5> (elapsed time 101ms, remaining time 1899ms)
    org.awaitility.AwaitilityJava8Test$CountDown expected (<0> or a value less than <0>) but was <4> (elapsed time 204ms, remaining time 1796ms)
    org.awaitility.AwaitilityJava8Test$CountDown expected (<0> or a value less than <0>) but was <3> (elapsed time 306ms, remaining time 1694ms)
    org.awaitility.AwaitilityJava8Test$CountDown expected (<0> or a value less than <0>) but was <2> (elapsed time 407ms, remaining time 1593ms)
    org.awaitility.AwaitilityJava8Test$CountDown expected (<0> or a value less than <0>) but was <1> (elapsed time 508ms, remaining time 1492ms)
    org.awaitility.AwaitilityJava8Test$CountDown reached its end value of (<0> or a value less than <0>) (elapsed time 610ms, remaining time 1390ms)

Upvotes: 3

Related Questions