繁体   English   中英

UDP算法的集成测试

[英]Integration testing of a UDP algorithm

我需要创建一个集成测试,以演示将UDP数据包成功发送到远程软件的过程。 该远程软件无法在测试环境中使用(它是旧版但仍受支持的版本),并且不受我的控制,因此我认为我应该进行测试,至少可以证明该命令能够按预期进行。 阅读此问题的答案后 ,我将代码设置如下:

public void TestRemoteCommand()
    {
        //A "strategy picker"; will instantiate a version-specific
        //implementation, using a UdpClient in this case
        var communicator = new NotifyCommunicator(IPAddress.Loopback.ToString(), "1.0");
        const string message = "REMOTE COMMAND";
        const int port = <specific port the actual remote software listens on>;
        var receivingEndpoint = new IPEndPoint(IPAddress.Loopback, port);

        //my test listener; will listen on the same port already connected to by
        //the communicator's UdpClient (set up without sharing)
        var client = new UdpClient();
        client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        client.Client.Bind(receivingEndpoint);

        //Results in the UDP diagram being sent
        communicator.SendRemoteCommand();

        //This assertion always fails
        Assert.IsTrue(client.Available > 0);
        var result = client.Receive(ref receivingEndpoint);

        Assert.AreEqual(result.Select(b => (char)b).ToArray(), message.ToCharArray());
    }

但是,这不起作用,如上面的注释所示。 有人看到我在这里想念的吗?

断言发生得太快了。 您正在发送数据,并立即检查要接收的数据。 它总是会失败,因为往返于客户端的往返时间远远超过程序执行下一行所需的纳秒级时间。 在某处放置一个wait语句,或创建一个while循环以检查数据,休眠几毫秒,然后再次检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM