示例#1
0
        public void TestReadConnectionDeadlockAsync()
        {
            var testData = "qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiop";

            _mutex.WaitOne(30000);
            var are = new AutoResetEvent(false);

            // Start the receiving side
            var receiveTask = Task.Run(async() =>
            {
                using (var reader = new TcpReadConnectionDeadlockSimulator(null, Port, _defaultTimeout, 10, new List <byte>()
                {
                    0x02
                }, new List <byte>()
                {
                    0x03
                }))
                {
                    reader.StartListening();
                    are.Set();
                    await Task.Delay(5000);
                }
            });

            // 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;
            }
        }
        public void TestSenderMultiRequest()
        {
            var cancelTokenSrc  = new CancellationTokenSource();
            var testData        = "QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890QWERTYUIOPasdfghjklzxcvbnm1234567890";
            var are             = new AutoResetEvent(false);
            var senderSemaphore = new SemaphoreSlim(10, 10);

            // Start the receiving side
            var receiveTask = Task.Run(async() =>
            {
                TcpListener receiverClient = new TcpListener(IPAddress.Any, Port);
                receiverClient.Start();
                are.Set();

                while (true)
                {
                    if (receiverClient.Pending())
                    {
                        using var mock   = new TcpReaderMock(await receiverClient.AcceptTcpClientAsync());
                        var returnedData = mock.ReadTcpData();
                        returnedData.Should().Be(testData);
                        senderSemaphore.Release();
                    }
                    else
                    {
                        if (cancelTokenSrc.IsCancellationRequested)
                        {
                            return;
                        }

                        await Task.Delay(10);
                    }
                }
            }, cancelTokenSrc.Token);

            // Wait for the receiver to start listening
            are.WaitOne(5000);

            // Start sending data
            var sendTask = Task.Run(async() =>
            {
                for (int i = 0; i < 1000; i++)
                {
                    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);
                    }

                    await senderSemaphore.WaitAsync(5000, cancelTokenSrc.Token);
                }

                cancelTokenSrc.Cancel();
            });

            if (Task.WaitAll(new[] { receiveTask, sendTask }, TimeSpan.FromMinutes(10)) == 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;
            }
        }
示例#3
0
        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;
            }
        }
        public void TestSendWithoutLength(string testData)
        {
            _mutex.WaitOne(30000);
            var are = new AutoResetEvent(false);

            // Start the receiving side
            var receiveTask = Task.Run(async() =>
            {
                TcpListener receiverClient = new TcpListener(IPAddress.Any, Port);
                receiverClient.Start();
                are.Set();

                for (var i = 0; i < 500; i++)
                {
                    if (receiverClient.Pending())
                    {
                        // We have a new pending request. Validate the received data, send a response and close the connection
                        using var mock   = new TcpReaderMock(await receiverClient.AcceptTcpClientAsync());
                        var returnedData = mock.ReadTcpData();
                        returnedData.Should().Be(testData);
                        mock.SendData("ACK");
                        receiverClient.Stop();
                        return;
                    }

                    await Task.Delay(10);
                }

                receiverClient.Stop();
                throw new Exception("Experienced timeout on receiving data");
            });

            // 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;
            }
        }