7

Happy Friday everyone, I'd like to replace Thread.sleep with Awaitility.await(),ideally with minimal changes, as I'm going over Sonar bugs in an older repository. I'm attempting to avoid usage of until of Awaitility, and it is not working out. I understand Awaitility is for async behavior and until function is a big part of it. I'm hoping someone with more experience with Awaitility could suggest a clean usage of it in this test scenario, much appreciate your input.

    //Thread.sleep(1000);
    Awaitility.await().atMost(Duration.ONE_SECOND);
    list = client.getLocation();
    Assertions.assertFalse(list.isEmpty());
user2917629
  • 883
  • 2
  • 15
  • 30
  • If you don’t want to use `until` you might as well use `sleep`. – Boris the Spider Aug 06 '21 at 21:46
  • Boris the Spider beat me to it: Q: If you've got a bunch of code that uses Thread.sleep() ... and if Thread.Sleep works ... then why do you want to change to Awaitility.await()??? What's your motivation/what are the benefits? PS: You might also want to consider these alternatives: https://www.baeldung.com/java-delay-code-execution – paulsm4 Aug 06 '21 at 21:47
  • 1
    @paulsm4 I'd like to replace it to satisfy Sonar rules. – user2917629 Aug 06 '21 at 21:51
  • Thank you for saying HAPPY FRIDAY !!! – KUTAY ZORLU Aug 06 '21 at 23:25

3 Answers3

9

Despite your intention, I encourage you to give Awaitility and until() another try. The example below test the same thing in a single one-liner (albeit it is written in several lines for improved readability):

@Test
void test_refresh() {
    Awaitility
       .await()
       .atMost(5, TimeUnit.SECONDS)
       .until(() -> { 
           List<Trp> trpList = client.getAllTrps();
           return !trpList.isEmpty();
       });
}

For more fine grained control of the polling you can take a look at methods like pollInterval() and pollDelay().

matsev
  • 32,104
  • 16
  • 121
  • 156
  • Per the OP's update, this appears to be an [XYProblem](https://xyproblem.info/). The problem isn't really "Thread.sleep()", and the solution isn't necessarily "Awaitility.await()". The *REAL* problem is that Sonar is complaining about "Thread.sleep()". – paulsm4 Aug 06 '21 at 22:11
  • I love your solution, it also addresses the for loop, thank you very much for the input, I very much appreciate it. – user2917629 Aug 06 '21 at 22:14
  • 1
    @paulsm4 thanks for your comment. I guess that this is this sonar warning that causes the problem: https://rules.sonarsource.com/java/RSPEC-2925 , it mentions `Awaitility.await().atMost().until()` as a possible solution. – matsev Aug 06 '21 at 22:17
6

Though what you did can make sense from the first look, but with a better understanding of the design of the library there are two apparent principles that you have missed.

The Awaitility library introduces a functional style usage to define properties, conditions, accumulators and transformations.

1. Terminal Operations.


  Awaitility
       .await()
       .atMost(Duration.TWO_SECONDS)

The above code will not execute, because internally what runs the chain is a call to Condition#await which is accessed only by caling ConditionFactory#until.

This call to until can be called a Terminal Operation

2. Intermediate Operations.


Operations like await, atMost , timeout and other operations only return the same instance of ConditionFactory, which will only define behaviour and actions in a lazy manner.

So in a nutshell the design expects you to:

  1. Create an instance of ConditionFactory using Awaitility#await.
  2. Define your await behaviour using the intermediate methods.
  3. Execute and/or transform using until and its variants.
SaleemKhair
  • 499
  • 3
  • 12
1

A simple replacement with Awaitility for:

Thread.sleep(200);

could be:

Awaitility.await()
      .pollDelay(Duration.ofMillis(200))
      .until(() -> true);
pbthorste
  • 309
  • 2
  • 6