public void ShouldResetCapacityWhenAboveMaxBufferSizeAfterEachSend()
        {
            var buffer = new byte[1536];
            var stream = new MemoryStream();
            var logger = new Mock <ILogger>();
            var writer = new ChunkWriter(stream, 512, 1024, logger.Object);

            writer.OpenChunk();
            writer.Write(buffer, 0, buffer.Length);
            writer.CloseChunk();
            writer.Send();

            writer.OpenChunk();
            writer.Write(buffer, 0, buffer.Length);
            writer.CloseChunk();
            writer.Send();

            logger.Verify(l => l.Info(It.IsRegex("^Shrinking write buffers to the"), It.IsAny <object[]>()), Times.Exactly(2));
        }
        public void ShouldResetCapacityWhenAboveMaxBufferSize(int defaultBufferSize, int maxBufferSize, int messageSize)
        {
            var buffer = new byte[messageSize];
            var stream = new MemoryStream();
            var logger = new Mock <ILogger>();
            var writer = new ChunkWriter(stream, defaultBufferSize, maxBufferSize, logger.Object);

            writer.OpenChunk();
            writer.Write(buffer, 0, buffer.Length);
            writer.CloseChunk();
            writer.Send();

            logger.Verify(l => l.Info(It.IsRegex("^Shrinking write buffers to the"), It.IsAny <object[]>()), Times.Once);
        }
Example #3
0
        private static byte[] CreateChunkedMessage(byte[] content)
        {
            var stream      = new MemoryStream();
            var chunkWriter = new ChunkWriter(stream);

            chunkWriter.OpenChunk();
            chunkWriter.Write(content, 0, content.Length);
            chunkWriter.CloseChunk();

            chunkWriter.OpenChunk();
            chunkWriter.CloseChunk();

            chunkWriter.Send();

            return(stream.ToArray());
        }
        public void ShouldWriteToUnderlyingStreamUponSend()
        {
            var buffer = new byte[1024];
            var stream = new MemoryStream();
            var writer = new ChunkWriter(stream);

            // Write data
            writer.OpenChunk();
            writer.Write(buffer, 0, buffer.Length);
            writer.CloseChunk();

            stream.Length.Should().Be(0);

            writer.Send();

            stream.Length.Should().Be(buffer.Length + 2);
        }
        public void ShouldLogDataOnSend()
        {
            var buffer = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray();
            var stream = new MemoryStream();
            var logger = LoggingHelper.GetTraceEnabledLogger();
            var writer = new ChunkWriter(stream, logger.Object);

            // Write data
            writer.OpenChunk();
            writer.Write(buffer, 0, buffer.Length);
            writer.CloseChunk();

            logger.Verify(x => x.Trace("C: {0}", It.IsAny <string>()), Times.Never);

            writer.Send();

            logger.Verify(x => x.Trace("C: {0}", It.IsAny <string>()), Times.Once);
        }
        private static byte[] GenerateMessageChunk(int messageSize)
        {
            var buffer = Enumerable.Range(0, messageSize).Select(i => i % byte.MaxValue).Select(i => (byte)i).ToArray();
            var stream = new MemoryStream();
            var writer = new ChunkWriter(stream);

            writer.OpenChunk();
            writer.Write(buffer, 0, buffer.Length);
            writer.CloseChunk();

            // Append end of message marker
            writer.OpenChunk();
            writer.CloseChunk();

            writer.Flush();
            writer.Send();

            return(stream.ToArray());
        }
        public async void ShouldCloseTheChunkWithCorrectSize(int chunkSize)
        {
            var buffer = Enumerable.Range(0, chunkSize).Select(i => i % byte.MaxValue).Select(i => (byte)i).ToArray();
            var stream = new MemoryStream();
            var writer = new ChunkWriter(stream);

            // Write data
            writer.OpenChunk();
            writer.Write(buffer, 0, buffer.Length);
            writer.CloseChunk();

            // End Of Message Marker
            writer.OpenChunk();
            writer.CloseChunk();

            // Write To Underlying Stream
            writer.Send();

            var constructed = await ConstructMessage(stream.ToArray());

            constructed.Should().HaveCount(chunkSize);
            constructed.Should().Equal(buffer);
        }