public async Task WriteAsync_AfterMemoryThresholdIsReached_BuffersToMemory()
        {
            // Arrange
            var input = new byte[] { 1, 2, 3, 4, 5, 6, 7 };

            using var bufferingStream = new FileBufferingWriteStream(memoryThreshold: 4, tempFileDirectoryAccessor: () => TempDirectory);

            // Act
            await bufferingStream.WriteAsync(input, 0, 5);

            await bufferingStream.WriteAsync(input, 5, 2);

            // Assert
            var pageBuffer = bufferingStream.PagedByteBuffer;
            var fileStream = bufferingStream.FileStream;

            // File should have been created.
            Assert.NotNull(fileStream);
            var fileBytes = ReadFileContent(fileStream);

            Assert.Equal(input.Length, bufferingStream.Length);

            Assert.Equal(new byte[] { 1, 2, 3, 4, 5, }, fileBytes);
            Assert.Equal(new byte[] { 6, 7 }, ReadBufferedContent(pageBuffer));
        }
        public async Task WriteAsync_DoesNotThrow_IfBufferLimitIsReached()
        {
            // Arrange
            var input = new byte[5];

            using var bufferingStream = new FileBufferingWriteStream(memoryThreshold: 2, bufferLimit: 10, tempFileDirectoryAccessor: () => TempDirectory);

            // Act
            await bufferingStream.WriteAsync(input, 0, input.Length);

            await bufferingStream.WriteAsync(input, 0, input.Length); // Should get to exactly the buffer limit, which is fine

            // If we got here, the test succeeded.
        }
        public async Task WriteAsync_Throws_IfWriteCumulativeWritesExceedsBuffersLimit()
        {
            // Arrange
            var input           = new byte[6];
            var bufferingStream = new FileBufferingWriteStream(memoryThreshold: 2, bufferLimit: 10, tempFileDirectoryAccessor: () => TempDirectory);

            // Act
            await bufferingStream.WriteAsync(input, 0, input.Length);

            var exception = await Assert.ThrowsAsync <IOException>(() => bufferingStream.WriteAsync(input, 0, input.Length));

            Assert.Equal("Buffer limit exceeded.", exception.Message);

            // Verify we return the buffer.
            Assert.True(bufferingStream.Disposed);
        }
        public async Task WriteAsync_BuffersContentToMemory()
        {
            // Arrange
            using var bufferingStream = new FileBufferingWriteStream(tempFileDirectoryAccessor: () => TempDirectory);
            var input = Encoding.UTF8.GetBytes("Hello world");

            // Act
            await bufferingStream.WriteAsync(input, 0, input.Length);

            // Assert
            // We should have written content to memory
            var pagedByteBuffer = bufferingStream.PagedByteBuffer;

            Assert.Equal(input, ReadBufferedContent(pagedByteBuffer));

            // No files should not have been created.
            Assert.Null(bufferingStream.FileStream);
        }
        public async Task WriteAsync_BeforeMemoryThresholdIsReached_WritesToMemory()
        {
            // Arrange
            var input = new byte[] { 1, 2, };

            using var bufferingStream = new FileBufferingWriteStream(memoryThreshold: 2, tempFileDirectoryAccessor: () => TempDirectory);

            // Act
            await bufferingStream.WriteAsync(input, 0, 2);

            // Assert
            var pageBuffer = bufferingStream.PagedByteBuffer;
            var fileStream = bufferingStream.FileStream;

            // File should have been created.
            Assert.Null(fileStream);

            // No content should be in the memory stream
            Assert.Equal(2, pageBuffer.Length);
            Assert.Equal(input, ReadBufferedContent(pageBuffer));
        }
        public async Task WriteAsync_BuffersContentToDisk_WhenWriteWillOverflowMemoryThreshold()
        {
            // Arrange
            var input = new byte[] { 1, 2, 3, };

            using var bufferingStream = new FileBufferingWriteStream(memoryThreshold: 2, tempFileDirectoryAccessor: () => TempDirectory);

            // Act
            await bufferingStream.WriteAsync(input, 0, input.Length);

            // Assert
            var pageBuffer = bufferingStream.PagedByteBuffer;
            var fileStream = bufferingStream.FileStream;

            // File should have been created.
            Assert.NotNull(fileStream);
            var fileBytes = ReadFileContent(fileStream);

            Assert.Equal(input, fileBytes);

            // No content should be in the memory stream
            Assert.Equal(0, pageBuffer.Length);
        }