0

我有一个客户在下面的这个方法中报告了堆栈溢出异常(在 UDP 套接字上)。文档说 Socket.RecieveFromAsync 可以同步返回,但我无法弄清楚如何在单元测试中做到这一点。如何强制该方法同步返回以进行测试?

private void OnReceive(object sender, SocketAsyncEventArgs e)
{
    if (e != null)
    {
        var length = e.BytesTransferred;
        if (length > 0)
        {
            FireBytesReceivedFrom(datagram, length, (IPEndPoint) e.RemoteEndPoint);
        }
        e.Dispose();
    }
    var s = _socket;
    if (s != null)
    {
        var args = new SocketAsyncEventArgs();
        try
        {
            args.RemoteEndPoint = _remoteEndPoint;
            args.SetBuffer(datagram, 0, datagram.Length);
            args.Completed += OnReceive;
            if (!s.ReceiveFromAsync(args))
                OnReceive(this, args); // stack overflow occurs here
        }
        catch (ObjectDisposedException)
        {
            // this is expected after a disconnect
            args.Dispose();
        }
    }
}
4

0 回答 0