public void Write_DoesNotThrow_IfBufferLimitIsReached()
        {
            // Arrange
            var input = new byte[5];

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

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

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

            // Act
            bufferingStream.Write(input, 0, input.Length);
            var exception = Assert.Throws <IOException>(() => bufferingStream.Write(input, 0, input.Length));

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

            // Verify we return the buffer.
            Assert.True(bufferingStream.Disposed);
        }
        public void Write_AfterMemoryThresholdIsReached_BuffersToMemory()
        {
            // Arrange
            var input = new byte[] { 1, 2, 3, 4, 5, 6, 7 };

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

            // Act
            bufferingStream.Write(input, 0, 5);
            bufferingStream.Write(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(new byte[] { 1, 2, 3, 4, 5, }, fileBytes);

            Assert.Equal(new byte[] { 6, 7 }, ReadBufferedContent(pageBuffer));
        }
        public void Write_BuffersContentToDisk_WhenMemoryThresholdIsReached()
        {
            // Arrange
            var input = new byte[] { 1, 2, 3, };

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

            // Act
            bufferingStream.Write(input, 2, 1);

            // 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);
        }
Beispiel #5
0
        public async Task DrainBufferAsync_WithContentInDisk_CopiesContentFromMemoryStream()
        {
            // Arrange
            var input = Enumerable.Repeat((byte)0xca, 30).ToArray();

            using var bufferingStream = new FileBufferingWriteStream(memoryThreshold: 21, tempFileDirectoryAccessor: () => TempDirectory);
            bufferingStream.Write(input, 0, input.Length);
            var memoryStream = new MemoryStream();

            // Act
            await bufferingStream.DrainBufferAsync(memoryStream, default);

            // Assert
            Assert.Equal(input, memoryStream.ToArray());
        }
Beispiel #6
0
        public async Task DrainBufferAsync_CopiesContentFromMemoryStream()
        {
            // Arrange
            var input = new byte[] { 1, 2, 3, 4, 5 };

            using var bufferingStream = new FileBufferingWriteStream(tempFileDirectoryAccessor: () => TempDirectory);
            bufferingStream.Write(input, 0, input.Length);
            var memoryStream = new MemoryStream();

            // Act
            await bufferingStream.DrainBufferAsync(memoryStream, default);

            // Assert
            Assert.Equal(input, memoryStream.ToArray());
        }
        public async Task DrainBufferAsync_IncludesContentPossiblyBufferedByFileStream()
        {
            // We want to ensure that the FileStream (which has a 1-byte buffer) flushes prior to the other read stream reading input.
            // Arrange
            var input = new byte[] { 3, };

            using var bufferingStream = new FileBufferingWriteStream(0, tempFileDirectoryAccessor: () => TempDirectory);
            bufferingStream.Write(input, 0, input.Length);
            var memoryStream = new MemoryStream();

            // Act
            await bufferingStream.DrainBufferAsync(memoryStream, default);

            // Assert
            Assert.Equal(input, memoryStream.ToArray());
            Assert.Equal(0, bufferingStream.Length);
        }
Beispiel #8
0
        public void Write_BuffersContentToMemory()
        {
            // Arrange
            using var bufferingStream = new FileBufferingWriteStream(tempFileDirectoryAccessor: () => TempDirectory);
            var input = Encoding.UTF8.GetBytes("Hello world");

            // Act
            bufferingStream.Write(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);
        }
Beispiel #9
0
        public void Write_BeforeMemoryThresholdIsReached_WritesToMemory()
        {
            // Arrange
            var input = new byte[] { 1, 2, };

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

            // Act
            bufferingStream.Write(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));
        }