0
원격 패킷에 UDP 패킷을 성공적으로 전송했음을 입증하는 통합 테스트를 작성해야합니다. 원격 소프트웨어는 테스트 환경 (기존 버전이지만 여전히 지원되는 버전)에서 사용할 수없고 제 권한하에 있지 않으므로 명령을 예상대로 진행할 수 있음을 증명하는 테스트를 설정해야한다고 생각했습니다.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());
}
그러나,이 위의 주석처럼 작동하지 않는 경우 다음과 같이 this question's answers을 읽은 후, 나는 내 코드를 설정합니다. 아무도 내가 여기에서 놓친 것을 본다?
그것의 일부로 밝혀졌습니다. 나는 더 이상 그걸 두들 겼고 특정 항구 대신 모든 항구에서 듣고 있다는 것을 알았다. 그것은 작동하지 않았다. 그러나 예, 패킷이 하드웨어 계층을 통해 수신 버킷에 들어 갔는지 확인하기 위해 빠른 Thread.Sleep() 문을 넣어야했습니다. 한 번에 하나의 테스트를 실행해도 문제가 없었지만 실제로는 실패했습니다. – KeithS