public void GetChunk_should_throw_when_subject_is_disposed()
        {
            var baseSource = Substitute.For<IBsonChunkSource>();
            var subject = new InputBufferChunkSource(baseSource);
            subject.Dispose();

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

            action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("InputBufferChunkSource");
        }
        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 InputBufferChunkSource(baseSource);

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

            action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("requestedSize");
        }
        public void GetChunk_should_round_requestedSize_to_power_of_2_without_wasting_too_much_space(int requestedSize, int roundedSize)
        {
            var baseSource = Substitute.For<IBsonChunkSource>();
            var subject = new InputBufferChunkSource(baseSource, maxUnpooledChunkSize: 0, minChunkSize: 4, maxChunkSize: 16);
            baseSource.GetChunk(Arg.Any<int>()).Returns(Substitute.For<IBsonChunk>());

            subject.GetChunk(requestedSize);

            baseSource.Received(1).GetChunk(roundedSize);
        }
        public void GetChunk_should_return_unpooled_chunk_when_requestedSize_is_less_than_or_equal_to_maxUnpooledChunkSize(
            [Values(1, 2, 4 * 1024 - 1, 4 * 1024)]
            int requestedSize)
        {
            var baseSource = Substitute.For<IBsonChunkSource>();
            var subject = new InputBufferChunkSource(baseSource);

            var result = subject.GetChunk(requestedSize);

            result.Should().BeOfType<ByteArrayChunk>();
            result.Bytes.Count.Should().Be(requestedSize);
            baseSource.DidNotReceive().GetChunk(Arg.Any<int>());
        }
        public void GetChunk_should_round_requestedSize_to_power_of_2_without_wasting_too_much_space(int requestedSize, int roundedSize)
        {
            var mockBaseSource = new Mock<IBsonChunkSource>();
            var subject = new InputBufferChunkSource(mockBaseSource.Object, maxUnpooledChunkSize: 0, minChunkSize: 4, maxChunkSize: 16);
            mockBaseSource.Setup(s => s.GetChunk(It.IsAny<int>())).Returns(() => new Mock<IBsonChunk>().Object);

            subject.GetChunk(requestedSize);

            mockBaseSource.Verify(s => s.GetChunk(roundedSize), Times.Once);
        }
        public void GetChunk_should_return_unpooled_chunk_when_requestedSize_is_less_than_or_equal_to_maxUnpooledChunkSize(
            [Values(1, 2, 4 * 1024 - 1, 4 * 1024)]
            int requestedSize)
        {
            var mockBaseSource = new Mock<IBsonChunkSource>();
            var subject = new InputBufferChunkSource(mockBaseSource.Object);

            var result = subject.GetChunk(requestedSize);

            result.Should().BeOfType<ByteArrayChunk>();
            result.Bytes.Count.Should().Be(requestedSize);
            mockBaseSource.Verify(s => s.GetChunk(It.IsAny<int>()), Times.Never);
        }