Visual Studio Unit Testing an asynchronous event callback

Problem …
Long title … simple problem: I am writing automated unit test cases for an asynchronous method call which returns back via an event handler (a callback).

If I had architected the method that I am calling I would have used the standard Begin/End methods to handle the asynchronous calls but I am using a pre-written source code so I need to adhere to its methodology.

Solution …
Nonetheless, I needed a way to have a TestMethod make the asynchronous method call and then somehow get the results back from the event handler. I was not (am still not) aware of anything built-in to NUnit or Visual Studio Unit Testing to handle this
so I figured I was on my own.

I figured that all I really needed was a way to sleep the test method until the event handler is called and then signal it to continue and analyze the data returned from the event. Signal. I had heard that before when talking about Thread Synchronization … threading … that is what the event callback is … duh. I need a thread signal!

Enter AutoResetEvent …
Very fortunately for me, .NET has built-in just such a signal using a class called AutoResetEvent (System.Threading). Once I realized this, the code was frighteningly simple. One thread creates the signal object (initializing its state to un-signaled) and waits on it. The second thread sets the signal and the initial thread continues.

Here’s some code:

  1. using System.Threading;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. namespace MyTests
  4. {
  5. [TestClass]
  6. public class MyTestClass
  7. {
  8. AutoResetEvent _TestTrigger;
  9. DataType _MyData;
  10. [TestMethod]
  11. public void MyTestMethod()
  12. {
  13. this._TestTrigger = new AutoResetEvent(false);
  14. MyAsyncCall();
  15. this._TestTrigger.WaitOne();
  16. Assert.AreEqual()
  17. }
  18. private void My_EventHandler()
  19. {
  20. this._MyData = result from event handler;
  21. this._TestTrigger.Set();
  22. }
  23. }
  24. }

You can make modifications to this so that the calling method does not wait indefinitely as well as other handy mods. If you come up with anything cool please drop a comment here so everyone can share in your find!

Advertisements

4 thoughts on “Visual Studio Unit Testing an asynchronous event callback

  1. I think that’s a great way to handle this problem. That’s a cool idea for testing things like WinWF and WCF async services, too! I must remember that object in the future. Thanks, J!

  2. I would suggest to use a ManualResetEvent instead of an AutoResetEvent because it might happen that MyAsyncCall has already finished its work before WaitOne() is called. Then WaitOne() would wait forever because the AutoResetEvent returns to the unsignalled state immediately after it has been set. However, the ManualResetEvent remains in the signalled state until it is manually reset. So the WaitOne() would not block.
    Furthermore, I would specify a timeout in the WaitOne() method and check its return value, so that one can make the test fail if the method did not finish after a certain period of time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s