Example #1
0
        public void TCPClient_ClientDisconnects_CallbackCalled()
        {
            //Arrange
            Client client           = null;
            Client clientDisconnect = null;

            server.Start("127.0.0.1", 0);
            ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(server);

            tcpclient.Start("127.0.0.1", server.LocalPort);
            ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(tcpclient);

            clientObjectTransport.OnClientDisconnect(c => clientDisconnect = c);
            client = clientObjectTransport.GetConnectedClients().First();

            Utilities.WaitFor(ref client);
            Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 1);

            //Act
            serverObjectTransport.Stop();

            Utilities.WaitFor(ref clientDisconnect);

            //Assert
            Assert.AreEqual(client.IPAddress, "127.0.0.1");
            Assert.AreEqual(clientDisconnect.IPAddress, "127.0.0.1");
            Assert.AreEqual(clientDisconnect, client);
            Utilities.WaitFor(() => clientObjectTransport.GetConnectedClients().Count() == 0);
            Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 0);
        }
        public void TCPServer_WhenClientConnects_CallbackFunctionCalled()
        {
            bool connected = false;

            tcpserver.Start("127.0.0.1", 0);
            tcpserver.OnClientConnect(c => connected = true);

            tcpclient.Start("127.0.0.1", tcpserver.LocalPort);

            Utilities.WaitFor(ref connected);
            Assert.IsTrue(connected);
        }
        public void TCPServerWith2Clients_Disconnect2Client_AllClientsDisconnected()
        {
            //Arrange
            List <Client> disconnectedClients = new List <Client>();

            tcpserver.Start("127.0.0.1", 0);
            ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(tcpserver);

            serverObjectTransport.OnClientDisconnect(c => disconnectedClients.Add(c));

            tcpclient.Start("127.0.0.1", tcpserver.LocalPort);
            ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(tcpclient);

            tcpclient2 = new TCPClientChannel();
            tcpclient2.Start("127.0.0.1", tcpserver.LocalPort);
            ObjectTransport clientObjectTransport2 = TestObjectTransportFactory.CreateNewObjectTransport(tcpclient2);

            Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 2);

            //Act

            var allClients = serverObjectTransport.GetConnectedClients().ToArray();

            serverObjectTransport.DisconnectClient(allClients);

            Utilities.WaitFor(() => disconnectedClients.Count == 2);

            //Assert
            Assert.AreEqual(0, serverObjectTransport.GetConnectedClients().Count());
            Assert.AreEqual(2, disconnectedClients.Count());
        }
        public void TCPServerWith2Clients_Disconnect1Client_1ClientDisconnected()
        {
            //Arrange
            Client disconnectedClient = null;

            tcpserver.Start("127.0.0.1", 0);
            ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(tcpserver);

            serverObjectTransport.OnClientDisconnect(c => disconnectedClient = c);

            tcpclient.Start("127.0.0.1", tcpserver.LocalPort);
            ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(tcpclient);

            tcpclient2 = new TCPClientChannel();
            tcpclient2.Start("127.0.0.1", tcpserver.LocalPort);
            ObjectTransport clientObjectTransport2 = TestObjectTransportFactory.CreateNewObjectTransport(tcpclient2);

            Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 2);

            //Act

            var FirstClient = serverObjectTransport.GetConnectedClients().First();

            serverObjectTransport.DisconnectClient(FirstClient);

            Utilities.WaitFor(ref disconnectedClient);

            //Assert
            Client LastClient = serverObjectTransport.GetConnectedClients().First();

            Assert.AreEqual(1, serverObjectTransport.GetConnectedClients().Count());
            Assert.AreNotEqual(FirstClient.Port, LastClient.Port);
        }
Example #5
0
        public static Tuple <ObjectTransport, ObjectTransport> GetConnectObjectTransports()
        {
            server.Start("127.0.0.1", 0);

            ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(server);

            tcpclient.Start("127.0.0.1", server.LocalPort);
            ObjectTransport client = TestObjectTransportFactory.CreateNewObjectTransport(tcpclient);

            Tuple <ObjectTransport, ObjectTransport> result = new Tuple <ObjectTransport, ObjectTransport>(serverObjectTransport, client);

            return(result);
        }
        public void TCPNetwork_SendAndReplyMessage_ResponseIsCalled()
        {
            //Arrange
            Client client           = null;
            Client clientDisconnect = null;

            tcpserver.Start("127.0.0.1", 0);
            ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(tcpserver);

            tcpclient.Start("127.0.0.1", tcpserver.LocalPort);
            ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(tcpclient);

            clientObjectTransport.OnClientDisconnect(c => clientDisconnect = c);
            client = clientObjectTransport.GetConnectedClients().First();

            Utilities.WaitFor(ref client);
            Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 1);

            //Act
            serverObjectTransport.Receive <MockObjectMessage>()
            .Reply((o) => { return(o); })
            .Execute();


            var mockObject = new MockObjectMessage()
            {
                Property1_string = "Mock Object"
            };
            MockObjectMessage responseObject = null;

            clientObjectTransport.Send(mockObject)
            .Response <MockObjectMessage>((r) => { responseObject = r; })
            .Execute();

            Utilities.WaitFor(ref responseObject);

            //Assert
            Assert.AreEqual(responseObject.Property1_string, "Mock Object");
        }
        public void TCPClient_ClientDisconnectsServer_ServerOnClientDisconnectCalled()
        {
            //Arrange
            Client disconnectedClient = null;
            Client connectedServer    = null;

            tcpserver.Start("127.0.0.1", 0);
            ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(tcpserver);

            serverObjectTransport.OnClientDisconnect(c => disconnectedClient = c);

            tcpclient.Start("127.0.0.1", tcpserver.LocalPort);
            ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(tcpclient);

            clientObjectTransport.OnClientConnect(c => connectedServer = c);

            tcpclient2 = new TCPClientChannel();
            tcpclient2.Start("127.0.0.1", tcpserver.LocalPort);
            ObjectTransport clientObjectTransport2 = TestObjectTransportFactory.CreateNewObjectTransport(tcpclient2);

            Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 2);

            //Act

            //disconnect the server from the client
            clientObjectTransport.DisconnectClient();

            Utilities.WaitFor(ref disconnectedClient);

            //Assert
            //Ensure that the client record was disconnect from the server
            Assert.AreEqual(1, serverObjectTransport.GetConnectedClients().Count());

            //Esnure that the client who disconnected from the server was the one that we called disconect
            Assert.AreEqual(disconnectedClient.Port, tcpclient.LocalPort);
        }