Exemplo n.º 1
0
        public void NewServer_TryToAcceptSessionButDidNotStartYet_ShouldThrowAnException()
        {
            var networkLayer = new ServerTestNetworkLinkLayer();
            var server       = new TestPollingServer(networkLayer);

            server.Accept();
        }
Exemplo n.º 2
0
        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();
        }
Exemplo n.º 3
0
        public void NewServer_ReceivesConnectionRequest_NothingHappens()
        {
            var networkLayer = new ServerTestNetworkLinkLayer();
            var server       = new TestPollingServer(networkLayer);

            networkLayer.Receive(new BinaryClientFrameEncoder().Encode(this.initConnectionFrame));
            Assert.AreEqual(0, server.SessionCount);
        }
Exemplo n.º 4
0
        public void InitializeServer_WithEmptyServerEncoder_ShouldThrowAnException()
        {
            var specification = new TestProtocolSpecification()
            {
                ClientEncoder = new BinaryClientFrameEncoder(),
                ServerEncoder = null
            };

            var server = new TestPollingServer(new ServerTestNetworkLinkLayer(), specification);
        }
Exemplo n.º 5
0
        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");
        }
Exemplo n.º 6
0
        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));
        }
Exemplo n.º 7
0
        public void EstablishedSession_ReceivesNoData_ShouldCloseReceiveTimeoutReason()
        {
            var sessionCloseReason = CloseReason.Unknown;

            var waitEvent = new ManualResetEvent(false);

            var networkLayer = new CombinedTestNetworkLayer();

            var client = new TestPollingClient(networkLayer);
            var server = new TestPollingServer(networkLayer);

            var session = ConnectionHelper.WaitForSessionOnServer(server, client);

            session.SessionClosed += (sender, args) =>
            {
                sessionCloseReason = args.Reason;
                waitEvent.Set();
            };

            ConnectionHelper.WaitForConnectionEstablishmentOnServer(session);

            Assert.AreEqual(SessionState.Connected, session.SessionState);

            var disconnectTask = client.DisconnectAsync();

            disconnectTask.Wait(5000);

            Assert.AreEqual(TaskStatus.RanToCompletion, disconnectTask.Status, "The disconnectAsync should be done withing 5s");
            Assert.AreEqual(0, client.CurrentPollingPoolSize);

            var eventHasBeenRaised = waitEvent.WaitOne(5000);

            Assert.IsTrue(eventHasBeenRaised, "There should be an event within the desired timeout!");
            Assert.AreEqual(SessionState.Closed, session.SessionState);
            Assert.AreEqual(CloseReason.ReceiveTimeout, sessionCloseReason);

            server.Stop();
        }
Exemplo n.º 8
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));
        }
Exemplo n.º 9
0
        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();
        }
Exemplo n.º 10
0
 public void InitializeServer_WithoutProtocolSpecification_ShouldThrowAnException()
 {
     var client = new TestPollingServer(new ServerTestNetworkLinkLayer(), null);
 }
Exemplo n.º 11
0
 public void InitializeServer_WithoutLinkLayer_ShouldRaiseException()
 {
     var server = new TestPollingServer(null);
 }