1

使用Awaitility 4.0.1,我整理了以下轮询,轮询我的服务器 5 分钟,以 10 秒为间隔,并且运行良好:

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;

有没有办法从 Awaitility 中获得 - 在untillambda 中 - 重试尝试以及在失败之前它将执行的剩余重试次数?

4

1 回答 1

2

不,你不能从内部得到它until。但是您可以使用Condition Evaluation Listener,但它不会为您提供尝试次数或剩余尝试次数,但确实会为您提供已用时间和剩余时间。例如:

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)));

将记录:

    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)
于 2021-08-24T11:03:54.243 回答