public void GetReadChannelSource_should_use_a_read_preference_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject        = new ReadPreferenceBinding(_mockCluster.Object, ReadPreference.Primary, NoCoreSession.NewHandle());
            var selectedServer = new Mock <IServer>().Object;

            var clusterId = new ClusterId();
            var endPoint  = new DnsEndPoint("localhost", 27017);
            var initialClusterDescription = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.Automatic,
                ClusterType.Unknown,
                new[] { new ServerDescription(new ServerId(clusterId, endPoint), endPoint) });
            var finalClusterDescription = initialClusterDescription.WithType(ClusterType.Standalone);

            _mockCluster.SetupSequence(c => c.Description).Returns(initialClusterDescription).Returns(finalClusterDescription);

            if (async)
            {
                _mockCluster.Setup(c => c.SelectServerAsync(It.IsAny <ReadPreferenceServerSelector>(), CancellationToken.None)).Returns(Task.FromResult(selectedServer));

                subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

                _mockCluster.Verify(c => c.SelectServerAsync(It.IsAny <ReadPreferenceServerSelector>(), CancellationToken.None), Times.Once);
            }
            else
            {
                _mockCluster.Setup(c => c.SelectServer(It.IsAny <ReadPreferenceServerSelector>(), CancellationToken.None)).Returns(selectedServer);

                subject.GetReadChannelSource(CancellationToken.None);

                _mockCluster.Verify(c => c.SelectServer(It.IsAny <ReadPreferenceServerSelector>(), CancellationToken.None), Times.Once);
            }
        }
        public void GetReadChannelSource_should_fork_the_session(
            [Values(false, true)] bool async)
        {
            var mockSession       = new Mock <ICoreSessionHandle>();
            var subject           = new ReadPreferenceBinding(_mockCluster.Object, ReadPreference.Primary, mockSession.Object);
            var cancellationToken = new CancellationTokenSource().Token;

            var selectedServer = new Mock <IServer>().Object;

            _mockCluster.Setup(m => m.SelectServer(It.IsAny <IServerSelector>(), cancellationToken)).Returns(selectedServer);
            _mockCluster.Setup(m => m.SelectServerAsync(It.IsAny <IServerSelector>(), cancellationToken)).Returns(Task.FromResult(selectedServer));
            var forkedSession = new Mock <ICoreSessionHandle>().Object;

            mockSession.Setup(m => m.Fork()).Returns(forkedSession);

            IChannelSourceHandle result;

            if (async)
            {
                result = subject.GetReadChannelSourceAsync(cancellationToken).GetAwaiter().GetResult();
            }
            else
            {
                result = subject.GetReadChannelSource(cancellationToken);
            }

            var handle           = result.Should().BeOfType <ChannelSourceHandle>().Subject;
            var referenceCounted = handle._reference().Should().BeOfType <ReferenceCounted <IChannelSource> >().Subject;
            var source           = referenceCounted.Instance;

            source.Session.Should().BeSameAs(forkedSession);
        }
        public void GetReadChannelSourceAsync_should_use_a_read_preference_server_selector_to_select_the_server_from_the_cluster()
        {
            var subject = new ReadPreferenceBinding(_cluster, ReadPreference.Primary);

            subject.GetReadChannelSourceAsync(CancellationToken.None).Wait();

            _cluster.Received().SelectServerAsync(Arg.Any <ReadPreferenceServerSelector>(), CancellationToken.None);
        }
        public void GetReadChannelSourceAsync_should_use_a_read_preference_server_selector_to_select_the_server_from_the_cluster()
        {
            var subject = new ReadPreferenceBinding(_cluster, ReadPreference.Primary);

            subject.GetReadChannelSourceAsync(CancellationToken.None).Wait();

            _cluster.Received().SelectServerAsync(Arg.Any<ReadPreferenceServerSelector>(), CancellationToken.None);
        }
        public void GetReadChannelSourceAsync_should_throw_if_disposed()
        {
            var subject = new ReadPreferenceBinding(_cluster, ReadPreference.Primary);
            subject.Dispose();

            Action act = () => subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

            act.ShouldThrow<ObjectDisposedException>();
        }
        public void GetReadChannelSourceAsync_should_throw_if_disposed()
        {
            var subject = new ReadPreferenceBinding(_cluster, ReadPreference.Primary);

            subject.Dispose();

            Action act = () => subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

            act.ShouldThrow <ObjectDisposedException>();
        }
        public void GetReadChannelSource_should_throw_if_disposed(
            [Values(false, true)]
            bool async)
        {
            var subject = new ReadPreferenceBinding(_mockCluster.Object, ReadPreference.Primary);
            subject.Dispose();

            Action act;
            if (async)
            {
                act = () => subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                act = () => subject.GetReadChannelSource(CancellationToken.None);
            }

            act.ShouldThrow<ObjectDisposedException>();
        }
        public void GetReadChannelSource_should_use_a_read_preference_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject = new ReadPreferenceBinding(_cluster, ReadPreference.Primary);

            if (async)
            {
                subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

                _cluster.Received().SelectServerAsync(Arg.Any<ReadPreferenceServerSelector>(), CancellationToken.None);
            }
            else
            {
                subject.GetReadChannelSource(CancellationToken.None);

                _cluster.Received().SelectServer(Arg.Any<ReadPreferenceServerSelector>(), CancellationToken.None);
            }
        }
        public void GetReadChannelSource_should_fork_the_session(
            [Values(false, true)] bool async)
        {
            var mockSession       = new Mock <ICoreSessionHandle>();
            var subject           = new ReadPreferenceBinding(_mockCluster.Object, ReadPreference.Primary, mockSession.Object);
            var cancellationToken = new CancellationTokenSource().Token;

            var selectedServer = new Mock <IServer>().Object;

            _mockCluster.Setup(m => m.SelectServer(It.IsAny <IServerSelector>(), cancellationToken)).Returns(selectedServer);
            _mockCluster.Setup(m => m.SelectServerAsync(It.IsAny <IServerSelector>(), cancellationToken)).Returns(Task.FromResult(selectedServer));
            var forkedSession = new Mock <ICoreSessionHandle>().Object;

            mockSession.Setup(m => m.Fork()).Returns(forkedSession);

            var clusterId = new ClusterId();
            var endPoint  = new DnsEndPoint("localhost", 27017);
            var initialClusterDescription = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.Automatic,
                ClusterType.Unknown,
                new[] { new ServerDescription(new ServerId(clusterId, endPoint), endPoint) });
            var finalClusterDescription = initialClusterDescription.WithType(ClusterType.Standalone);

            _mockCluster.SetupSequence(c => c.Description).Returns(initialClusterDescription).Returns(finalClusterDescription);

            IChannelSourceHandle result;

            if (async)
            {
                result = subject.GetReadChannelSourceAsync(cancellationToken).GetAwaiter().GetResult();
            }
            else
            {
                result = subject.GetReadChannelSource(cancellationToken);
            }

            var handle           = result.Should().BeOfType <ChannelSourceHandle>().Subject;
            var referenceCounted = handle._reference().Should().BeOfType <ReferenceCounted <IChannelSource> >().Subject;
            var source           = referenceCounted.Instance;

            source.Session.Should().BeSameAs(forkedSession);
        }
        public void GetReadChannelSource_should_use_a_read_preference_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject = new ReadPreferenceBinding(_cluster, ReadPreference.Primary);

            if (async)
            {
                subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

                _cluster.Received().SelectServerAsync(Arg.Any <ReadPreferenceServerSelector>(), CancellationToken.None);
            }
            else
            {
                subject.GetReadChannelSource(CancellationToken.None);

                _cluster.Received().SelectServer(Arg.Any <ReadPreferenceServerSelector>(), CancellationToken.None);
            }
        }
