Package spock.util.concurrent
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()
-
-
Constructor Summary
Constructors Constructor Description AsyncConditions()
Same as AsyncConditions(1).AsyncConditions(int numEvalBlocks)
Instantiates an AsyncConditions instance with the specified number of evaluate blocks.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
await()
Same asawait(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.useawait(double)
insteadvoid
evaluate(Runnable block)
Evaluates the specified block, which is expected to contain one or more explicit conditions (i.e.
-
-
-
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
-
await
public void await(double seconds) throws 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 interruptedThrowable
- the first exception thrown by an evaluate block
-
await
@Deprecated public void await(int value, TimeUnit unit) throws Throwable
Deprecated.useawait(double)
insteadWaits 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 interruptedThrowable
- the first exception thrown by an evaluate block
-
-