|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectspock.util.concurrent.AsyncConditions
public class AsyncConditions
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 |
|---|
public AsyncConditions()
public AsyncConditions(int numEvalBlocks)
Note: One evaluate block may contain multiple conditions.
numEvalBlocks - the number of evaluate blocks that await()
should wait for| Method Detail |
|---|
public void evaluate(Runnable block)
block - the code block to evaluate
Throwable
public void await()
throws InterruptedException,
Throwable
InterruptedException
Throwable
public void await(int timeout,
TimeUnit unit)
throws InterruptedException,
Throwable
InterruptedException - if the calling thread is interrupted
Throwable - the first exception thrown by an evaluate block
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||