public void SendReceiveAndVerify_keeps_SendTimeout_ReceiveTimeout()
        {
            var endpoint            = TestHelpers.CreateEndpointForTransport(RedFoxTransport.Tcp);
            var socketConfiguration = new SocketConfiguration
            {
                SendTimeout    = TimeSpan.FromSeconds(10),
                ReceiveTimeout = TimeSpan.FromSeconds(20)
            };

            var server = new TcpListener(IPAddress.Loopback, endpoint.Port);

            server.Start();
            try
            {
                server.AcceptTcpClientAsync();

                var nodeGreetingVerifier = new NodeGreetingMessageVerifier(NodeType.Requester, NodeType.Responder);

                var socket = (TcpSocket) new SocketFactory().CreateAndConnectAsync(endpoint, NodeType.Responder, socketConfiguration);
                Assert.Throws <TimeoutException>(() => nodeGreetingVerifier.SendReceiveAndVerify(socket, TimeSpan.FromSeconds(2)));

                Assert.AreEqual(socketConfiguration.SendTimeout.ToMillisOrZero(), socket.TcpClient.SendTimeout);
                Assert.AreEqual(socketConfiguration.ReceiveTimeout.ToMillisOrZero(), socket.TcpClient.ReceiveTimeout);
            }
            finally
            {
                server.Stop();
            }
        }
        public void SendReceiveAndVerify_times_out_after_ConnectTimeout()
        {
            var endpoint            = TestHelpers.CreateEndpointForTransport(RedFoxTransport.Tcp);
            var socketConfiguration = new SocketConfiguration {
                ConnectTimeout = TimeSpan.FromSeconds(2)
            };

            var server = new TcpListener(IPAddress.Loopback, endpoint.Port);

            server.Start();
            try
            {
                server.AcceptTcpClientAsync();

                var nodeGreetingVerifier = new NodeGreetingMessageVerifier(NodeType.Publisher, NodeType.Subscriber);

                var socket = new SocketFactory().CreateAndConnectAsync(endpoint, NodeType.Subscriber, socketConfiguration);

                var sw   = Stopwatch.StartNew();
                var task = Task.Factory.StartNew(() =>
                {
                    nodeGreetingVerifier.SendReceiveAndVerify(socket, socketConfiguration.ConnectTimeout);
                });

                var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(5));
                try
                {
                    task.Wait(cancellationToken.Token);
                }
                catch (AggregateException ex)
                {
                    Assert.AreEqual(typeof(TimeoutException), ex.InnerExceptions.Single().GetType());
                }
                Assert.Greater(sw.Elapsed, socketConfiguration.ConnectTimeout);
            }
            finally
            {
                server.Stop();
            }
        }
        public void SendReceiveAndVerify_times_out_after_ConnectTimeout()
        {
            var endpoint = TestHelpers.CreateEndpointForTransport(RedFoxTransport.Tcp);
            var socketConfiguration = new SocketConfiguration { ConnectTimeout = TimeSpan.FromSeconds(2) };

            var server = new TcpListener(IPAddress.Loopback, endpoint.Port);
            server.Start();
            try
            {
                server.AcceptTcpClientAsync();

                var nodeGreetingVerifier = new NodeGreetingMessageVerifier(NodeType.Publisher, NodeType.Subscriber);

                var socket = new SocketFactory().CreateAndConnectAsync(endpoint, NodeType.Subscriber, socketConfiguration);

                var sw = Stopwatch.StartNew();
                var task = Task.Factory.StartNew(() =>
                {
                    nodeGreetingVerifier.SendReceiveAndVerify(socket, socketConfiguration.ConnectTimeout);
                });

                var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(5));
                try
                {
                    task.Wait(cancellationToken.Token);
                }
                catch (AggregateException ex)
                {
                    Assert.AreEqual(typeof(TimeoutException), ex.InnerExceptions.Single().GetType());
                }
                Assert.Greater(sw.Elapsed, socketConfiguration.ConnectTimeout);
            }
            finally
            {
                server.Stop();
            }
        }
        public void SendReceiveAndVerify_keeps_SendTimeout_ReceiveTimeout()
        {
            var endpoint = TestHelpers.CreateEndpointForTransport(RedFoxTransport.Tcp);
            var socketConfiguration = new SocketConfiguration
            {
                SendTimeout = TimeSpan.FromSeconds(10),
                ReceiveTimeout = TimeSpan.FromSeconds(20)
            };

            var server = new TcpListener(IPAddress.Loopback, endpoint.Port);
            server.Start();
            try
            {
                server.AcceptTcpClientAsync();

                var nodeGreetingVerifier = new NodeGreetingMessageVerifier(NodeType.Requester, NodeType.Responder);

                var socket = (TcpSocket)new SocketFactory().CreateAndConnectAsync(endpoint, NodeType.Responder, socketConfiguration);
                Assert.Throws<TimeoutException>(() => nodeGreetingVerifier.SendReceiveAndVerify(socket, TimeSpan.FromSeconds(2)));

                Assert.AreEqual(socketConfiguration.SendTimeout.ToMillisOrZero(), socket.TcpClient.SendTimeout);
                Assert.AreEqual(socketConfiguration.ReceiveTimeout.ToMillisOrZero(), socket.TcpClient.ReceiveTimeout);
            }
            finally
            {
                server.Stop();
            }
        }