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_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 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 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 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 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 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 Receve_RegisterSameTypeTwice_ObjectTransportExceptionThrown() { //Arrange var networkChannel = MockNetworkChannelFactory.GetMockedNetworkChannel(); //Act ObjectTransport transport = TestObjectTransportFactory.CreateNewObjectTransport(networkChannel); transport.Receive <MockObjectMessage>().Execute(); transport.Receive <MockObjectMessage>().Execute(); }
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 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); }