spock.util.concurrent
T - the variable's typepublic class BlockingVariable<T> extends Object
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 and Description |
|---|
BlockingVariable()
Same as BlockingVariable(1).
|
BlockingVariable(double timeout)
Instantiates a BlockingVariable with the specified timeout in seconds.
|
BlockingVariable(int timeout,
TimeUnit unit)
Deprecated.
use
BlockingVariable(double) instead |
| Modifier and Type | Method and Description |
|---|---|
T |
get()
Blocks until a value has been set for this variable, or a timeout expires.
|
double |
getTimeout()
Returns the timeout (in seconds).
|
void |
set(T value)
Sets a value for this variable.
|
public BlockingVariable()
public BlockingVariable(double timeout)
timeout - the timeout (in seconds) for calls to get().@Deprecated public BlockingVariable(int timeout, TimeUnit unit)
BlockingVariable(double) insteadtimeout - the timeout for calls to get().unit - the time unitpublic double getTimeout()
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