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(); }
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); }
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); }