public async void ShouldThrowIOExceptionWhenReadAsyncIsFaulted()
        {
            var reader = new ChunkReader(AsyncTestStream.CreateFailingStream(new IOException("some error")));

            var ex = await Record.ExceptionAsync(() => reader.ReadNextMessagesAsync(new MemoryStream()));

            ex.Should().NotBeNull();
            ex.Should().BeAssignableTo <IOException>().Which.Message.Should().Be("some error");
        }
        public async void ShouldThrowIOExceptionWhenReadAsyncReturnsZeroOrLess(int returnValue)
        {
            var reader = new ChunkReader(AsyncTestStream.CreateStream(Task.FromResult(returnValue)));

            var ex = await Record.ExceptionAsync(() => reader.ReadNextMessagesAsync(new MemoryStream()));

            ex.Should().NotBeNull();
            ex.Should().BeAssignableTo <IOException>().Which.Message.Should().StartWith("Unexpected end of stream, read returned");
        }
        private static async Task <byte[]> ConstructMessage(byte[] buffer)
        {
            var stream = new MemoryStream();
            var reader = new ChunkReader(new MemoryStream(buffer));

            await reader.ReadNextMessagesAsync(stream);

            return(stream.ToArray());
        }
        public async void ShouldThrowCancellationWhenReadAsyncIsCancelled()
        {
            var reader = new ChunkReader(AsyncTestStream.CreateCancellingStream());

            var ex = await Record.ExceptionAsync(() => reader.ReadNextMessagesAsync(new MemoryStream()));

            ex.Should().NotBeNull();
            ex.Should().BeAssignableTo <OperationCanceledException>();
        }
