Package spock.util.concurrent
Class PollingConditions
java.lang.Object
spock.util.concurrent.PollingConditions
@Beta public class PollingConditions extends Object
Repeatedly evaluates one or more conditions until they are satisfied or a timeout has elapsed.
The timeout and delays between evaluation attempts are configurable. All durations are in seconds.
Usage example:
def conditions = new PollingConditions(timeout: 10, initialDelay: 1.5, factor: 1.25)
def machine = new Machine()
when:
machine.start()
then:
conditions.eventually {
assert machine.temperature >= 100
assert machine.efficiency >= 0.9
}
Warning! Avoiding assert keyword in the clojure is only possible if the conditions object type is known
during compilation (no "def" on the left side):
PollingConditions conditions = new PollingConditions(timeout: 10, initialDelay: 1.5, factor: 1.25)
-
Constructor Summary
Constructors Constructor Description PollingConditions() -
Method Summary
Modifier and Type Method Description voidcall(double seconds, Closure<?> conditions)Alias forwithin(double, groovy.lang.Closure).voidcall(Closure<?> conditions)Alias foreventually(groovy.lang.Closure).voideventually(Closure<?> conditions)Repeatedly evaluates the specified conditions until they are satisfied or the timeout has elapsed.doublegetDelay()Returns the delay (in seconds) between successive evaluations of the conditions.doublegetFactor()Returns the factor by which the delay grows (or shrinks) after each evaluation of the conditions.doublegetInitialDelay()Returns the initial delay (in seconds) before first evaluating the conditions.doublegetTimeout()Returns the timeout (in seconds) until which the conditions have to be satisfied.voidsetDelay(double seconds)Sets the delay (in seconds) between successive evaluations of the conditions.voidsetFactor(double factor)Sets the factor by which the delay grows (or shrinks) after each evaluation of the conditions.voidsetInitialDelay(double seconds)Sets the initial delay (in seconds) before first evaluating the conditions.voidsetTimeout(double seconds)Sets the timeout (in seconds) until which the conditions have to be satisfied.voidwithin(double seconds, Closure<?> conditions)Repeatedly evaluates the specified conditions until they are satisfied or the specified timeout (in seconds) has elapsed.
-
Constructor Details
-
PollingConditions
public PollingConditions()
-
-
Method Details
-
getTimeout
public double getTimeout()Returns the timeout (in seconds) until which the conditions have to be satisfied. Defaults to one second.- Returns:
- the timeout (in seconds) until which the conditions have to be satisfied
-
setTimeout
public void setTimeout(double seconds)Sets the timeout (in seconds) until which the conditions have to be satisfied. Defaults to one second.- Parameters:
seconds- the timeout (in seconds) until which the conditions have to be satisfied
-
getInitialDelay
public double getInitialDelay()Returns the initial delay (in seconds) before first evaluating the conditions. Defaults to zero seconds. -
setInitialDelay
public void setInitialDelay(double seconds)Sets the initial delay (in seconds) before first evaluating the conditions. Defaults to zero seconds.- Parameters:
seconds- the initial delay (in seconds) before first evaluating the conditions
-
getDelay
public double getDelay()Returns the delay (in seconds) between successive evaluations of the conditions. Defaults to 0.1 seconds. -
setDelay
public void setDelay(double seconds)Sets the delay (in seconds) between successive evaluations of the conditions. Defaults to 0.1 seconds.- Parameters:
seconds- the delay (in seconds) between successive evaluations of the conditions.
-
getFactor
public double getFactor()Returns the factor by which the delay grows (or shrinks) after each evaluation of the conditions. Defaults to 1. -
setFactor
public void setFactor(double factor)Sets the factor by which the delay grows (or shrinks) after each evaluation of the conditions. Defaults to 1.- Parameters:
factor- the factor by which the delay grows (or shrinks) after each evaluation of the conditions
-
eventually
Repeatedly evaluates the specified conditions until they are satisfied or the timeout has elapsed.- Parameters:
conditions- the conditions to evaluate- Throws:
InterruptedException- if evaluation is interrupted
-
within
Repeatedly evaluates the specified conditions until they are satisfied or the specified timeout (in seconds) has elapsed.- Parameters:
conditions- the conditions to evaluate- Throws:
InterruptedException- if evaluation is interrupted
-
call
Alias foreventually(groovy.lang.Closure).- Throws:
InterruptedException
-
call
Alias forwithin(double, groovy.lang.Closure).- Throws:
InterruptedException
-