Published 28th of October, 2012, by Nikos Baxevanis. Tagged as xunit.net.

Exercising the SUT asynchronously

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

How can we wait for the exercise to complete execution?

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

There is also a SpinUntil overload accepting a TimeSpan timeout.



Wish to comment?

You can comment on this post on GitHub. Alternatively, you can discuss this post on Twitter or elsewhere with a permalink. Ping me with the link, and I may respond.