What is the best way to unit test an asynchronous method? What is the best way to unit test an asynchronous method? multithreading multithreading

What is the best way to unit test an asynchronous method?


I typically use an anonymous delegate and a waithandle. FOr example I have a function in my presenter called SetRemoteTableName. When the name is set, it also raises an event. I want to test that event, which is raised asynchronously. The test looks like this:

[TestMethod][WorkItem(244)][Description("Ensures calling SetRemoteTableName with a valid name works               AND raises the RemoteTableNameChange event")]public void SetRemoteTableNamePositive(){  string expected = "TestRemoteTableName";  string actual = string.Empty;  AutoResetEvent are = new AutoResetEvent(false);  SQLCECollectorPresenter presenter = new SQLCECollectorPresenter();  presenter.RemoteTableNameChange += new EventHandler<GenericEventArg<string>>(    delegate(object o, GenericEventArg<string> a)    {      actual = a.Value;      are.Set();    });  presenter.SetRemoteTableName(expected);  Assert.IsTrue(are.WaitOne(1000, false), "Event never fired");  Assert.AreEqual(actual, expected);}


Split the code so that the logic is in a synchronous bit of code that is called by a thin asynchronous wrapper.

Then most of your unit tests can test the synchronous code.


is there a better way than simply sleeping for a bit and then checking for a flag that is set by the callback?

Replace the flag with a wait handle. Instead of setting the flag, set the wait handle. Instead of sleeping and then checking whether the flag is set, wait on the wait handle ... and wait with a timeout, so that if you wake up because of timer expiry instead of waking up because the handle on which you were waiting was fired by the callback, then you know that the test failed (i.e. the callback wasn't invoked within the timeout period).