Example #11
0
        public void GetReadChannelSource_should_throw_if_disposed(
            [Values(false, true)]
            bool async)
        {
            var subject = new ReadPreferenceBinding(_mockCluster.Object, ReadPreference.Primary);

            subject.Dispose();

            Action act;

            if (async)
            {
                act = () => subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                act = () => subject.GetReadChannelSource(CancellationToken.None);
            }

            act.ShouldThrow <ObjectDisposedException>();
        }
Example #12
0
        public void GetReadChannelSource_should_use_a_read_preference_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject        = new ReadPreferenceBinding(_mockCluster.Object, ReadPreference.Primary);
            var selectedServer = new Mock <IServer>().Object;

            if (async)
            {
                _mockCluster.Setup(c => c.SelectServerAsync(It.IsAny <ReadPreferenceServerSelector>(), CancellationToken.None)).Returns(Task.FromResult(selectedServer));

                subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

                _mockCluster.Verify(c => c.SelectServerAsync(It.IsAny <ReadPreferenceServerSelector>(), CancellationToken.None), Times.Once);
            }
            else
            {
                _mockCluster.Setup(c => c.SelectServer(It.IsAny <ReadPreferenceServerSelector>(), CancellationToken.None)).Returns(selectedServer);

                subject.GetReadChannelSource(CancellationToken.None);

                _mockCluster.Verify(c => c.SelectServer(It.IsAny <ReadPreferenceServerSelector>(), CancellationToken.None), Times.Once);
            }
        }
        public void GetReadChannelSource_should_use_a_read_preference_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject = new ReadPreferenceBinding(_mockCluster.Object, ReadPreference.Primary);
            var selectedServer = new Mock<IServer>().Object;

            if (async)
            {
                _mockCluster.Setup(c => c.SelectServerAsync(It.IsAny<ReadPreferenceServerSelector>(), CancellationToken.None)).Returns(Task.FromResult(selectedServer));

                subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

                _mockCluster.Verify(c => c.SelectServerAsync(It.IsAny<ReadPreferenceServerSelector>(), CancellationToken.None), Times.Once);
            }
            else
            {
                _mockCluster.Setup(c => c.SelectServer(It.IsAny<ReadPreferenceServerSelector>(), CancellationToken.None)).Returns(selectedServer);

                subject.GetReadChannelSource(CancellationToken.None);

                _mockCluster.Verify(c => c.SelectServer(It.IsAny<ReadPreferenceServerSelector>(), CancellationToken.None), Times.Once);
            }
        }