开发者

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. 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 str开发者_开发百科ing 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