开发者

How to test that my callback is called from an event?

I have the following method signature:

IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback cal开发者_StackOverflow社区lback, object state)

The callback is hooked into the Completed event like this:

public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
{
  SocketAsyncEventArgs args = new SocketAsyncEventArgs()
  WriteAsyncResult ar = new WriteAsyncResult();
  ar.Callback = callback;
  args.UserToken = ar;
  // ...
  if (callback != null)
      args.Completed += OnSendAsyncComplete;
  // ...

  socket.SendAsync(args);
  // ...
}

private void OnSendAsyncComplete(object sender, SocketAsyncEventArgs e)
{
  var ar = e.UserToken as WriteAsyncResult;
  ar.Callback(ar);
}

How do I test that my callback is called whenever the args.Completed event when it is raised.

I know that I have to raise it myself but how do I test that my callback was called?


Assuming that you can find a way to raise the completed event yourself (perhaps using an additional abstraction layer), you can do something like this:

A simple way you be to have your async callback in a small class with a property named CallbackCalled. When the callback is called, you set this property to true.

In your test, you call BeginWrite passing the callback method from an instance of the class and then manually raise the event and assert that after that the CallbackCalled property is true.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