Example #1
0
        public async Task Disconnects_When_Receiving_Invalid_Packet()
        {
            var pipe         = new DuplexPipe();
            var cancellation = new CancellationTokenSource();

            using (IConnection connection = new DuplexConnection(pipe)) {
                connection.Disconnected += (_, ev) => cancellation.Cancel();
                _ = connection.BeginReceive();

                try {
                    await connection.Output.WriteAsync(new byte[] { 0xFF, 0xFF, 0xFF });

                    await connection.Output.FlushAsync();
                } catch {
                }

                await Task.Delay(100, cancellation.Token).ContinueWith(_ => { });

                Assert.IsTrue(cancellation.IsCancellationRequested);
                Assert.IsFalse(connection.IsConnected);
            }
        }