简体   繁体   English

UDP算法的集成测试

[英]Integration testing of a UDP algorithm

I need to create an integration test that demonstrates successful sending of a UDP packet to a remote piece of software. 我需要创建一个集成测试,以演示将UDP数据包成功发送到远程软件的过程。 The remote software is unavailable in a test environent (it's a legacy but still supported version) and not under my control, so I thought I'd set up a test that at least proves the command's going out as expected. 该远程软件无法在测试环境中使用(它是旧版但仍受支持的版本),并且不受我的控制,因此我认为我应该进行测试,至少可以证明该命令能够按预期进行。 After reading this question's answers , I set up my code as follows: 阅读此问题的答案后 ,我将代码设置如下:

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());
    }

However, this isn't working as in the comment above. 但是,这不起作用,如上面的注释所示。 Anybody see what I'm missing here? 有人看到我在这里想念的吗?

The Assertion is happening way too fast. 断言发生得太快了。 You're sending data, and instantly checking for data to receive. 您正在发送数据,并立即检查要接收的数据。 It will always fail since the round trip time to the client and back is way more than the nanoseconds it takes your program to execute the next line. 它总是会失败,因为往返于客户端的往返时间远远超过程序执行下一行所需的纳秒级时间。 Put a wait statement in there somewhere, or create a while loop to check for data, sleep for a few ms and then check again. 在某处放置一个wait语句,或创建一个while循环以检查数据,休眠几毫秒,然后再次检查。

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

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