Exercising the SUT asynchronously
28 October 2012
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.
- Once the
Taskhas been created it is immediately scheduled for execution by calling the
- 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.