Class 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)
     
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void call​(double seconds, Closure<?> conditions)
      void call​(Closure<?> conditions)
      void eventually​(Closure<?> conditions)
      Repeatedly evaluates the specified conditions until they are satisfied or the timeout has elapsed.
      double getDelay()
      Returns the delay (in seconds) between successive evaluations of the conditions.
      double getFactor()
      Returns the factor by which the delay grows (or shrinks) after each evaluation of the conditions.
      double getInitialDelay()
      Returns the initial delay (in seconds) before first evaluating the conditions.
      double getTimeout()
      Returns the timeout (in seconds) until which the conditions have to be satisfied.
      void setDelay​(double seconds)
      Sets the delay (in seconds) between successive evaluations of the conditions.
      void setFactor​(double factor)
      Sets the factor by which the delay grows (or shrinks) after each evaluation of the conditions.
      void setInitialDelay​(double seconds)
      Sets the initial delay (in seconds) before first evaluating the conditions.
      void setTimeout​(double seconds)
      Sets the timeout (in seconds) until which the conditions have to be satisfied.
      void within​(double seconds, Closure<?> conditions)
      Repeatedly evaluates the specified conditions until they are satisfied or the specified timeout (in seconds) has elapsed.
    • Constructor Detail

      • PollingConditions

        public PollingConditions()
    • Method Detail

      • 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

        public void eventually​(Closure<?> conditions)
                        throws InterruptedException
        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

        public void within​(double seconds,
                           Closure<?> conditions)
                    throws InterruptedException
        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