public void Receive_ReplyToReceivedObject_ObjectIsSentBack() { //Arrange string replayPayload = null; Client client = new Client("10.0.0.1", 123); var networkChannel = MockNetworkChannelFactory.GetMockedNetworkChannel() .OnSendHandle((Client, payload) => replayPayload = payload); //Act ObjectTransport transport = TestObjectTransportFactory.CreateNewObjectTransport(networkChannel); transport.Receive <MockObjectMessage>() .Reply(o => { MockObjectMessage sendBack = new MockObjectMessage(); sendBack.Property1_string = "Reply message"; sendBack.Property2_int = 12; sendBack.Property3_decimal = 1.33M; return(sendBack); }) .Execute(); networkChannel.SimulateClientConnect(client); networkChannel.SimulateClientResponse(client, "OTransport.tests.MockObjectMessage, ObjectTransport.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null::{\"Property1_string\":\"Test String\",\"Property2_int\":12,\"Property3_decimal\":1.33}" ); //Assert Assert.AreEqual("OTransport.tests.MockObjectMessage, ObjectTransport.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null::{\"Property1_string\":\"Reply message\",\"Property2_int\":12,\"Property3_decimal\":1.33}", replayPayload); }
public void Receive_ObjectType_ObjectReceiveFunctionExecuted() { //Arrange Client client = new Client("10.0.0.1", 123); var networkChannel = MockNetworkChannelFactory.GetMockedNetworkChannel(); MockObjectMessage receive = new MockObjectMessage(); //Act ObjectTransport transport = TestObjectTransportFactory.CreateNewObjectTransport(networkChannel); transport.Receive <MockObjectMessage>(o => { receive = o; } ) .Execute(); networkChannel.SimulateClientConnect(client); networkChannel.SimulateClientResponse(client, "OTransport.tests.MockObjectMessage, ObjectTransport.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null::{\"Property1_string\":\"Test String\",\"Property2_int\":12,\"Property3_decimal\":1.33}" ); //Assert Assert.AreEqual("Test String", receive.Property1_string); Assert.AreEqual(12, receive.Property2_int); Assert.AreEqual(1.33M, receive.Property3_decimal); }
public void TCPClient_SendUnreliably_ExceptionThrown() { //Arrange Client client = null; Client clientDisconnect = null; ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(Server); serverObjectTransport.Start("127.0.0.1", 0); ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(Client1); clientObjectTransport.OnClientDisconnect(c => clientDisconnect = c); clientObjectTransport.Start("127.0.0.1", Server.LocalPort); Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 1); client = clientObjectTransport.GetConnectedClients().First(); Utilities.WaitFor(ref client); var message = new MockObjectMessage(); //Act clientObjectTransport.Send(message) .Unreliable() .Execute(); }
public void TCPClient_ObjectTransportStartCalled_ServerIsAddedAsClient() { //Arrange Client client = null; server.Start("127.0.0.1", 0); ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(server); ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransportTCPclient(); clientObjectTransport.Start("127.0.0.1", server.LocalPort); //When the start method is called, there should be clients client = clientObjectTransport.GetConnectedClients().First(); Utilities.WaitFor(ref client); Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 1); //Act //Assert Assert.AreEqual(client.IPAddress, "127.0.0.1"); Assert.AreEqual(client.Port, server.LocalPort); }
public void SendWithResponse_ObjectWithPropertiesSet_PayloadWithObjectAndTokenSent() { //Arrange var sentPayload = string.Empty; var client = new Client("10.0.0.1", 123); var networkChannel = MockNetworkChannelFactory.GetMockedNetworkChannel() .OnSendHandle((Client, payload) => sentPayload = payload); MockObjectMessage sendObject = new MockObjectMessage(); sendObject.Property1_string = "Test String"; sendObject.Property2_int = 12; sendObject.Property3_decimal = 1.33M; ObjectTransport transport = TestObjectTransportFactory.CreateNewObjectTransport(networkChannel); networkChannel.SimulateClientConnect(client); //Act transport.Send(sendObject) .Response <MockObjectMessage>(o => o.GetType()) .Execute(); Utilities.WaitFor(ref sentPayload); //Assert Regex rgx = new Regex(typeof(MockObjectMessage).AssemblyQualifiedName + "::(.*)::{\"Property1_string\":\"Test String\",\"Property2_int\":12,\"Property3_decimal\":1.33}"); Assert.IsTrue(rgx.IsMatch(sentPayload)); }
public void SendReceiveThroughChannel_ObjectTransportConnectedChannel_ObjectSentAndReceivedBetweenClients() { //Arrange var joinedNetworkChannels = MockNetworkChannelFactory.GetConnectedChannels(); MockObjectMessage sendMessage = new MockObjectMessage(); sendMessage.Property1_string = "test send"; sendMessage.Property2_int = 123; sendMessage.Property3_decimal = 56M; MockObjectMessage receivedMessage = null; //Act ObjectTransport client1 = TestObjectTransportFactory.CreateNewObjectTransport(joinedNetworkChannels.Item1); client1.Receive <MockObjectMessage>(o => { receivedMessage = o; }) .Execute(); ObjectTransport client2 = TestObjectTransportFactory.CreateNewObjectTransport(joinedNetworkChannels.Item2); client2.Send(sendMessage) .Execute(); Utilities.WaitFor(ref receivedMessage); //Assert Assert.AreEqual(sendMessage.Property3_decimal, receivedMessage.Property3_decimal); Assert.AreEqual(sendMessage.Property1_string, receivedMessage.Property1_string); Assert.AreEqual(sendMessage.Property2_int, receivedMessage.Property2_int); }
public void UDPClient_ClientDisconnectsServer_ServerOnClientDisconnectCalled() { //Arrange Client disconnectedClient = null; Client connectedServer = null; udpServer = new UDPServerChannel("127.0.0.1", 0, 32); ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpServer); serverObjectTransport.OnClientDisconnect(c => disconnectedClient = c); udpClient = new UDPClientChannel("127.0.0.1", udpServer.LocalPort); ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpClient); clientObjectTransport.OnClientConnect(c => connectedServer = c); udpClient2 = new UDPClientChannel("127.0.0.1", udpServer.LocalPort); ObjectTransport clientObjectTransport2 = TestObjectTransportFactory.CreateNewObjectTransport(udpClient2); 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, udpClient.LocalPort); }
public void SendExecute_ObjectWithProperties_PayloadWithObjectAndTypeSent() { //Arrange var sentPayload = string.Empty; var client = new Client("10.0.0.1", 123); var networkChannel = MockNetworkChannelFactory.GetMockedNetworkChannel() .OnSendHandle((Client, payload) => sentPayload = payload); MockObjectMessage sendObject = new MockObjectMessage(); sendObject.Property1_string = "Test String"; sendObject.Property2_int = 12; sendObject.Property3_decimal = 1.33M; //Act ObjectTransport transport = TestObjectTransportFactory.CreateNewObjectTransport(networkChannel); networkChannel.SimulateClientConnect(client); transport.Send(sendObject) .To(client) .Execute(); //Assert Assert.AreEqual(typeof(MockObjectMessage).AssemblyQualifiedName + "::{\"Property1_string\":\"Test String\",\"Property2_int\":12,\"Property3_decimal\":1.33}", sentPayload); }
public void ClientDisconnectsServer_ServerOnClientDisconnectCalled() { //Arrange //Create a second client ObjectTransport clientObjectTransport2 = TestObjectTransportFactory.CreateNewObjectTransport(Client2); clientObjectTransport2.Start("127.0.0.1", Server.LocalPort); //Wait for the connection to have been made Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 2); //Act //disconnect the server from the client Client1ObjectTransport.DisconnectClient(); Utilities.WaitFor(() => ServerOnDisconnectClients.Count == 1); //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(ServerOnDisconnectClients.First().Port, Client1.LocalPort); }
public void UDPServerWith2Clients_Disconnect1Client_1ClientDisconnected() { //Arrange Client disconnectedClient = null; udpServer = new UDPServerChannel("127.0.0.1", 0, 32); ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpServer); serverObjectTransport.OnClientDisconnect(c => disconnectedClient = c); udpClient = new UDPClientChannel("127.0.0.1", udpServer.Port); ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpClient); udpClient2 = new UDPClientChannel("127.0.0.1", udpServer.Port); ObjectTransport clientObjectTransport2 = TestObjectTransportFactory.CreateNewObjectTransport(udpClient2); 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); }
public void UDPServerWith2Clients_Disconnect2Client_AllClientsDisconnected() { //Arrange List <Client> disconnectedClients = new List <Client>(); udpServer = new UDPServerChannel("127.0.0.1", 0, 32); ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpServer); serverObjectTransport.OnClientDisconnect(c => disconnectedClients.Add(c)); udpClient = new UDPClientChannel("127.0.0.1", udpServer.Port); ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpClient); udpClient2 = new UDPClientChannel("127.0.0.1", udpServer.Port); ObjectTransport clientObjectTransport2 = TestObjectTransportFactory.CreateNewObjectTransport(udpClient2); 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 SendToAll_Except2Client_AllClientsExcept2AreSendTo() { //Arrange var client1 = new Client("10.0.0.1", 123); var client2 = new Client("10.0.0.2", 123); var client3 = new Client("10.0.0.3", 123); var clientsSendTo = new List <Client>(); var networkChannel = MockNetworkChannelFactory.GetMockedNetworkChannel() .OnSendHandle((Client, paylaod) => clientsSendTo.Add(Client)); MockObjectMessage sendObject = new MockObjectMessage(); sendObject.Property1_string = "Test String"; sendObject.Property2_int = 12; sendObject.Property3_decimal = 1.33M; ObjectTransport transport = TestObjectTransportFactory.CreateNewObjectTransport(networkChannel); networkChannel.SimulateClientConnect(client1); networkChannel.SimulateClientConnect(client2); networkChannel.SimulateClientConnect(client3); //Act transport.Send(sendObject) .Response <MockObjectMessage>(o => o.GetType()) .ToAllExcept(client1, client3) .Execute(); //Assert Assert.AreEqual(1, clientsSendTo.Count); Assert.AreEqual(clientsSendTo[0].IPAddress, "10.0.0.2"); }
public void TCPServer_ClientDisconnects_CallbackCalled() { //Arrange Client clientConnect = null; Client clientDisconnect = null; tcpserver = new TCPServerChannel("127.0.0.1", 0); ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(tcpserver); serverObjectTransport.OnClientConnect(c => clientConnect = c); serverObjectTransport.OnClientDisconnect(c => clientDisconnect = c); tcpclient = new TCPClientChannel("127.0.0.1", tcpserver.LocalPort); ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(tcpclient); Utilities.WaitFor(ref clientConnect); Utilities.WaitFor(() => clientObjectTransport.GetConnectedClients().Count() == 1); //Act clientObjectTransport.Stop(); Utilities.WaitFor(ref clientDisconnect); Utilities.Wait(); //Assert Assert.AreEqual(clientConnect.IPAddress, "127.0.0.1"); Assert.AreEqual(clientDisconnect.IPAddress, "127.0.0.1"); Assert.AreEqual(clientDisconnect, clientConnect); Assert.AreEqual(0, clientObjectTransport.GetConnectedClients().Count()); Assert.AreEqual(0, serverObjectTransport.GetConnectedClients().Count()); }
public void Receive_InvalidPayload_PayloadNotProcessed() { //Arrange Client client = new Client("10.0.0.1", 123); var networkChannel = MockNetworkChannelFactory.GetMockedNetworkChannel(); MockObjectMessage receive = null; //Act ObjectTransport transport = TestObjectTransportFactory.CreateNewObjectTransport(networkChannel); transport.Receive <MockObjectMessage>(o => { receive = o; } ) .Execute(); networkChannel.SimulateClientConnect(client); networkChannel.SimulateClientResponse(client, "{\"Type\":\"OTransport.tests.MockObjectMessage, ObjectTransport.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\",:{\"Property1_string\":\"Test String\",\"Property2_int\":12,\"Property3_decimal\":1.33}}" ); //Assert Assert.IsNull(receive); }
public void UDPClient_ServerDisconnects_ClientDisconnectCallbackCalled() { //Arrange Client client = null; Client clientDisconnect = null; udpServer = new UDPServerChannel("127.0.0.1", 0, 32); ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpServer); udpClient = new UDPClientChannel("127.0.0.1", udpServer.LocalPort); ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpClient); 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 OnFailedReceived_InvalidPayloadReceived_OnFailHandleExecuted() { //Arrange Client client = new Client("10.0.0.1", 123); ReceivedMessage receivedMessage = null; Exception failedException = null; var networkChannel = MockNetworkChannelFactory.GetMockedNetworkChannel(); MockObjectMessage receive = new MockObjectMessage(); //Act ObjectTransport transport = TestObjectTransportFactory.CreateNewObjectTransport(networkChannel); transport.Receive <MockObjectMessage>(o => { receive = o; } ) .Execute(); transport.OnFailedReceive((r, e) => { receivedMessage = r; failedException = e; }); networkChannel.SimulateClientConnect(client); networkChannel.SimulateClientResponse(client, "OTransport.tests.NOTHING, ObjectTransport.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullProperty1_string\":\"Test String\",\"Property2_int\":12,\"Property3_decimal\":1.33}"); Assert.IsNotNull(receivedMessage); Assert.IsNotNull(failedException); }
public void Receive_ObjectWithBinaryProperty_BinaryReceived() { //Arrange var joinedNetworkChannels = MockNetworkChannelFactory.GetConnectedChannels(); MockObjectMessageWithBinary sendMessage = new MockObjectMessageWithBinary(); sendMessage.Property2_String = "Message with binary"; sendMessage.Property1_Bytes = Encoding.ASCII.GetBytes("hello world"); MockObjectMessageWithBinary receivedMessage = null; Client receivedClient = null; //Act ObjectTransport client1 = TestObjectTransportFactory.CreateNewObjectTransport(joinedNetworkChannels.Item1); client1.Receive <MockObjectMessageWithBinary>((c, o) => { receivedClient = c; receivedMessage = o; }) .Execute(); ObjectTransport client2 = TestObjectTransportFactory.CreateNewObjectTransport(joinedNetworkChannels.Item2); client2.Send(sendMessage) .Execute(); //Assert Assert.AreEqual("hello world", System.Text.Encoding.UTF8.GetString(receivedMessage.Property1_Bytes)); Assert.AreEqual(receivedMessage.Property2_String, "Message with binary"); Assert.AreEqual("10.0.0.2", receivedClient.IPAddress); }
protected void SetUpNetworkChannels(INetworkChannel client, INetworkChannel client2, INetworkChannel server) { Client1 = client; Client2 = client2; Server = server; ServerOnConnectClients = new List <Client>(); ServerOnDisconnectClients = new List <Client>(); Client1OnConnectClients = new List <Client>(); Client1OnDisconnectClients = new List <Client>(); serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(Server); serverObjectTransport.OnClientConnect(c => ServerOnConnectClients.Add(c)); serverObjectTransport.OnClientDisconnect(c => ServerOnDisconnectClients.Add(c)); serverObjectTransport.Start("127.0.0.1", 0); Utilities.WaitFor(() => server.LocalPort != 0); Client1ObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(Client1); Client1ObjectTransport.OnClientConnect(c => Client1OnConnectClients.Add(c)); Client1ObjectTransport.OnClientDisconnect(c => Client1OnDisconnectClients.Add(c)); Client1ObjectTransport.Start("127.0.0.1", Server.LocalPort); //Wait for the connection to have been made Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 1); Utilities.WaitFor(() => Client1ObjectTransport.GetConnectedClients().Count() == 1); }
public void Receve_RegisterSameTypeTwice_ObjectTransportExceptionThrown() { //Arrange var networkChannel = MockNetworkChannelFactory.GetMockedNetworkChannel(); //Act ObjectTransport transport = TestObjectTransportFactory.CreateNewObjectTransport(networkChannel); transport.Receive <MockObjectMessage>().Execute(); transport.Receive <MockObjectMessage>().Execute(); }
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 static Tuple <ObjectTransport, ObjectTransport> GetConnectObjectTransports() { server = new UDPServerChannel("127.0.0.1", 0, 32); ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(server); udpclient = new UDPClientChannel("127.0.0.1", server.Port); ObjectTransport client = TestObjectTransportFactory.CreateNewObjectTransport(udpclient); Tuple <ObjectTransport, ObjectTransport> result = new Tuple <ObjectTransport, ObjectTransport>(serverObjectTransport, client); return(result); }
public void SendReceiveReplyThroughChannel_TwoObjectTransportsConnected_SameTokenUsed() { //Arrange var client2ReceiveFunctionCalled = false; var client2RespondFunctionCalled = false; var joinedNetworkChannels = MockNetworkChannelFactory.GetConnectedChannels(); MockObjectMessage sendMessage = new MockObjectMessage(); sendMessage.Property1_string = "test send"; sendMessage.Property2_int = 123; sendMessage.Property3_decimal = 56M; MockObjectMessage receivedMessage = null; //Act ObjectTransport client1 = TestObjectTransportFactory.CreateNewObjectTransport(joinedNetworkChannels.Item1); client1.Receive <MockObjectMessage>() .Reply((c, o) => { o.Property1_string = "replied"; return(o); }) .Execute(); ObjectTransport client2 = TestObjectTransportFactory.CreateNewObjectTransport(joinedNetworkChannels.Item2); client2.Send(sendMessage) .Response <MockObjectMessage>(o => { client2RespondFunctionCalled = true; receivedMessage = o; }) .Execute(); client2.Receive <MockObjectMessage>(o => { client2ReceiveFunctionCalled = true; receivedMessage = o; }).Execute(); Utilities.WaitFor(ref receivedMessage); //Assert Assert.IsFalse(client2ReceiveFunctionCalled); Assert.IsTrue(client2RespondFunctionCalled); }
public void TimeOut_MessageSentToMultipleClientsWithSomeResponding_TimeOutFunctionReturnsClientsWhoDidNotRespond() { //Arrange var client1 = new Client("10.0.0.1", 123); var client2 = new Client("10.0.0.2", 123); var client3 = new Client("10.0.0.3", 123); Client[] clientsWhoDidNotRespond = null; MockObjectMessage messageThatTimedOut = null; var sentPayload = string.Empty; var networkChannel = MockNetworkChannelFactory.GetMockedNetworkChannel() .OnSendHandle((Client, payload) => sentPayload = payload); MockObjectMessage sendObject = new MockObjectMessage(); sendObject.Property1_string = "Test String"; sendObject.Property2_int = 12; sendObject.Property3_decimal = 1.33M; ObjectTransport transport = TestObjectTransportFactory.CreateNewObjectTransport(networkChannel); networkChannel.SimulateClientConnect(client1); networkChannel.SimulateClientConnect(client2); networkChannel.SimulateClientConnect(client3); //Act transport.Send(sendObject) .Response <MockObjectMessage>(o => { }) .SetTimeOut(2) .OnTimeOut( (c, o) => { clientsWhoDidNotRespond = c; messageThatTimedOut = o; }) .ToAll() .Execute(); //Echo back the message networkChannel.SimulateClientResponse(client2, sentPayload); Utilities.WaitFor(ref messageThatTimedOut); //Assert Assert.AreEqual(2, clientsWhoDidNotRespond.Length); Assert.IsTrue(clientsWhoDidNotRespond.Contains(client1)); Assert.IsTrue(clientsWhoDidNotRespond.Contains(client3)); Assert.AreEqual(sendObject, messageThatTimedOut); }
public void OnClientConnect_ClientConnectsToObject_ClientInjectedIntoAction() { //Arrange var sentJson = string.Empty; var client = new Client("10.0.0.1", 123); var networkChannel = MockNetworkChannelFactory.GetMockedNetworkChannel(); Client connectedClient = null; //Act ObjectTransport transport = TestObjectTransportFactory.CreateNewObjectTransport(networkChannel); transport.OnClientConnect(c => { connectedClient = c; }); networkChannel.SimulateClientConnect(client); //Assert Assert.AreEqual(client, connectedClient); }
public void ReceiveReply_WithClientAction_ObjectAndClientInjectedInToAction() { //Arrange var joinedNetworkChannels = MockNetworkChannelFactory.GetConnectedChannels(); MockObjectMessage sendMessage = new MockObjectMessage(); sendMessage.Property1_string = "test send"; sendMessage.Property2_int = 123; sendMessage.Property3_decimal = 56M; MockObjectMessage receivedMessage = null; Client receivedClient = null; //Act ObjectTransport client1 = TestObjectTransportFactory.CreateNewObjectTransport(joinedNetworkChannels.Item1); client1.Receive <MockObjectMessage>() .Reply((c, o) => { receivedMessage = o; receivedClient = c; return(null); }) .Execute(); ObjectTransport client2 = TestObjectTransportFactory.CreateNewObjectTransport(joinedNetworkChannels.Item2); client2.Send(sendMessage) .Execute(); Utilities.WaitFor(ref receivedMessage); //Assert Assert.AreEqual(sendMessage.Property3_decimal, receivedMessage.Property3_decimal); Assert.AreEqual(sendMessage.Property1_string, receivedMessage.Property1_string); Assert.AreEqual(sendMessage.Property2_int, receivedMessage.Property2_int); Assert.AreEqual("10.0.0.2", receivedClient.IPAddress); }
public void UDPServer_SendObject_CorrectObjectSent() { //Arrange MockObjectMessage receivedObject = null; udpServer = new UDPServerChannel("127.0.0.1", 0, 32); Client client = null; ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpServer); serverObjectTransport.OnClientConnect(c => client = c); udpClient = new UDPClientChannel("127.0.0.1", udpServer.Port); ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpClient); Utilities.WaitFor(ref client); //Act clientObjectTransport.Receive <MockObjectMessage>(o => receivedObject = o ).Execute(); serverObjectTransport.Send(new MockObjectMessage() { Property1_string = "hello world!", Property2_int = 123, Property3_decimal = 12.3M }) .To(client) .Execute(); Utilities.WaitFor(ref receivedObject); //Assert Assert.AreEqual("hello world!", receivedObject.Property1_string); Assert.AreEqual(123, receivedObject.Property2_int); Assert.AreEqual(12.3M, receivedObject.Property3_decimal); }
public void TimeOut_TimeOutFunctionSet_TimeOutFunctionCalled() { //Arrange var client = new Client("10.0.0.1", 123); Client[] clientsWhoDidNotRespond = null; MockObjectMessage messageThatTimedOut = null; var sentPayload = string.Empty; var networkChannel = MockNetworkChannelFactory.GetMockedNetworkChannel() .OnSendHandle((Client, payload) => sentPayload = payload); MockObjectMessage sendObject = new MockObjectMessage(); sendObject.Property1_string = "Test String"; sendObject.Property2_int = 12; sendObject.Property3_decimal = 1.33M; //Act ObjectTransport transport = TestObjectTransportFactory.CreateNewObjectTransport(networkChannel); networkChannel.SimulateClientConnect(client); transport.Send(sendObject) .Response <MockObjectMessage>(o => { }) .SetTimeOut(1) .OnTimeOut( (c, o) => { clientsWhoDidNotRespond = c; messageThatTimedOut = o; }) .Execute(); Utilities.WaitFor(ref clientsWhoDidNotRespond); //Assert Assert.AreEqual(1, clientsWhoDidNotRespond.Length); Assert.AreEqual(sendObject, messageThatTimedOut); }
public void TCPNetwork_SendAndReplyMessage_ResponseIsCalled() { //Arrange Client client = null; Client clientDisconnect = null; server = new TCPServerChannel("127.0.0.1", 0); ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(server); tcpclient = new TCPClientChannel("127.0.0.1", server.Port); 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 ServerWith2Clients_ServerDisconnects1Client_1ClientDisconnected() { //Arrange ObjectTransport clientObjectTransport2 = TestObjectTransportFactory.CreateNewObjectTransport(Client2); clientObjectTransport2.Start("127.0.0.1", Server.LocalPort); //Wait for the connection to have been made Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 2); var FirstClient = serverObjectTransport.GetConnectedClients().First(); //Act serverObjectTransport.DisconnectClient(FirstClient); Utilities.WaitFor(() => ServerOnDisconnectClients.Count() == 1); //Assert Client LastClient = serverObjectTransport.GetConnectedClients().First(); Assert.AreEqual(1, serverObjectTransport.GetConnectedClients().Count()); Assert.AreNotEqual(FirstClient.Port, LastClient.Port); }
public void Server_ServerDisconnects2Client_AllClientsDisconnected() { //Arrange //Create a second client ObjectTransport clientObjectTransport2 = TestObjectTransportFactory.CreateNewObjectTransport(Client2); clientObjectTransport2.Start("127.0.0.1", Server.LocalPort); //Wait for the connection to have been made Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 2); //Act var allClients = serverObjectTransport.GetConnectedClients().ToArray(); serverObjectTransport.DisconnectClient(allClients); Utilities.WaitFor(() => ServerOnDisconnectClients.Count == 2); //Assert Assert.AreEqual(0, serverObjectTransport.GetConnectedClients().Count()); Assert.AreEqual(2, ServerOnDisconnectClients.Count()); }