public void TestReadConnection(string testData) { _mutex.WaitOne(30000); var are = new AutoResetEvent(false); // Start the receiving side var receiveTask = Task.Run(async() => { using (var reader = new TcpReadConnection(null, Port, _defaultTimeout, 10, new List <byte>() { 0x02 }, new List <byte>() { 0x03 })) { reader.StartListening(); are.Set(); var result = await reader.WaitForData(_defaultTimeout); await reader.SendData("ACK", Encoding.UTF8); result.Should().Be(testData); } }); // Wait for the receiver to start listening are.WaitOne(5000); // Start sending data var sendTask = Task.Run(async() => { using (var sendConnection = new TcpSendConnection(null, LocalHost, Port, TimeSpan.FromSeconds(30), 10, new List <byte>() { 0x02 }, new List <byte>() { 0x03 })) { await sendConnection.SendData(testData, Encoding.UTF8); sendConnection.ReceiveData().Should().Be("ACK"); } }); if (Task.WaitAll(new[] { receiveTask, sendTask }, 30000) == false) { throw new TimeoutException("Send or receive task has not completed within the allotted time"); } // Make sure that exceptions on other thread tasks fail the unit test if (receiveTask.Exception != null) { throw receiveTask.Exception; } if (sendTask.Exception != null) { throw sendTask.Exception; } }
/// <summary> /// Returns a task that synchronously waits for the data. /// </summary> /// <param name="readConnection">Tcp Read connection</param> /// <param name="timeout">Timeout to wait for data to be received</param> /// <returns></returns> public static async Task <string> WaitForData(this TcpReadConnection readConnection, TimeSpan timeout) { return(await Task.Run(() => { string result = null; var dataReceivedAutoResetEvent = new AutoResetEvent(false); readConnection.OnDataReceived = data => { result = data; dataReceivedAutoResetEvent.Set(); }; if (!dataReceivedAutoResetEvent.WaitOne(timeout)) { throw new NetworkingException("Failed waiting for data within the given timeout", NetworkingException.NetworkingExceptionTypeEnum.ReadTimeout); } return result; })); }
public void TestReadConnectionAsyncMultipleRequestsAtOnce() { var testData = "qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiop"; _mutex.WaitOne(30000); var receiverReady = new AutoResetEvent(false); var cancelReceiverTokenSource = new CancellationTokenSource(); // Start the receiving side var receiveTask = Task.Run(() => { using (var reader = new TcpReadConnection(null, Port, _defaultTimeout, 10, new List <byte>() { 0x02 }, new List <byte>() { 0x03 })) { reader.OnDataReceived = (returnedData) => { returnedData.Should().Be(testData); reader.SendData("ACK", Encoding.UTF8).Wait(_defaultTimeout); }; reader.StartListening(); receiverReady.Set(); while (true) { if (cancelReceiverTokenSource.IsCancellationRequested) { return; } } } }); // Start sending data var sendTask = Task.Run(async() => { for (int i = 0; i < 100; i++) { // Wait for the receiver to start listening receiverReady.WaitOne(5000); using (var sendConnection = new TcpSendConnection(null, LocalHost, Port, TimeSpan.FromSeconds(30), 10, new List <byte>() { 0x02 }, new List <byte>() { 0x03 })) { await sendConnection.SendData(testData, Encoding.UTF8); sendConnection.ReceiveData().Should().Be("ACK"); } using (var sendConnection = new TcpSendConnection(null, LocalHost, Port, TimeSpan.FromSeconds(30), 10, new List <byte>() { 0x02 }, new List <byte>() { 0x03 })) { await sendConnection.SendData(testData, Encoding.UTF8); sendConnection.ReceiveData().Should().Be("ACK"); } await Task.Delay(30000); } }); if (Task.WaitAll(new[] { receiveTask, sendTask }, TimeSpan.FromMinutes(30)) == false) { throw new TimeoutException("Send or receive task has not completed within the allotted time"); } // Make sure that exceptions on other thread tasks fail the unit test if (receiveTask.Exception != null) { throw receiveTask.Exception; } if (sendTask.Exception != null) { throw sendTask.Exception; } }