In unit testing, there are times were the SUT has to be exercised asynchronously.

How can we wait for the exercise to complete execution?

  • An instance of the SUT can be created on the main thread.
  • The main (waiting) thread spins in user mode while starting the exercise of the SUT asynchronously.
  • Once the result has been received, the execution continues on the main thread.
  • The assertion takes place.

The SpinWait synchronization type contains a method named SpinUntil which works perfect for the described scenario.

// Fixture setup
var sut = new ObjectLocalStorage();
sut.Set(@object, expected);
object result = null;

// Exercise system
new Task(() => result = sut.Get(@object)).Start();
SpinWait.SpinUntil(() => result != null);

// Verify outcome
Assert.Equal(expected, result);
// Teardown
  • Once the Task has been created it is immediately scheduled for execution by calling the Start method.
  • As long as the unit test runs fast, the waiting thread spins in user mode, which is a good thing.

There is also a SpinUntil overload accepting a TimeSpan timeout.