moodmosaic (Nikos Baxevanis)
Blog - About - Open Source - Bookmarks - Tags

Exercising the SUT asynchronously

Sunday, 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?

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.

━━━

This post has been filed under #xunit.net

Feedback

Have feedback on this page? Let @nikosbaxevanis know on twitter. Need help or found a bug? Contact me.


© 2011-2018 Nikos Baxevanis. All written content on this site is provided under a Creative Commons ShareAlike license. All code is provided under a MIT license unless otherwise stated.