3

I am trying to write up a scenario for my integration testing using Awaitility package in java.

I have a call as below:

System.out.println(...)
await().atMost(10,Duration.SECONDS).until(myFunction());
and some code here....

Here, it waits for 10 seconds until the myFunction() is called.

I want something like this,my requirement is: It should keep calling myFunction() for every second for a duration of 10 seconds. Is there any better approach for this?

RanjR
  • 65
  • 2
  • 2
  • 8

2 Answers2

5

The default poll interval for awaitility is 100 milliseconds (i.e. 0.1 of a second). It's documented under Polling in the wiki.

If you want to set the poll interval to a second, then add it to the await:

with().pollInterval(Duration.ONE_SECOND).await().atMost(Duration.TEN_SECONDS).until(myFunction());

This should accomplish the polling once a second for up to 10 seconds.

Here's a very simple example:

import static org.awaitility.Awaitility.*;
import org.awaitility.Duration;
import java.util.concurrent.Callable;

public class Test {

    private Callable<Boolean> waitmeme(int timeout) {
        return new Callable<Boolean>() {
            int counter = 0;
            int limit = timeout;
            public Boolean call() throws Exception {
                System.out.println("Hello");
                counter++;
                return (counter == limit);
            }
        };
    }

    public void runit(int timeout) {
        try {
            with().pollInterval(Duration.ONE_SECOND)
                  .await()
                  .atMost(Duration.TEN_SECONDS)
                  .until(waitmeme(timeout));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) throws Exception {
        int timeout = 11;
        if (args.length >= 1)
            timeout = Integer.parseInt(args[0]);
        new Test().runit(timeout);
    }
}
Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • using Poll, does it keep calling myFunction() every 1 second? Does it stop polling once myFunction() returns me true? – RanjR Jul 19 '18 at 10:35
  • 1
    Yes, it keeps calling it every 1 second, as long as it returns false. Once it returns true it will exit, if it doesn't then after the timeout it throws a `ConditionTimeoutException`. I'll add a short example code. – Anya Shenanigans Jul 19 '18 at 11:04
  • What happens if the myFunction has some parameter like myFunction(x,y). Will it work ? – kayesh parvez Feb 18 '20 at 12:01
  • As you can see from the code, the original function returns a Callable. It’s up to you to pass in the arguments to the Callable (you can see how I passed in the timeout) and then process it within the callable. The Calllable needs to return true or false, but inside it can do whatever you need with the parameters used to create the Callable – Anya Shenanigans Feb 18 '20 at 12:07
  • That means before putting with().pollInterval(Duration.ONE_SECOND). It was calling myFunction() each 0.1 seconds until timeout OR condition returns true. Not like waiting for 10 seconds then call the function, Right? – Benjamin Mar 24 '22 at 06:52
  • Yes, if you don't change the interval, it uses 0.1 second intervals. The entire purpose of Awaitility is to synchronize asynchronous systems in a simple manner - it polls with a regular interval, with a maximum time; it's not waiting until the timeout before issuing it's first call. – Anya Shenanigans Mar 25 '22 at 09:43
-1

it should keep calling myFunction() for every second for a duration of 10 seconds

Why not just use Thread.sleep() instead?

for(int i=1;10>=i;i++){
   myFunction();
   try{
      Thread.sleep(1000);
   }catch(InterruptedException e){
      System.out.println('Thread was interrupted!');
   }
}