コード例 #1
0
        public void TransportShouldNotDropConnectionWhenKeepAliveSuccess()
        {
            MemoryStream             stream           = new MemoryStream();
            ConcreteNetworkTransport networkTransport = new ConcreteNetworkTransport(stream, CancellationToken.None);

            networkTransport.StartKeepAlive(100, 1, 500);

            Thread.Sleep(200);

            GetHeaderFromStream(stream, out byte packetType, out int length);
            byte[] payload = GetPayloadFromStream(stream, length);

            Assert.AreEqual("PING", Encoding.Unicode.GetString(payload));

            byte[] payloadSent = Encoding.Unicode.GetBytes("PONG");
            payload = ConstructPayload(payloadSent, 1);
            stream.Write(payload, 0, payload.Length);
            stream.Position = 0;

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            networkTransport.StartReading();
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            Assert.IsTrue(stream.CanRead);
            Assert.IsTrue(stream.CanWrite);

            networkTransport.DropConnection();
        }
コード例 #2
0
        public async Task SendShouldAddPacketTypeInHeader()
        {
            MemoryStream             stream           = new MemoryStream();
            ConcreteNetworkTransport networkTransport = new ConcreteNetworkTransport(stream, CancellationToken.None);

            await networkTransport.SendData(new byte[] { 100, 101 });

            GetHeaderFromStream(stream, out byte packetType, out int length);

            Assert.AreEqual(2, packetType);
        }
コード例 #3
0
        public void TransportShouldDropConnectionWhenKeepAliveFails()
        {
            MemoryStream             stream           = new MemoryStream();
            ConcreteNetworkTransport networkTransport = new ConcreteNetworkTransport(stream, CancellationToken.None);

            networkTransport.StartKeepAlive(100, 1, 100);

            Thread.Sleep(1000);

            Assert.IsFalse(stream.CanRead);
            Assert.IsFalse(stream.CanWrite);
        }
コード例 #4
0
        public async Task SendShouldWritePayloadAfterHeader()
        {
            MemoryStream             stream           = new MemoryStream();
            ConcreteNetworkTransport networkTransport = new ConcreteNetworkTransport(stream, CancellationToken.None);

            byte[] payloadSent = new byte[] { 100, 101 };
            await networkTransport.SendData(payloadSent);

            GetHeaderFromStream(stream, out byte packetType, out int length);
            byte[] payloadReceived = GetPayloadFromStream(stream, (int)length);

            Assert.AreEqual(2, packetType);
            Assert.AreEqual(2, length);
            CollectionAssert.AreEqual(payloadSent, payloadReceived);
        }
コード例 #5
0
        public void TransportShouldSendKeepAliveAfterTimeout()
        {
            MemoryStream             stream           = new MemoryStream();
            ConcreteNetworkTransport networkTransport = new ConcreteNetworkTransport(stream, CancellationToken.None);

            networkTransport.StartKeepAlive(500, 5, 10 * 1000);

            Thread.Sleep(1000);

            GetHeaderFromStream(stream, out byte packetType, out int length);
            byte[] payload = GetPayloadFromStream(stream, length);

            Assert.AreEqual(1, packetType);
            Assert.AreEqual("PING", Encoding.Unicode.GetString(payload));

            networkTransport.DropConnection();
        }
コード例 #6
0
        public void TransportShouldDropConnectionOnInvalidPacketType()
        {
            MemoryStream             stream           = new MemoryStream();
            ConcreteNetworkTransport networkTransport = new ConcreteNetworkTransport(stream, CancellationToken.None);

            byte[] payloadSent = new byte[] { 100, 101 };
            byte[] payload     = ConstructPayload(payloadSent, 1);
            stream.Write(payload, 0, payload.Length);
            stream.Position = 0;

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            networkTransport.StartReading();
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            Thread.Sleep(1000);

            Assert.IsFalse(stream.CanRead);
            Assert.IsFalse(stream.CanWrite);
        }
コード例 #7
0
        public void ReceiveShouldContainCorrectData()
        {
            MemoryStream             stream           = new MemoryStream();
            ConcreteNetworkTransport networkTransport = new ConcreteNetworkTransport(stream, CancellationToken.None);

            byte[] payloadRecived = null;
            networkTransport.OnDataReceived += (data) =>
            {
                payloadRecived = data;
            };

            byte[] payloadSent = new byte[] { 100, 101 };
            byte[] payload     = ConstructPayload(payloadSent);
            stream.Write(payload, 0, payload.Length);
            stream.Position = 0;

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            networkTransport.StartReading();
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            Thread.Sleep(1000);

            CollectionAssert.AreEqual(payloadSent, payloadRecived);
        }