Class 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 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 interrupted
        Throwable - the first exception thrown by an evaluate block
      • await

        @Deprecated
        public void await​(int value,
                          TimeUnit unit)
                   throws Throwable
        Deprecated.
        use await(double) instead
        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