org.scalatest.concurrent

trait ConductorMethods

[source: org/scalatest/concurrent/ConductorMethods.scala]

trait ConductorMethods
extends AbstractSuite with AnyRef
Trait that provides each test with access to a new Conductor via methods.

Here's an example of the use of this trait to test the ArrayBlockingQueue concurrency abstraction from java.util.concurrent:

 import org.scalatest.FunSuite
 import org.scalatest.concurrent.ConductorMethods
 import org.scalatest.matchers.ShouldMatchers
 import java.util.concurrent.ArrayBlockingQueue

 class ArrayBlockingQueueSuite extends FunSuite with ConductorMethods with ShouldMatchers {
 
   test("calling put on a full queue blocks the producer thread") {

     val buf = new ArrayBlockingQueue[Int](1)
 
     thread("producer") {
       buf put 42
       buf put 17
       beat should be (1)
     }
 
     thread("consumer") {
       waitForBeat(1)
       buf.take should be (42)
       buf.take should be (17)
     }
 
     whenFinished {
       buf should be ('empty)
     }
   }

   test("calling take on an empty queue blocks the consumer thread") {

     val buf = new ArrayBlockingQueue[Int](1)

     thread("producer") {
       waitForBeat(1)
       buf put 42
       buf put 17
     }

     thread("consumer") {
       buf.take should be (42)
       buf.take should be (17)
       beat should be (1)
     }

     whenFinished {
       buf should be ('empty)
     }
   }
 }
 

For an explanation of how these tests work, see the documentation for Conductor.

Author
Josh Cough
Bill Venners
Method Summary
protected def beat : Int
Gets the current value of the clock. Primarily useful in assert statements.
protected def isConductorFrozen : Boolean
Check if the clock has been frozen by any threads. (The only way a thread can freeze the clock is by calling withClockFrozen.)
protected def thread [T](f : => T) : java.lang.Thread
Create a new thread that will execute the given function. If the test is started, then the thread will run the function immediately. If it is not yet started, the Thread will wait to run the function until all threads are up and ready to go.
protected def thread [T](name : java.lang.String)(f : => T) : java.lang.Thread
Create a new thread that will execute the given function. If the test is started, then the thread will run the function immediately. If it is not yet started, the Thread will wait to run the function until all threads are up and ready to go.
protected def waitForBeat (beat : Int) : Unit
Force the current thread to block until the thread clock reaches the specified value, at which point the current thread is unblocked.
protected def whenFinished (fun : => Unit) : Unit
Register a function to be executed after the simulation has finished.
protected def withConductorFrozen [T](f : => T) : Unit
Run the passed function, ensuring the clock does not advance while the function is running (has not yet returned or thrown an exception).
override def withFixture (test : NoArgTest) : Unit
Creates and initializes a private instance variable with a new Conductor, ensuring it is visible to any thread, invokes the passed test function, and invokes conduct on the Conductor, if it was not already invoked by the test.
Methods inherited from AnyRef
getClass, hashCode, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Methods inherited from AbstractSuite
run (abstract), runNestedSuites (abstract), runTests (abstract), runTest (abstract), testNames (abstract), nestedSuites (abstract), tags (abstract), expectedTestCount (abstract)
Method Details
protected def thread[T](f : => T) : java.lang.Thread
Create a new thread that will execute the given function. If the test is started, then the thread will run the function immediately. If it is not yet started, the Thread will wait to run the function until all threads are up and ready to go.
Parameters
f - the function to be executed by the thread

protected def thread[T](name : java.lang.String)(f : => T) : java.lang.Thread
Create a new thread that will execute the given function. If the test is started, then the thread will run the function immediately. If it is not yet started, the Thread will wait to run the function until all threads are up and ready to go.
Parameters
name - the name of the thread
f - the function to be executed by the thread

protected def waitForBeat(beat : Int) : Unit
Force the current thread to block until the thread clock reaches the specified value, at which point the current thread is unblocked.
Parameters
c - the tick value to wait for

protected def withConductorFrozen[T](f : => T) : Unit
Run the passed function, ensuring the clock does not advance while the function is running (has not yet returned or thrown an exception).

protected def isConductorFrozen : Boolean
Check if the clock has been frozen by any threads. (The only way a thread can freeze the clock is by calling withClockFrozen.)

protected def beat : Int
Gets the current value of the clock. Primarily useful in assert statements.
Returns
the current tick value

protected def whenFinished(fun : => Unit) : Unit
Register a function to be executed after the simulation has finished.

override def withFixture(test : NoArgTest) : Unit
Creates and initializes a private instance variable with a new Conductor, ensuring it is visible to any thread, invokes the passed test function, and invokes conduct on the Conductor, if it was not already invoked by the test.
Overrides
AbstractSuite.withFixture


Copyright (C) 2001-2009 Artima, Inc. All rights reserved.