Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save aside an object in awaitility callback?

My code calls a server and get a old-response.

Then I want to poll until I get a different response from the server (aka new-response).

I I use while loop I can hold the new-response and use it after polling.

If I use awaitility how can I get the new-response easily?

Here is my code:

public Version waitForNewConfig() throws Exception {
    Version oldVersion = deploymentClient.getCurrentConfigVersion(appName);
    await().atMost(1, MINUTES).pollInterval(5, SECONDS).until(newVersionIsReady(oldVersion));
    Version newVersion = deploymentClient.getCurrentConfigVersion(appName);

}

private Callable<Boolean> newVersionIsReady(Version oldVersion) {
    return new Callable<Boolean>() {
        public Boolean call() throws Exception {
            Version newVersion = deploymentClient.getCurrentConfigVersion(appName);

            return !oldVersion.equals(newVersion);
        }
    };
}
like image 747
Elad Benda2 Avatar asked Dec 11 '22 15:12

Elad Benda2


2 Answers

You can use ConditionFactory.until(Callable[T], Predicate[T])

Eg:

Callable<MyObject> supplier = () -> queryForMyObject();
Predicate<MyObject> predicate = myObject -> myObject.getFooCount() > 3;

MyObject myObject = Awaitility.await()
   .atMost(1, TimeUnit.MINUTE)
   .pollInterval(Duration.ofSeconds(5))
   .until(supplier, predicate);

doStuff(myObject);
like image 121
lance-java Avatar answered Dec 13 '22 04:12

lance-java


One way is to make a specialized Callable implementation that remembers it :

public Version waitForNewConfig() throws Exception {
    NewVersionIsReady newVersionIsReady = new NewVersionIsReady(deploymentClient.getCurrentConfigVersion(appName));
    await().atMost(1, MINUTES).pollInterval(5, SECONDS).until(newVersionIsReady);

    return newVersionIsReady.getNewVersion();
}

private final class NewVersionIsReady implements Callable<Boolean> {
    private final Version oldVersion;
    private Version newVersion;

    private NewVersionIsReady(Version oldVersion) {
        this.oldVersion = oldVersion;
    }

    public Boolean call() throws Exception {
        Version newVersion = deploymentClient.getCurrentConfigVersion(appName);

        return !oldVersion.equals(newVersion);
    }

    public Version getNewVersion() {
        return newVersion;
    }
}

Another is to store it in a container (as an example I use an array)

public Version waitForNewConfig() throws Exception {
    Version[] currentVersionHolder = new Version[1];
    Version oldVersion = deploymentClient.getCurrentConfigVersion(appName);
    await().atMost(1, MINUTES).pollInterval(5, SECONDS).until(() -> {
        Version newVersion = deploymentClient.getCurrentConfigVersion(appName);
        currentVersionHolder[0] = newVersion;
        return !oldVersion.equals(newVersion);
    });

    return currentVersionHolder[0];
}

If you don't use java 8 yet, you can do it using an anonymous inner class as well.

like image 36
bowmore Avatar answered Dec 13 '22 03:12

bowmore