0

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

ただし、これは上記のコメントのようには機能しません。ここで私が見逃しているものを見た人はいますか?

4

1 に答える 1

0

アサーションの発生が速すぎます。データを送信していて、受信するデータを即座に確認しています。クライアントへの往復時間は、プログラムが次の行を実行するのにかかるナノ秒よりもはるかに長いため、常に失敗します。どこかに待機ステートメントを配置するか、while ループを作成してデータをチェックし、数ミリ秒スリープしてから再度チェックします。

于 2011-02-02T19:23:23.573 に答える