Esempio n. 1
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());
        }
        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 ShouldResetCapacityWhenAboveMaxBufferSizeAfterEachSendAsync()
        {
            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();
            await writer.SendAsync();

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

            logger.Verify(l => l.Info(It.IsRegex("^Shrinking write buffers to the"), It.IsAny <object[]>()), Times.Exactly(2));
        }
        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);
        }
        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);
        }
Esempio n. 6
0
        public async void ShouldNotResetCapacityWhenBelowMaxBufferSizeAsync(int defaultBufferSize, int maxBufferSize, int messageSize)
        {
            var buffer = new byte[messageSize];
            var stream = new MemoryStream();
            var logger = new Mock <IDriverLogger>();
            var writer = new ChunkWriter(stream, defaultBufferSize, maxBufferSize, logger.Object);

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

            await writer.SendAsync();

            logger.Verify(l => l.Info(It.IsRegex("^Shrinking write buffers to the"), It.IsAny <object[]>()), Times.Never);
        }
        public async void ShouldWriteToUnderlyingStreamUponSendAsync()
        {
            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);

            await writer.SendAsync();

            stream.Length.Should().Be(buffer.Length + 2);
        }
        public async void ShouldLogDataOnSendAsync()
        {
            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);

            await writer.SendAsync();

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