示例#1
0
        public async Task MoveToAsync_CopiesContentToStream()
        {
            // Arrange
            var input = Enumerable.Repeat((byte)0xba, PagedByteBuffer.PageSize * 3 + 10).ToArray();

            using var buffer = new PagedByteBuffer(ArrayPool <byte> .Shared);
            buffer.Add(input, 0, input.Length);
            var stream = new MemoryStream();

            // Act
            await buffer.MoveToAsync(stream, default);

            // Assert
            Assert.Equal(input, stream.ToArray());

            // Verify adding and moving new content works.
            var newInput = Enumerable.Repeat((byte)0xcb, PagedByteBuffer.PageSize * 2 + 13).ToArray();

            buffer.Add(newInput, 0, newInput.Length);
            stream.SetLength(0);
            await buffer.MoveToAsync(stream, default);

            Assert.Equal(newInput, stream.ToArray());
        }
示例#2
0
        public async Task MoveToAsync_ClearsBuffers()
        {
            // Arrange
            var input = Enumerable.Repeat((byte)0xba, PagedByteBuffer.PageSize * 3 + 10).ToArray();

            using var buffer = new PagedByteBuffer(ArrayPool <byte> .Shared);
            buffer.Add(input, 0, input.Length);
            var stream = new MemoryStream();

            // Act
            await buffer.MoveToAsync(stream, default);

            // Assert
            Assert.Equal(input, stream.ToArray());

            // Verify copying it again works.
            Assert.Equal(0, buffer.Length);
            Assert.False(buffer.Disposed);
            Assert.Empty(buffer.Pages);
        }
示例#3
0
        public async Task MoveToAsync_ReturnsBuffers()
        {
            // Arrange
            var input     = new byte[] { 1, };
            var arrayPool = new Mock <ArrayPool <byte> >();
            var byteArray = new byte[PagedByteBuffer.PageSize];

            arrayPool.Setup(p => p.Rent(PagedByteBuffer.PageSize))
            .Returns(byteArray);
            var memoryStream = new MemoryStream();

            using (var buffer = new PagedByteBuffer(arrayPool.Object))
            {
                // Act
                buffer.Add(input, 0, input.Length);
                await buffer.MoveToAsync(memoryStream, default);

                // Assert
                Assert.Equal(input, memoryStream.ToArray());
            }

            arrayPool.Verify(p => p.Rent(It.IsAny <int>()), Times.Once());
            arrayPool.Verify(p => p.Return(It.IsAny <byte[]>(), It.IsAny <bool>()), Times.Once());
        }