Jason님의 프로필Jason's .NET Blog블로그리스트 도구 도움말

블로그


    7월 14일

    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!

    댓글 (1개)

    잠시만 기다려 주세요...
    죄송합니다. 입력한 댓글이 너무 깁니다. 내용을 줄여 보세요.
    입력한 내용이 없습니다. 다시 시도해 보세요.
    죄송합니다. 지금은 댓글을 추가할 수 없습니다. 나중에 다시 시도해 보세요.
    댓글을 추가하려면 부모님의 사용 허락이 필요합니다. 허용 요청
    부모님이 댓글 기능을 해제한 상태입니다.
    죄송합니다. 지금은 댓글을 삭제할 수 없습니다. 나중에 다시 시도해 보세요.
    하루에 남길 수 있는 댓글의 최대 한도를 초과했습니다. 24시간 후에 다시 시도해 보세요.
    회원님의 계정은 다른 사용자에게 스팸 메일을 보낼 수 있다고 여겨지므로 댓글 기능이 비활성화되어 있습니다. 이 설정에 문제가 있다고 생각되면 Windows Live 지원에 문의하시기 바랍니다.
    댓글을 남기려면 아래 보안 검사를 완료해야 합니다.
    보안 검사에 입력한 글자는 그림 또는 오디오에 있는 글자와 일치해야 합니다.

    댓글을 추가하려면 Windows Live ID로 로그인하세요. 핫메일, 메신저 또는 Xbox LIVE를 사용하는 경우 해당 계정을 Windows Live ID로 사용할 수 있습니다.로그인


    Windows Live ID가 없으신가요? 등록

    알 수 없음님의 사진
    Suman Chakrabarti 님이 남긴 글:
    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!
    7월 17일

    트랙백

    이 블로그를 참조하는 웹 로그
    • 없음