0
我需要創建一個集成測試,演示如何將UDP數據包成功發送到遠程軟件。遠程軟件在測試環境中是不可用的(這是一個傳統但仍支持的版本),並且不在我的控制之下,所以我認爲我會設置一個測試,至少證明命令按預期發生。閱讀this question's answers後,我建立了我的代碼如下: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());
}
但是,這是行不通的如上評論。有人看到我在這裏失蹤了嗎?
原來是它的一部分。我再次抨擊它,並發現我在所有的端口上監聽,而不是在特定的端口上監聽,而這個端口不工作。但是,是的,我還不得不拋出一個快速的Thread.Sleep()語句來確保數據包通過硬件層進入接收存儲桶。它實際上運行良好,一次運行一個測試,但在套件中失敗。 – KeithS 2011-02-02 20:21:43