public void Dispose_after_send_causes_subsequent_send_and_receive_to_throw_ObjectDisposed()
        {
            MemoryChannel channel = new MemoryChannel();

            channel.Send(new byte[1]);
            channel.Dispose();

            Assert.Throws <ObjectDisposedException>(() => channel.Send(new byte[1]));
            Assert.Throws <ObjectDisposedException>(() => channel.ReceiveAsync(new byte[1]));
        }
        public void Two_sends_then_receive_with_greater_data_size_completes_sync()
        {
            MemoryChannel channel = new MemoryChannel();

            channel.Send(new byte[] { 1, 2, 3 });
            channel.Send(new byte[] { 4, 5, 6 });

            byte[] receiveBuffer = new byte[7];
            AssertTaskCompleted(6, channel.ReceiveAsync(receiveBuffer));
            Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6, 0 }, receiveBuffer);
        }
        public void Two_sends_then_three_receives_with_equal_data_size_completes_sync()
        {
            MemoryChannel channel = new MemoryChannel();

            channel.Send(new byte[] { 1, 2, 3 });
            channel.Send(new byte[] { 4, 5, 6 });

            byte[] receiveBuffer = new byte[2];
            AssertTaskCompleted(2, channel.ReceiveAsync(receiveBuffer));
            Assert.Equal(new byte[] { 1, 2 }, receiveBuffer);

            byte[] receiveBuffer2 = new byte[2];
            AssertTaskCompleted(2, channel.ReceiveAsync(receiveBuffer2));
            Assert.Equal(new byte[] { 3, 4 }, receiveBuffer2);

            byte[] receiveBuffer3 = new byte[2];
            AssertTaskCompleted(2, channel.ReceiveAsync(receiveBuffer3));
            Assert.Equal(new byte[] { 5, 6 }, receiveBuffer3);
        }
        public void Send_then_receive_with_lower_data_size_completes_sync()
        {
            MemoryChannel channel = new MemoryChannel();

            channel.Send(new byte[] { 1, 2, 3 });

            byte[] receiveBuffer = new byte[2];
            AssertTaskCompleted(2, channel.ReceiveAsync(receiveBuffer));
            Assert.Equal(new byte[] { 1, 2 }, receiveBuffer);
        }
        public void Pending_receive_completes_after_send_with_equal_data_size_followed_by_another_send_and_receive()
        {
            MemoryChannel channel = new MemoryChannel();

            byte[]     receiveBuffer = new byte[3];
            Task <int> receiveTask   = AssertTaskPending(channel.ReceiveAsync(receiveBuffer));

            channel.Send(new byte[] { 1, 2, 3 });

            AssertTaskCompleted(3, receiveTask);
            Assert.Equal(new byte[] { 1, 2, 3 }, receiveBuffer);

            byte[]     receiveBuffer2 = new byte[3];
            Task <int> receiveTask2   = AssertTaskPending(channel.ReceiveAsync(receiveBuffer2));

            channel.Send(new byte[] { 4, 5, 6 });

            AssertTaskCompleted(3, receiveTask2);
            Assert.Equal(new byte[] { 4, 5, 6 }, receiveBuffer2);
        }
        public void Pending_receive_completes_after_send_with_greater_data_size()
        {
            MemoryChannel channel = new MemoryChannel();

            byte[]     receiveBuffer = new byte[3];
            Task <int> receiveTask   = AssertTaskPending(channel.ReceiveAsync(receiveBuffer));

            channel.Send(new byte[] { 1, 2, 3, 4 });

            AssertTaskCompleted(3, receiveTask);
            Assert.Equal(new byte[] { 1, 2, 3 }, receiveBuffer);
        }
        public void Dispose_completes_pending_receive_and_causes_subsequent_send_and_receive_to_throw_ObjectDisposed()
        {
            Task <int>    receiveTask;
            MemoryChannel channel = new MemoryChannel();

            receiveTask = AssertTaskPending(channel.ReceiveAsync(new byte[1]));

            channel.Dispose();
            AssertTaskCompleted(0, receiveTask);

            Assert.Throws <ObjectDisposedException>(() => channel.Send(new byte[1]));
            Assert.Throws <ObjectDisposedException>(() => channel.ReceiveAsync(new byte[1]));
        }
        public void Pending_receive_followed_by_another_receive_throws_InvalidOperation_without_affecting_first_receive()
        {
            MemoryChannel channel = new MemoryChannel();

            byte[]     receiveBuffer = new byte[1];
            Task <int> receiveTask   = AssertTaskPending(channel.ReceiveAsync(receiveBuffer));

            byte[] receiveBuffer2 = new byte[1];
            Assert.Throws <InvalidOperationException>(() => channel.ReceiveAsync(receiveBuffer2));

            channel.Send(new byte[] { 1 });

            AssertTaskCompleted(1, receiveTask);
            Assert.Equal(new byte[] { 1 }, receiveBuffer);
        }
        public void Pending_receive_completes_after_send_with_greater_data_size_followed_by_one_receive_to_read_excess_leaving_final_receive_pending()
        {
            MemoryChannel channel = new MemoryChannel();

            byte[]     receiveBuffer = new byte[3];
            Task <int> receiveTask   = AssertTaskPending(channel.ReceiveAsync(receiveBuffer));

            channel.Send(new byte[] { 1, 2, 3, 4, 5, 6 });

            AssertTaskCompleted(3, receiveTask);
            Assert.Equal(new byte[] { 1, 2, 3 }, receiveBuffer);

            byte[] receiveBuffer2 = new byte[3];
            AssertTaskCompleted(3, channel.ReceiveAsync(receiveBuffer2));
            Assert.Equal(new byte[] { 4, 5, 6 }, receiveBuffer2);

            byte[] receiveBuffer3 = new byte[1];
            AssertTaskPending(channel.ReceiveAsync(receiveBuffer3));
        }