public async Task ConsumePartialBufferWorks()
        {
            Reader = new StreamPipeReader(MemoryStream, 16, new TestMemoryPool());
            Write(Encoding.ASCII.GetBytes(new string('a', 8)));
            var readResult = await Reader.ReadAsync();

            Reader.AdvanceTo(readResult.Buffer.GetPosition(4), readResult.Buffer.End);
            MemoryStream.Position = 0;

            readResult = await Reader.ReadAsync();

            var resultString = Encoding.ASCII.GetString(readResult.Buffer.ToArray());

            Assert.Equal(new string('a', 12), resultString);
            Reader.AdvanceTo(readResult.Buffer.End);
        }
        public async Task AsyncReadWorks()
        {
            MemoryStream = new AsyncStream();
            Reader       = new StreamPipeReader(MemoryStream, 16, new TestMemoryPool());
            Write(Encoding.ASCII.GetBytes(new string('a', 10000)));

            for (var i = 0; i < 99; i++)
            {
                var readResult = await Reader.ReadAsync();

                Reader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End);
            }

            var result = await Reader.ReadAsync();

            Assert.Equal(1600, result.Buffer.Length);
            Reader.AdvanceTo(result.Buffer.End);
        }
        public async Task AdvanceMultipleSegments()
        {
            Reader = new StreamPipeReader(MemoryStream, 16, new TestMemoryPool());
            Write(new byte[128]);

            var result = await Reader.ReadAsync();

            Assert.Equal(16, result.Buffer.Length);
            Reader.AdvanceTo(result.Buffer.Start, result.Buffer.End);

            var result2 = await Reader.ReadAsync();

            Assert.Equal(32, result2.Buffer.Length);
            Reader.AdvanceTo(result.Buffer.End, result2.Buffer.End);

            var result3 = await Reader.ReadAsync();

            Assert.Equal(32, result3.Buffer.Length);
        }
        public async Task ReadBetweenBlocks()
        {
            var blockSize = 16;

            Reader = new StreamPipeReader(MemoryStream, blockSize, new TestMemoryPool());

            WriteWithoutPosition(Enumerable.Repeat((byte)'a', blockSize - 5).ToArray());
            Write(Encoding.ASCII.GetBytes("Hello World"));

            // ReadAsync will only return one chunk at a time, so Advance/ReadAsync to get two chunks
            var result = await Reader.ReadAsync();

            Reader.AdvanceTo(result.Buffer.Start, result.Buffer.End);
            result = await Reader.ReadAsync();

            var buffer = result.Buffer;

            Assert.False(buffer.IsSingleSegment);
            var helloBuffer = buffer.Slice(blockSize - 5);

            Assert.False(helloBuffer.IsSingleSegment);
            var memory = new List <ReadOnlyMemory <byte> >();

            foreach (var m in helloBuffer)
            {
                memory.Add(m);
            }

            var spans = memory;

            Reader.AdvanceTo(buffer.Start, buffer.Start);

            Assert.Equal(2, memory.Count);
            var helloBytes = new byte[spans[0].Length];

            spans[0].Span.CopyTo(helloBytes);
            var worldBytes = new byte[spans[1].Length];

            spans[1].Span.CopyTo(worldBytes);
            Assert.Equal("Hello", Encoding.ASCII.GetString(helloBytes));
            Assert.Equal(" World", Encoding.ASCII.GetString(worldBytes));
        }
        public async Task ReadMultipleTimesAdvanceFreesAppropriately()
        {
            var blockSize = 16;
            var pool      = new TestMemoryPool();

            Reader = new StreamPipeReader(MemoryStream, blockSize, pool);
            Write(Encoding.ASCII.GetBytes(new string('a', 10000)));

            for (var i = 0; i < 99; i++)
            {
                var readResult = await Reader.ReadAsync();

                Reader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End);
            }

            var result = await Reader.ReadAsync();

            Reader.AdvanceTo(result.Buffer.End);
            Assert.Equal(1, pool.GetRentCount());
        }
        public async Task AdvancePastMinReadSizeReadAsyncReturnsMoreData()
        {
            Reader = new StreamPipeReader(MemoryStream, 16, new TestMemoryPool());
            Write(new byte[32]);
            var result = await Reader.ReadAsync();

            Assert.Equal(16, result.Buffer.Length);

            Reader.AdvanceTo(result.Buffer.GetPosition(12), result.Buffer.End);
            result = await Reader.ReadAsync();

            Assert.Equal(20, result.Buffer.Length);
        }
        public async Task ReadWithAdvanceSmallSegments()
        {
            Reader = new StreamPipeReader(MemoryStream, 16, new TestMemoryPool());
            Write(new byte[128]);

            var readResult = await Reader.ReadAsync();

            Reader.AdvanceTo(readResult.Buffer.End);

            readResult = await Reader.ReadAsync();

            Assert.Equal(16, readResult.Buffer.Length);
            Assert.True(readResult.Buffer.IsSingleSegment);
        }
        public async Task ReadWithAdvanceDifferentSegmentSize()
        {
            Reader = new StreamPipeReader(MemoryStream, 4095, new TestMemoryPool());
            Write(new byte[10000]);

            var readResult = await Reader.ReadAsync();

            Reader.AdvanceTo(readResult.Buffer.End);

            readResult = await Reader.ReadAsync();

            Assert.Equal(4095, readResult.Buffer.Length);
            Assert.True(readResult.Buffer.IsSingleSegment);
        }
Ejemplo n.º 9
0
        public async Task ArrayPoolUsedByDefault()
        {
            WriteByteArray(20);
            var reader = new StreamPipeReader(Stream);
            var result = await reader.ReadAsync();

            SequenceMarshal.TryGetReadOnlySequenceSegment(
                result.Buffer,
                out var startSegment,
                out var startIndex,
                out var endSegment,
                out var endIndex);

            var start = (BufferSegment)startSegment;
            var end   = (BufferSegment)endSegment;

            Assert.Same(start, end);
            Assert.IsType <byte[]>(start.MemoryOwner);

            reader.AdvanceTo(result.Buffer.End);
            reader.Complete();
        }