public void BaseSource_get_should_return_expected_result()
        {
            var baseSource = Substitute.For<IBsonChunkSource>();
            var subject = new OutputBufferChunkSource(baseSource);

            var result = subject.BaseSource;

            result.Should().BeSameAs(baseSource);
        }
        public void BaseSource_get_should_return_expected_result()
        {
            var mockBaseSource = new Mock<IBsonChunkSource>();
            var subject = new OutputBufferChunkSource(mockBaseSource.Object);

            var result = subject.BaseSource;

            result.Should().BeSameAs(mockBaseSource.Object);
        }
        public void BaseSource_get_should_throw_when_subject_is_disposed()
        {
            var baseSource = Substitute.For<IBsonChunkSource>();
            var subject = new OutputBufferChunkSource(baseSource);
            subject.Dispose();

            Action action = () => { var _ = subject.BaseSource; };

            action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("OutputBufferChunkSource");
        }
        public void constructor_should_initialize_subject()
        {
            var baseSource = Substitute.For<IBsonChunkSource>();
            var initialUnpooledChunkSize = 2;
            var minChunkSize = 4;
            var maxChunkSize = 8;

            var subject = new OutputBufferChunkSource(baseSource, initialUnpooledChunkSize, minChunkSize, maxChunkSize);

            var reflector = new Reflector(subject);
            subject.BaseSource.Should().BeSameAs(baseSource);
            subject.InitialUnpooledChunkSize.Should().Be(initialUnpooledChunkSize);
            subject.MaxChunkSize.Should().Be(maxChunkSize);
            subject.MinChunkSize.Should().Be(minChunkSize);
            reflector._disposed.Should().BeFalse();
        }
 public Reflector(OutputBufferChunkSource instance)
 {
     _instance = instance;
 }
        public void MinChunkSize_get_should_return_expected_result()
        {
            var baseSource = Substitute.For<IBsonChunkSource>();
            var minChunkSize = Reflector.DefaultMinChunkSize * 2;
            var subject = new OutputBufferChunkSource(baseSource, minChunkSize: minChunkSize);

            var result = subject.MinChunkSize;

            result.Should().Be(minChunkSize);
        }
        public void InitialUnpooledChunkSize_get_should_return_expected_result()
        {
            var baseSource = Substitute.For<IBsonChunkSource>();
            var initialUnpooledChunkSize = Reflector.DefaultInitialUnpooledChunkSize * 2;
            var subject = new OutputBufferChunkSource(baseSource, initialUnpooledChunkSize: initialUnpooledChunkSize);

            var result = subject.InitialUnpooledChunkSize;

            result.Should().Be(initialUnpooledChunkSize);
        }
        public void GetChunk_should_throw_when_requestedSize_is_less_than_or_equal_to_zero(
            [Values(-1, 0)]
            int requestedSize)
        {
            var baseSource = Substitute.For<IBsonChunkSource>();
            var subject = new OutputBufferChunkSource(baseSource);

            Action action = () => subject.GetChunk(requestedSize);

            action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("requestedSize");
        }
        public void Dispose_should_dispose_subject()
        {
            var mockBaseSource = new Mock<IBsonChunkSource>();
            var subject = new OutputBufferChunkSource(mockBaseSource.Object);

            subject.Dispose();

            var reflector = new Reflector(subject);
            reflector._disposed.Should().BeTrue();

        }
        public async Task SendMessagesAsync(IEnumerable<RequestMessage> messages, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(messages, nameof(messages));
            ThrowIfDisposedOrNotOpen();

            var messagesToSend = messages.ToList();
            var requestIds = messagesToSend.Select(x => x.RequestId).ToList();
            try
            {
                if (_sendingMessagesEventHandler != null)
                {
                    _sendingMessagesEventHandler(new ConnectionSendingMessagesEvent(_connectionId, requestIds, EventContext.OperationId));
                }

                cancellationToken.ThrowIfCancellationRequested();

                var stopwatch = Stopwatch.StartNew();
                var outputBufferChunkSource = new OutputBufferChunkSource(BsonChunkPool.Default);
                using (var buffer = new MultiChunkBuffer(outputBufferChunkSource))
                {
                    using (var stream = new ByteBufferStream(buffer, ownsBuffer: false))
                    {
                        var encoderFactory = new BinaryMessageEncoderFactory(stream, messageEncoderSettings);
                        foreach (var message in messagesToSend)
                        {
                            if (message.ShouldBeSent == null || message.ShouldBeSent())
                            {
                                var encoder = message.GetEncoder(encoderFactory);
                                encoder.WriteMessage(message);
                                message.WasSent = true;
                            }

                            // Encoding messages includes serializing the
                            // documents, so encoding message could be expensive
                            // and worthy of us honoring cancellation here.
                            cancellationToken.ThrowIfCancellationRequested();
                        }
                        buffer.Length = (int)stream.Length;
                        buffer.MakeReadOnly();
                    }

                    var serializationDuration = stopwatch.Elapsed;

                    if (_commandEventHelper.ShouldCallBeforeSending)
                    {
                        _commandEventHelper.BeforeSending(
                            messages,
                            _connectionId,
                            buffer,
                            messageEncoderSettings,
                            stopwatch);
                    }

                    var stopwatchCheckPoint = stopwatch.Elapsed;

                    await SendBufferAsync(buffer, cancellationToken).ConfigureAwait(false);

                    var networkDuration = stopwatch.Elapsed - stopwatchCheckPoint;

                    if (_commandEventHelper.ShouldCallAfterSending)
                    {
                        _commandEventHelper.AfterSending(messages, _connectionId);
                    }

                    if (_sentMessagesEventHandler != null)
                    {
                        _sentMessagesEventHandler(new ConnectionSentMessagesEvent(_connectionId, requestIds, buffer.Length, networkDuration, serializationDuration, EventContext.OperationId));
                    }
                }
            }
            catch (Exception ex)
            {
                if (_commandEventHelper.ShouldCallErrorSending)
                {
                    _commandEventHelper.ErrorSending(messages, _connectionId, ex);
                }

                if (_failedSendingMessagesEvent != null)
                {
                    _failedSendingMessagesEvent(new ConnectionSendingMessagesFailedEvent(_connectionId, requestIds, ex, EventContext.OperationId));
                }

                throw;
            }
        }
        public void Dispose_can_be_called_more_than_once()
        {
            var baseSource = Substitute.For<IBsonChunkSource>();
            var subject = new OutputBufferChunkSource(baseSource);

            subject.Dispose();
            subject.Dispose();

        }
        public void MinChunkSize_get_should_return_expected_result()
        {
            var mockBaseSource = new Mock<IBsonChunkSource>();
            var minChunkSize = Reflector.DefaultMinChunkSize * 2;
            var subject = new OutputBufferChunkSource(mockBaseSource.Object, minChunkSize: minChunkSize);

            var result = subject.MinChunkSize;

            result.Should().Be(minChunkSize);
        }
        public void InitialUnpooledChunkSize_get_should_return_expected_result()
        {
            var mockBaseSource = new Mock<IBsonChunkSource>();
            var initialUnpooledChunkSize = Reflector.DefaultInitialUnpooledChunkSize * 2;
            var subject = new OutputBufferChunkSource(mockBaseSource.Object, initialUnpooledChunkSize: initialUnpooledChunkSize);

            var result = subject.InitialUnpooledChunkSize;

            result.Should().Be(initialUnpooledChunkSize);
        }
        public void GetChunk_should_throw_when_subject_is_disposed()
        {
            var mockBaseSource = new Mock<IBsonChunkSource>();
            var subject = new OutputBufferChunkSource(mockBaseSource.Object);
            subject.Dispose();

            Action action = () => subject.GetChunk(1);
 
            action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("OutputBufferChunkSource");
       }
        public void GetChunk_should_return_expected_result_when_initialUnpooledChunkSize_is_zero(int numberOfCalls, int[] expectedRequestSizes)
        {
            var mockBaseSource = new Mock<IBsonChunkSource>();
            mockBaseSource.Setup(s => s.GetChunk(It.IsAny<int>())).Returns((int requestedSize) => new ByteArrayChunk(requestedSize));
            var subject = new OutputBufferChunkSource(mockBaseSource.Object, initialUnpooledChunkSize: 0, minChunkSize: 4, maxChunkSize: 32);

            for (var n = 0; n < numberOfCalls; n++)
            {
                var result = subject.GetChunk(1);
                result.Bytes.Count.Should().Be(expectedRequestSizes[n]);
            }

            mockBaseSource.Verify(s => s.GetChunk(It.IsAny<int>()), Times.Exactly(numberOfCalls));
            foreach (var expectedRequestSize in expectedRequestSizes)
            {
                var requiredNumberOfCalls = expectedRequestSizes.Count(s => s == expectedRequestSize);
                mockBaseSource.Verify(s => s.GetChunk(expectedRequestSize), Times.Exactly(requiredNumberOfCalls));
            }
        }
        public void Dispose_should_dispose_subject()
        {
            var baseSource = Substitute.For<IBsonChunkSource>();
            var subject = new OutputBufferChunkSource(baseSource);

            subject.Dispose();

            var reflector = new Reflector(subject);
            reflector._disposed.Should().BeTrue();

        }
        public void GetChunk_should_return_expected_result_when_initialUnpooledChunkSize_is_zero(int numberOfCalls, int[] expectedRequestSizes)
        {
            var baseSource = Substitute.For<IBsonChunkSource>();
            baseSource.GetChunk(Arg.Any<int>()).Returns(x =>
            {
                var requestedSize = (int)x[0];
                return new ByteArrayChunk(requestedSize);
            });
            var subject = new OutputBufferChunkSource(baseSource, initialUnpooledChunkSize: 0, minChunkSize: 4, maxChunkSize: 32);

            for (var n = 0; n < numberOfCalls; n++)
            {
                var result = subject.GetChunk(1);
                result.Bytes.Count.Should().Be(expectedRequestSizes[n]);
            }

            baseSource.Received(numberOfCalls).GetChunk(Arg.Any<int>());
            foreach (var expectedRequestSize in expectedRequestSizes)
            {
                var requiredNumberOfCalls = expectedRequestSizes.Count(s => s == expectedRequestSize);
                baseSource.Received(requiredNumberOfCalls).GetChunk(expectedRequestSize);
            }
        }
            public IByteBuffer EncodeMessages(CancellationToken cancellationToken)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var serializationStopwatch = Stopwatch.StartNew();
                var outputBufferChunkSource = new OutputBufferChunkSource(BsonChunkPool.Default);
                var buffer = new MultiChunkBuffer(outputBufferChunkSource);
                using (var stream = new ByteBufferStream(buffer, ownsBuffer: false))
                {
                    var encoderFactory = new BinaryMessageEncoderFactory(stream, _messageEncoderSettings);
                    foreach (var message in _messages)
                    {
                        if (message.ShouldBeSent == null || message.ShouldBeSent())
                        {
                            var encoder = message.GetEncoder(encoderFactory);
                            encoder.WriteMessage(message);
                            message.WasSent = true;
                        }

                        // Encoding messages includes serializing the
                        // documents, so encoding message could be expensive
                        // and worthy of us honoring cancellation here.
                        cancellationToken.ThrowIfCancellationRequested();
                    }
                    buffer.Length = (int)stream.Length;
                    buffer.MakeReadOnly();
                }
                serializationStopwatch.Stop();
                _serializationDuration = serializationStopwatch.Elapsed;

                return buffer;
            }
        public void Dispose_can_be_called_more_than_once()
        {
            var mockBaseSource = new Mock<IBsonChunkSource>();
            var subject = new OutputBufferChunkSource(mockBaseSource.Object);

            subject.Dispose();
            subject.Dispose();

        }