Inheritance: IServerDispatcher
コード例 #1
0
        public void ConcurrentConnectsBlock()
        {
            ManualResetEvent resetEvent = new ManualResetEvent(false);

            ServerDispatcher dispatcher = new ServerDispatcher(_factory.Object);
            bool secondConnectionOccurred = false;

            _factory.Setup(m => m.BeginConnect("www.cnn.com", 80, It.IsAny<Action<bool, INetworkConnection>>()))
                .Callback<string, int, Action<bool, INetworkConnection>>((a, b, c) => c(true, _connection.Object));

            _factory.Setup(m => m.BeginConnect("www.yahoo.com", 80, It.IsAny<Action<bool, INetworkConnection>>()))
                .Callback<string, int, Action<bool, INetworkConnection>>((a, b, c) =>
                                                                             {
                                                                                 secondConnectionOccurred = true;
                                                                                 c(true, _connection.Object);
                                                                             });

            dispatcher.ConnectToServer("www.cnn.com", 80, _responseFilter.Object, b =>
                                                                {
                                                                    // Connected to remote host. Other connection should still be blocking.
                                                                    if (b && !secondConnectionOccurred)
                                                                    {
                                                                        resetEvent.Set();
                                                                    }
                                                                });

            dispatcher.ConnectToServer("www.yahoo.com", 80, _responseFilter.Object, b =>
                                                                  {
                                                                      secondConnectionOccurred = true;
                                                                  });

            Assert.That(resetEvent.WaitOne(2000), "Never attempted to connect to remote host");
        }
コード例 #2
0
        public void WaitForIt()
        {
            ManualResetEvent resetEvent = new ManualResetEvent(false);

            _dispatcher = new ServerDispatcher(_factory.Object);

            _factory.Setup(m => m.BeginConnect("www.cnn.com", 80, It.IsAny<Action<bool, INetworkConnection>>()))
                .Callback<string, int, Action<bool, INetworkConnection>>((a, b, c) => c(true, _connection.Object));

            _dispatcher.ConnectToServer("www.cnn.com", 80, _responseFilter.Object, b =>
                                                                 {
                                                                     if (b)
                                                                     {
                                                                         resetEvent.Set();
                                                                     }
                                                                 });

            Assert.That(resetEvent.WaitOne(2000), "Never attempted to connect to remote host");
        }
コード例 #3
0
        public void SendingDataToOriginalServerWithMultipleConnections()
        {
            var buffer = new byte[] { 1, 2, 3 };

            ManualResetEvent resetEvent = new ManualResetEvent(false);

            int callbackCount = 0;
            Mock<INetworkConnection> fooServer = new Mock<INetworkConnection>();
            Mock<INetworkConnection> barServer = new Mock<INetworkConnection>();
            Mock<INetworkConnection> fooServer2 = new Mock<INetworkConnection>();

            ServerDispatcher dispatcher = new ServerDispatcher(_factory.Object);

            _factory.Setup(m => m.BeginConnect("www.foo.com", 80, It.IsAny<Action<bool, INetworkConnection>>()))
                .Callback<string, int, Action<bool, INetworkConnection>>((a, b, c) =>
                                                                             {
                                                                                 if (callbackCount == 0)
                                                                                     c(true, fooServer.Object);
                                                                                 else
                                                                                     c(true, fooServer2.Object);
                                                                             });
            _factory.Setup(m => m.BeginConnect("www.bar.com", 80, It.IsAny<Action<bool, INetworkConnection>>()))
                .Callback<string, int, Action<bool, INetworkConnection>>((a, b, c) => c(true, barServer.Object));

            dispatcher.ConnectToServer("www.foo.com", 80, _responseFilter.Object, b =>
                                                                {
                                                                    if (b)
                                                                    {
                                                                        callbackCount++;
                                                                    }
                                                                });

            dispatcher.ConnectToServer("www.bar.com", 80, _responseFilter.Object, b =>
                                                                {
                                                                    if (b)
                                                                    {
                                                                        callbackCount++;
                                                                    }
                                                                });

            dispatcher.ConnectToServer("www.foo.com", 80, _responseFilter.Object, b =>
                                                                {
                                                                    if (b)
                                                                    {
                                                                        callbackCount++;
                                                                        resetEvent.Set();
                                                                    }
                                                                });

            Assert.That(resetEvent.WaitOne(2000));

            Assert.That( dispatcher.TrySendDataToActiveServer(buffer) );

            fooServer.Verify(m => m.SendData(It.IsAny<byte[]>()), Times.Never());
            fooServer.Verify(m => m.Start(), Times.Once());

            barServer.Verify(m => m.SendData(It.IsAny<byte[]>()), Times.Never());
            barServer.Verify(m => m.Start(), Times.Once());

            fooServer2.Verify(m => m.SendData(buffer), Times.Once());
            fooServer2.Verify(m => m.Start(), Times.Once());
        }