public void ShouldSendDataByTcpCorrectly() { //Arrange var result = new byte[0]; var testData = new byte[] { 1, 2, 3, 4, 5 }; var tcpReceiverMock = new Mock <IReceiver>(); tcpReceiverMock.Setup(x => x.ReceivedData(It.IsAny <byte[]>())) .Callback <byte[]>(x => result = x); var port = 1234; var listener = new TcpRawListener(port); //Act Task.Run(() => listener.StartListening(tcpReceiverMock.Object)); using (var client = new TcpRawClient(new IpAddress(IPAddress.Loopback.ToString(), port))) { client.Send(testData); } while (result.Length == 0) { } listener.StopListening(); //Assert Assert.That(result, Is.EqualTo(testData)); }
public void ShouldReceiveManyRequests() { //Arrange var receivedRequests = new List <byte[]>(); var testRequests = new List <byte[]>() { Encoding.UTF8.GetBytes("test"), Encoding.UTF8.GetBytes("abc"), Encoding.UTF8.GetBytes("xyz"), }; var tcpReceiverMock = new Mock <IReceiver>(); tcpReceiverMock.Setup(x => x.ReceivedData(It.IsAny <byte[]>())) .Callback <byte[]>(x => receivedRequests.Add(x)); var port = 1234; var listener = new TcpRawListener(port); //Act Task.Run(() => listener.StartListening(tcpReceiverMock.Object)); testRequests.ForEach(x => { using (var client = new TcpRawClient(new IpAddress(IPAddress.Loopback.ToString(), port))) { client.Send(x); } }); while (receivedRequests.Count < testRequests.Count) { } listener.StopListening(); //Assert Assert.That(receivedRequests, Is.EqualTo(testRequests)); Assert.That(receivedRequests.Count, Is.EqualTo(testRequests.Count)); }