spock.util.concurrent
Class AsyncConditions

java.lang.Object
  extended by 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()
 


Constructor Summary
AsyncConditions()
          Same as AsyncConditions(1).
AsyncConditions(int numEvalBlocks)
          Instantiates an AsyncConditions instance with the specified number of evaluate blocks.
 
Method Summary
 void await()
          Same as await(1, TimeUnit.SECONDS).
 void await(int timeout, TimeUnit unit)
          Waits until all evaluate blocks have completed or the specified timeout expires.
 void evaluate(Runnable block)
          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 Detail

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 Detail

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
Throws:
Throwable

await

public void await()
           throws InterruptedException,
                  Throwable
Same as await(1, TimeUnit.SECONDS).

Throws:
InterruptedException
Throwable

await

public void await(int timeout,
                  TimeUnit unit)
           throws InterruptedException,
                  Throwable
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