public void ConnectionEstablished_SendDataFromClient_ShoudBeReceivedOnServer() { var helloWorld = "Hello World!"; var receivedMessagesInSession = new List <string>(); var networkLayer = new CombinedTestNetworkLayer(); var client = new TestPollingClient(networkLayer); var server = new TestPollingServer(networkLayer); server.Start(); var session = ConnectionHelper.WaitForSessionOnServer(server, client); session.FrameReceived += (sender, args) => receivedMessagesInSession.Add(Encoding.UTF8.GetString(args.Frame.Payload)); client.Send(new ClientDataFrame() { Payload = Encoding.UTF8.GetBytes(helloWorld) }); Assert.IsTrue(receivedMessagesInSession.Any()); Assert.AreEqual(helloWorld, receivedMessagesInSession[0]); server.Stop(); client.DisconnectAsync().Wait(); }
public void StartedServer_ReceivesEmptyClientId_ShouldUnblockAcceptAndReturnSession() { var networkLayer = new ServerTestNetworkLinkLayer(); var server = new TestPollingServer(networkLayer); PollingClientSession <ClientDataFrame, ServerDataFrame> session = null; server.Start(); session = ConnectionHelper.WaitForClientSession(server, networkLayer, this.initConnectionFrame); Assert.IsNotNull(session, "There was no session captured"); }
public void StartedServer_ReceivesEmptyClientId_ShouldResponseWithAClientId() { var networkLayer = new ServerTestNetworkLinkLayer(); var server = new TestPollingServer(networkLayer); server.Start(); ConnectionHelper.WaitForClientSession(server, networkLayer, this.initConnectionFrame); networkLayer.Receive(new BinaryClientFrameEncoder().Encode(initConnectionFrame)); Assert.IsTrue(networkLayer.SentBytes.Any(), "There should be at least one captured frame"); var sentFrameBytes = networkLayer.SentBytes[0]; var sentFrame = new GenericSerializer <ServerDataFrame>().Deserialze(sentFrameBytes); Assert.IsNotNull(sentFrame); Assert.IsTrue(sentFrame.SequenceId != 0); Assert.IsNotNull(sentFrame.Payload); Assert.AreNotEqual(0, BitConverter.ToInt32(sentFrame.Payload, 0)); }
public void EstablishedSession_ReceivesData_ShouldTriggerEventOnSession() { var networkLayer = new ServerTestNetworkLinkLayer(); var server = new TestPollingServer(networkLayer); var serverOnFrameReceived = new List <ClientDataFrame>(); server.Start(); var session = ConnectionHelper.WaitForClientSession(server, networkLayer, this.initConnectionFrame); session.FrameReceived += (sender, args) => serverOnFrameReceived.Add(args.Frame); // Find out the ClientId Assert.IsTrue(networkLayer.SentBytes.Any(), "There should be at least one captured frame"); var sentFrameBytes = networkLayer.SentBytes[0]; var sentFrame = new GenericSerializer <ServerDataFrame>().Deserialze(sentFrameBytes); Assert.IsNotNull(sentFrame); Assert.IsTrue(sentFrame.SequenceId != 0); Assert.IsNotNull(sentFrame.Payload); Assert.AreNotEqual(0, BitConverter.ToInt32(sentFrame.Payload, 0)); var clientId = BitConverter.ToInt32(sentFrame.Payload, 0); var clientFrame = new ClientDataFrame() { ClientId = clientId, SequenceId = initConnectionFrame.SequenceId + 1, Payload = Encoding.UTF8.GetBytes("Hello World") }; networkLayer.Receive(new GenericSerializer <ClientDataFrame>().Serialize(clientFrame)); Assert.AreEqual(1, serverOnFrameReceived.Count); Assert.AreEqual("Hello World", Encoding.UTF8.GetString(serverOnFrameReceived[0].Payload)); }
public void ConnectionEstablished_SendDataFromServer_ShoudBeReceivedOnClient() { var helloWorld = "Hello World!"; var receivedMessagesOnClient = new List <string>(); var atLeastOneFrameReceivedOnClient = new AutoResetEvent(false); var networkLayer = new CombinedTestNetworkLayer(); var client = new TestPollingClient(networkLayer); var server = new TestPollingServer(networkLayer); server.Start(); var session = ConnectionHelper.WaitForSessionOnServer(server, client); session = ConnectionHelper.WaitForConnectionEstablishmentOnServer(session); client.FrameReceived += (sender, args) => { receivedMessagesOnClient.Add(Encoding.UTF8.GetString(args.Frame.Payload)); atLeastOneFrameReceivedOnClient.Set(); }; session.Send(new ServerDataFrame() { Payload = Encoding.UTF8.GetBytes(helloWorld) }); atLeastOneFrameReceivedOnClient.WaitOne(5000); Assert.IsTrue(receivedMessagesOnClient.Any()); Assert.AreEqual(helloWorld, receivedMessagesOnClient[0]); server.Stop(); client.DisconnectAsync().Wait(); }