Class AsyncConditions

java.lang.Object
spock.util.concurrent.AsyncConditions

public class AsyncConditions extends Object
Alternative to class BlockingVariable(s) that allows to evaluate conditions in a thread other than the spec runner's thread(s). Rather than transferring state to an expect- or then-block, it is verified right where it is captured. On the upside, this can result in a more informative stack trace if the evaluation of a condition fails. On the downside, the coordination between threads is more explicit (number of evaluate blocks has to be specified if greater than one), and the usual structure of a feature method cannot be preserved (conditions are no longer located in expect-/then-block).

Example:

 // create object under specification
 def machine = new Machine()

 def conds = new AsyncConditions()

 // register async callback
 machine.workDone << { result ->
   conds.evaluate {
     assert result == WorkResult.OK
     // could add more explicit conditions here
   }
 }

 when:
 machine.start()

 then:
 // wait for the evaluation to complete
 // any exception thrown in the evaluate block will be rethrown from this method
 conds.await()

 cleanup:
 // shut down all threads
 machine?.shutdown()
 
Author:
Peter Niederwieser
  • Constructor Summary

    Constructors
    Constructor
    Description
    Same as AsyncConditions(1).
    AsyncConditions(int numEvalBlocks)
    Instantiates an AsyncConditions instance with the specified number of evaluate blocks.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Same as await(1).
    void
    await(double seconds)
    Waits until all evaluate blocks have completed or the specified timeout (in seconds) expires.
    void
    await(int value, TimeUnit unit)
    Deprecated.
    use await(double) instead
    void
    Evaluates the specified block, which is expected to contain one or more explicit conditions (i.e.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AsyncConditions

      public AsyncConditions()
      Same as AsyncConditions(1).
    • AsyncConditions

      public AsyncConditions(int numEvalBlocks)
      Instantiates an AsyncConditions instance with the specified number of evaluate blocks. await() will block until all evaluate blocks have completed or a timeout expires.

      Note: One evaluate block may contain multiple conditions.

      Parameters:
      numEvalBlocks - the number of evaluate blocks that await() should wait for
  • Method Details

    • evaluate

      public void evaluate(Runnable block)
      Evaluates the specified block, which is expected to contain one or more explicit conditions (i.e. assert statements). Any caught exception will be rethrown from await().
      Parameters:
      block - the code block to evaluate
    • await

      public void await() throws InterruptedException, Throwable
      Same as await(1).
      Throws:
      InterruptedException
      Throwable
    • await

      public void await(double seconds) throws InterruptedException, Throwable
      Waits until all evaluate blocks have completed or the specified timeout (in seconds) expires. If one of the evaluate blocks throws an exception, it is rethrown from this method.
      Parameters:
      seconds - the timeout (in seconds)
      Throws:
      InterruptedException - if the calling thread is interrupted
      Throwable - the first exception thrown by an evaluate block
    • await

      @Deprecated public void await(int value, TimeUnit unit) throws InterruptedException, Throwable
      Deprecated.
      use await(double) instead
      Waits until all evaluate blocks have completed or the specified timeout expires. If one of the evaluate blocks throws an exception, it is rethrown from this method.
      Throws:
      InterruptedException - if the calling thread is interrupted
      Throwable - the first exception thrown by an evaluate block