Exemple #5
0
        private static byte[] ConstructMessage(byte[] buffer)
        {
            var stream = new MemoryStream();
            var reader = new ChunkReader(new MemoryStream(buffer));

            reader.ReadNextMessages(stream);

            return(stream.ToArray());
        }
        public async void ShouldThrowWhenEndOfMessageMarkerNotPresentAsync(byte[] input)
        {
            var reader = new ChunkReader(new MemoryStream(input));

            var targetStream = new MemoryStream();
            var ex           = await Record.ExceptionAsync(() => reader.ReadNextMessagesAsync(targetStream));

            ex.Should().NotBeNull();
            ex.Should().BeOfType <IOException>().Which.Message.Should().StartWith("Unexpected end of stream");
        }
        public void ShouldThrowWhenEndOfStreamIsDetected(byte[] input)
        {
            var reader = new ChunkReader(new MemoryStream(input));

            var targetStream = new MemoryStream();
            var ex           = Record.Exception(() => reader.ReadNextMessages(targetStream));

            ex.Should().NotBeNull();
            ex.Should().BeOfType <IOException>().Which.Message.Should().StartWith("Unexpected end of stream");
        }
        public void ShouldReadMessageSpanningMultipleChunks(byte[] input, byte[] expectedMessageBuffers, int expectedCount)
        {
            var reader = new ChunkReader(new MemoryStream(input));

            var targetStream   = new MemoryStream();
            var count          = reader.ReadNextMessages(targetStream);
            var messageBuffers = targetStream.ToArray();

            count.Should().Be(expectedCount);
            messageBuffers.Should().Equal(expectedMessageBuffers);
        }
        public async void ShouldResetInternalBufferPositionsWhenWritableBufferIsSmallerThanSetWatermarkAsync()
        {
            var input  = GenerateMessageChunk(Constants.ChunkBufferSize - Constants.ChunkBufferResetPositionsWatermark);
            var logger = LoggingHelper.GetTraceEnabledLogger();
            var reader = new ChunkReader(new MemoryStream(input), logger.Object);

            var count = await reader.ReadNextMessagesAsync(new MemoryStream());

            count.Should().Be(1);
            logger.Verify(l => l.Trace(It.IsRegex(CompactingArgumentRegEx), It.IsAny <object[]>()), Times.Once);
        }
        public void ShouldResetInternalBufferPositionsWhenAMessageOfNChunks()
        {
            var size   = 3 * Constants.MaxChunkSize;
            var input  = GenerateMessageChunk(3 * Constants.MaxChunkSize);
            var logger = LoggingHelper.GetTraceEnabledLogger();
            var reader = new ChunkReader(new MemoryStream(input), logger.Object);

            var count = reader.ReadNextMessages(new MemoryStream());

            count.Should().Be(1);
            logger.Verify(l => l.Trace(It.IsRegex(CompactingArgumentRegEx), It.IsAny <object[]>()), Times.AtLeast(size / Constants.ChunkBufferSize));
        }
        public async void ShouldReadSingleMessageStreamLargerThanBufferSizeAsync()
        {
            const int chunkSize           = 22 * 1024;
            const int totalStreamSizeByte = 2 * chunkSize;
            var       inputStream         = new MemoryStream(GenerateMessages(chunkSize, totalStreamSizeByte)); // Will create a message of two 11k chunks, these will straddle the 16k internal buffer size...
            var       resultStream        = new MemoryStream();
            var       reader = new ChunkReader(inputStream);

            var count = await reader.ReadNextMessagesAsync(resultStream);

            count.Should().Be(1);
        }
        public async void ShouldReadNoopsBetweenMessagesAsync(byte[] input, byte[] expectedMessageBuffers, int expectedCount)
        {
            var reader = new ChunkReader(new MemoryStream(input));

            var targetStream = new MemoryStream();
            var count        = await reader.ReadNextMessagesAsync(targetStream);

            var messageBuffers = targetStream.ToArray();

            count.Should().Be(expectedCount);
            messageBuffers.Should().Equal(expectedMessageBuffers);
        }
        public async void ShouldResetInternalBufferPositionsWhenWritableBufferIsSmallerThanSetWatermarkWithConsecutiveMessagesAsync()
        {
            const int messageSizePerChunk = 1000;
            const int maxBytes            = Constants.ChunkBufferSize;

            var input  = GenerateMessages(messageSizePerChunk, maxBytes);
            var logger = LoggingHelper.GetTraceEnabledLogger();
            var reader = new ChunkReader(new MemoryStream(input), logger.Object);

            var count = await reader.ReadNextMessagesAsync(new MemoryStream());

            count.Should().BeGreaterOrEqualTo(maxBytes / messageSizePerChunk);
            logger.Verify(l => l.Trace(It.IsRegex(CompactingArgumentRegEx), It.IsAny <object[]>()), Times.Once);
        }
        public async void ShouldResetBufferStreamPositionAsync()
        {
            var data = GenerateMessages(1000, 128 * 1024);

            var logger = new Mock <ILogger>();
            var reader = new ChunkReader(new MemoryStream(data.ToArray()), logger.Object);

            var bufferStream = new MemoryStream();

            bufferStream.Write(GenerateMessageChunk(1035));

            var bufferPosition = bufferStream.Position;

            var count = await reader.ReadNextMessagesAsync(bufferStream);

            bufferStream.Position.Should().Be(bufferPosition);
        }
        public async void ShouldReadMultipleMessageStreamLargerThanBufferSizeAsync()
        {
            const int chunkSize           = 11 * 1024;
            const int totalStreamSizeByte = 2 * chunkSize;
            var       inputStream         = new MemoryStream();

            inputStream.Write(GenerateMessages(chunkSize, totalStreamSizeByte));    //Add a message made of two 11k chunks.
            inputStream.Write(GenerateMessages(chunkSize, totalStreamSizeByte));    //Add another message made of two 11k chunks.
            inputStream.Position = 0;

            var resultStream = new MemoryStream();
            var reader       = new ChunkReader(inputStream);

            var count = await reader.ReadNextMessagesAsync(resultStream);

            count.Should().Be(2);
        }