|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectspock.util.concurrent.BlockingVariable<T>
T - the variable's typepublic class BlockingVariable<T>
A statically typed variable whose get() method will block until some other thread has set a value with the set() method, or a timeout expires. Useful for verifying state in an expect- or then-block that has been captured in some other thread.
Example:
// create object under specification
def machine = new Machine()
def result = new BlockingVariable<WorkResult>
// register async callback
machine.workDone << { r ->
result.set(r)
}
when:
machine.start()
then:
// blocks until workDone callback has set result, or a timeout expires
result.get() == WorkResult.OK
cleanup:
// shut down all threads
machine?.shutdown()
| Constructor Summary | |
|---|---|
BlockingVariable()
Same as BlockingVariable(1, TimeUnit.SECONDS). |
|
BlockingVariable(int timeout)
Instantiates a BlockingVariable with the specified timeout in seconds. |
|
BlockingVariable(int timeout,
TimeUnit unit)
Instantiates a BlockingVariable with the specified timeout. |
|
| Method Summary | |
|---|---|
T |
get()
Blocks until a value has been set for this variable, or a timeout expires. |
void |
set(T value)
Sets a value for this variable. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public BlockingVariable()
public BlockingVariable(int timeout)
timeout - the timeout (seconds) for calls to get().
public BlockingVariable(int timeout,
TimeUnit unit)
timeout - the timeout for calls to get().unit - the time unit| Method Detail |
|---|
public T get()
throws InterruptedException
InterruptedException - if the calling thread is interruptedpublic void set(T value)
value - the value to be set for this variable
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||