Example #1
0
        public void ConnectedClient_ReceivesNoServerFrames_ShouldTimeoutAndDisonnect()
        {
            var timedOutEventRaised = new AutoResetEvent(false);

            var connectionRequestResponse = new ServerDataFrame()
            {
                SequenceId = 7,
                Payload    = BitConverter.GetBytes(12345)
            };

            var networkLayer = new ClientTestNetworkLinkLayer();

            var client = new TestPollingClient(networkLayer);

            client.ReceiveTimeout = TimeSpan.FromMilliseconds(500);

            client.ConnectionStateChanged += (sender, args) =>
            {
                if (args.State == ConnectionState.Timeout)
                {
                    timedOutEventRaised.Set();
                }
            };

            client.ConnectAsync();

            byte[] data = this.serverDataFrameSerializer.Serialize(connectionRequestResponse);
            networkLayer.Receive(data);

            Assert.AreEqual(ConnectionState.Connected, client.ConnectionState);

            var isEventRaised = timedOutEventRaised.WaitOne(5000);

            Assert.IsTrue(isEventRaised);
        }
Example #2
0
        public void ConnectedClient_WhenConnected_ShouldSendAControlFrameAtLeast()
        {
            var connectionRequestResponse = new ServerDataFrame()
            {
                SequenceId = 7,
                Payload    = BitConverter.GetBytes(12345)
            };

            var networkLayer = new ClientTestNetworkLinkLayer();

            var client = new TestPollingClient(networkLayer);

            client.ConnectAsync();

            byte[] data = this.serverDataFrameSerializer.Serialize(connectionRequestResponse);
            networkLayer.Receive(data);

            Assert.AreEqual(ConnectionState.Connected, client.ConnectionState);

            client.DisconnectAsync().Wait(5000);

            var allSentControlFrames = networkLayer.SentBytes.Select(b => this.clientAnyFrameSerializer.Deserialze(b)).ToList();

            Assert.IsTrue(allSentControlFrames.Any());
            Assert.IsTrue(allSentControlFrames.OfType <ClientControlFrame>().Any());
        }
Example #3
0
        public void ConnectedClient_InIdleMode_ShouldSendKeepAlivePackets()
        {
            var connectionRequestResponse = new ServerDataFrame()
            {
                SequenceId = 7,
                Payload    = BitConverter.GetBytes(12345)
            };

            var networkLayer = new ClientTestNetworkLinkLayer();

            var client = new TestPollingClient(networkLayer);

            client.ReceiveTimeout         = Timeout.InfiniteTimeSpan;
            client.InitialPollingPoolSize = 0;

            client.ConnectAsync();

            byte[] data = this.serverDataFrameSerializer.Serialize(connectionRequestResponse);
            networkLayer.Receive(data);

            Assert.AreEqual(ConnectionState.Connected, client.ConnectionState);

            var captureDuration = new Stopwatch();

            var captureKeepAlivePackesTask = new Task <int>(() =>
            {
                captureDuration.Start();
                var allSentControlFrames = networkLayer.SentBytes.Select(b => this.clientAnyFrameSerializer.Deserialze(b)).OfType <ClientControlFrame>().ToList();

                var startNumberOfPackets = allSentControlFrames.Count;

                Thread.Sleep((int)(client.Protocol.KeepAliveClientInterval.TotalMilliseconds * 1.5));
                allSentControlFrames = networkLayer.SentBytes.Select(b => this.clientAnyFrameSerializer.Deserialze(b)).OfType <ClientControlFrame>().ToList();

                var totalNumberOfPackets = allSentControlFrames.Count - startNumberOfPackets;

                captureDuration.Stop();

                return(totalNumberOfPackets);
            });

            captureKeepAlivePackesTask.Start();
            captureKeepAlivePackesTask.Wait();

            var allSentWhileConnected = captureKeepAlivePackesTask.Result;

            var atLeastExpectedNumberOfKeepAlivePackes = (captureDuration.ElapsedMilliseconds / client.Protocol.KeepAliveClientInterval.TotalMilliseconds) - 1;

            Assert.IsTrue(allSentWhileConnected > atLeastExpectedNumberOfKeepAlivePackes);
        }
Example #4
0
        public void ConnectingClient_ReceivesResponseWhileConnecting_ShouldUseClientIdInData()
        {
            var connectionRequestResponse = new ServerDataFrame()
            {
                SequenceId = 7,
                Payload    = BitConverter.GetBytes(12345)
            };

            var networkLayer = new ClientTestNetworkLinkLayer();

            var client = new TestPollingClient(networkLayer);

            client.ConnectAsync();

            networkLayer.Receive(this.serverDataFrameSerializer.Serialize(connectionRequestResponse));
            client.Send(new ClientDataFrame());
            client.DisconnectAsync();
        }
Example #5
0
        public void ConnectingClient_ReceivesResponseWhileConnecting_ShouldSwitchToConnectedState()
        {
            var connectionRequestResponse = new ServerDataFrame()
            {
                SequenceId = 7,
                Payload    = BitConverter.GetBytes(12345)
            };

            var networkLayer = new ClientTestNetworkLinkLayer();

            var client = new TestPollingClient(networkLayer);

            client.ConnectAsync();

            byte[] data = this.serverDataFrameSerializer.Serialize(connectionRequestResponse);

            networkLayer.Receive(data);
            Assert.AreEqual(ConnectionState.Connected, client.ConnectionState);

            client.DisconnectAsync();
        }