Ejemplo n.º 1
0
        public void MoveTo_WithClear_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);
            arrayPool.Setup(p => p.Return(byteArray, false)).Verifiable();
            var memoryStream = new MemoryStream();

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

                // 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());
        }
Ejemplo n.º 2
0
        public void MoveTo_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
            buffer.MoveTo(stream);

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

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

            buffer.Add(newInput, 0, newInput.Length);

            stream.SetLength(0);
            buffer.MoveTo(stream);

            Assert.Equal(newInput, stream.ToArray());
        }
        private static byte[] ReadBufferedContent(PagedByteBuffer buffer)
        {
            using var memoryStream = new MemoryStream();
            buffer.MoveTo(memoryStream);

            return(memoryStream.ToArray());
        }
Ejemplo n.º 4
0
        public void Add_AppendsToMultiplePages()
        {
            // Arrange
            var input = Enumerable.Repeat((byte)0xba, PagedByteBuffer.PageSize + 10).ToArray();

            using var buffer = new PagedByteBuffer(ArrayPool <byte> .Shared);

            // Act
            buffer.Add(input, 0, input.Length);

            // Assert
            Assert.Equal(2, buffer.Pages.Count);
            Assert.Equal(PagedByteBuffer.PageSize + 10, buffer.Length);
            Assert.Equal(input.ToArray(), ReadBufferedContent(buffer));
        }
Ejemplo n.º 5
0
        public void Add_WithOffsets()
        {
            // Arrange
            var input = new byte[] { 1, 2, 3, 4, 5 };

            using var buffer = new PagedByteBuffer(ArrayPool <byte> .Shared);

            // Act
            buffer.Add(input, 1, 3);

            // Assert
            Assert.Single(buffer.Pages);
            Assert.Equal(3, buffer.Length);
            Assert.Equal(new byte[] { 2, 3, 4 }, ReadBufferedContent(buffer));
        }
Ejemplo n.º 6
0
        public void Add_CreatesNewPage()
        {
            // Arrange
            var input = Encoding.UTF8.GetBytes("Hello world");

            using var buffer = new PagedByteBuffer(ArrayPool <byte> .Shared);

            // Act
            buffer.Add(input, 0, input.Length);

            // Assert
            Assert.Single(buffer.Pages);
            Assert.Equal(input.Length, buffer.Length);
            Assert.Equal(input, ReadBufferedContent(buffer));
        }
Ejemplo n.º 7
0
        public void Add_FillsUpBuffer()
        {
            // Arrange
            var input1 = Enumerable.Repeat((byte)0xba, PagedByteBuffer.PageSize - 1).ToArray();
            var input2 = new byte[] { 0xca };

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

            // Act
            buffer.Add(input2, 0, 1);

            // Assert
            Assert.Single(buffer.Pages);
            Assert.Equal(PagedByteBuffer.PageSize, buffer.Length);
            Assert.Equal(Enumerable.Concat(input1, input2).ToArray(), ReadBufferedContent(buffer));
        }
Ejemplo n.º 8
0
        public void Add_AppendsToExistingPage()
        {
            // Arrange
            var input1 = Encoding.UTF8.GetBytes("Hello");
            var input2 = Encoding.UTF8.GetBytes("world");

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

            // Act
            buffer.Add(input2, 0, input2.Length);

            // Assert
            Assert.Single(buffer.Pages);
            Assert.Equal(10, buffer.Length);
            Assert.Equal(Enumerable.Concat(input1, input2).ToArray(), ReadBufferedContent(buffer));
        }
Ejemplo n.º 9
0
        public void Dispose_ReturnsBuffers_ExactlyOnce()
        {
            // Arrange
            var input     = Enumerable.Repeat((byte)0xba, PagedByteBuffer.PageSize * 3 + 10).ToArray();
            var arrayPool = new Mock <ArrayPool <byte> >();

            arrayPool.Setup(p => p.Rent(PagedByteBuffer.PageSize))
            .Returns(new byte[PagedByteBuffer.PageSize]);

            var buffer = new PagedByteBuffer(arrayPool.Object);

            // Act
            buffer.Add(input, 0, input.Length);
            buffer.Dispose();
            buffer.Dispose();

            arrayPool.Verify(p => p.Rent(It.IsAny <int>()), Times.Exactly(4));
            arrayPool.Verify(p => p.Return(It.IsAny <byte[]>(), It.IsAny <bool>()), Times.Exactly(4));
        }
Ejemplo n.º 10
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);
        }
        /// <summary>
        /// Initializes a new instance of <see cref="FileBufferingWriteStream"/>.
        /// </summary>
        /// <param name="memoryThreshold">
        /// The maximum amount of memory in bytes to allocate before switching to a file on disk.
        /// Defaults to 32kb.
        /// </param>
        /// <param name="bufferLimit">
        /// The maximum amouont of bytes that the <see cref="FileBufferingWriteStream"/> is allowed to buffer.
        /// </param>
        /// <param name="tempFileDirectoryAccessor">Provides the location of the directory to write buffered contents to.
        /// When unspecified, uses the value specified by the environment variable <c>ASPNETCORE_TEMP</c> if available, otherwise
        /// uses the value returned by <see cref="Path.GetTempPath"/>.
        /// </param>
        public FileBufferingWriteStream(
            int memoryThreshold = DefaultMemoryThreshold,
            long?bufferLimit    = null,
            Func <string> tempFileDirectoryAccessor = null)
        {
            if (memoryThreshold < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(memoryThreshold));
            }

            if (bufferLimit != null && bufferLimit < memoryThreshold)
            {
                // We would expect a limit at least as much as memoryThreshold
                throw new ArgumentOutOfRangeException(nameof(bufferLimit), $"{nameof(bufferLimit)} must be larger than {nameof(memoryThreshold)}.");
            }

            _memoryThreshold           = memoryThreshold;
            _bufferLimit               = bufferLimit;
            _tempFileDirectoryAccessor = tempFileDirectoryAccessor ?? AspNetCoreTempDirectory.TempDirectoryFactory;
            PagedByteBuffer            = new PagedByteBuffer(ArrayPool <byte> .Shared);
        }