3

While I was testing Kafka I end up with some issues around Awaitility. The goal was to test that Kafka topic doesn't contain the new records for a specified time. This is the simplified scratch of my test but it shows the problem. I expect that ConditionTimeoutException doesn't throw in this case. But it does.

public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        await("wait").during(5_000, TimeUnit.MILLISECONDS).atMost(5_000, TimeUnit.MILLISECONDS)
                .pollInterval(100, TimeUnit.MILLISECONDS)
                .until(() -> list, List::isEmpty);
}

I increased the atMost timeout to 5000 + pollInterval -> 5100 but still end up with exception. On my local machine throwing exception was stoped closed the value of atMost timeout of 5170-5180. Should I keep something in mind? Or may the test isn't correct?

Uranium
  • 41
  • 4

1 Answers1

3

In your case, you want to ensure, that the list is still empty after 5 seconds. Just remove "atMost" part. This code should work:

  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    await("wait").during(5_000, TimeUnit.MILLISECONDS)
            .pollInterval(100, TimeUnit.MILLISECONDS)
            .until(() -> list, List::isEmpty);
  }

If you want to use "during" with "atMost" together, ensure, that atMost time is greater:

  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    await("wait").during(5_000, TimeUnit.MILLISECONDS).atMost(5_500, TimeUnit.MILLISECONDS)
            .pollInterval(100, TimeUnit.MILLISECONDS)
            .until(() -> list, List::isEmpty);
  }
spx01
  • 359
  • 4
  • 6
  • 2
    Remember that the first version only works if your "during" time is less that default "atMost" time (10 seconds). – yurez Jul 12 '22 at 08:17