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 GetReadConnectionSourceAsync_should_use_a_read_preference_server_selector_to_select_the_server_from_the_cluster()
        {
            var subject = new ReadPreferenceBinding(_cluster, ReadPreference.Primary);

            subject.GetReadConnectionSourceAsync(Timeout.InfiniteTimeSpan, CancellationToken.None).Wait();

            _cluster.Received().SelectServerAsync(Arg.Any <ReadPreferenceServerSelector>(), Timeout.InfiniteTimeSpan, CancellationToken.None);
        }
Beispiel #4
0
        public void Dispose_should_not_call_dispose_on_the_cluster()
        {
            var subject = new ReadPreferenceBinding(_mockCluster.Object, ReadPreference.Primary);

            subject.Dispose();

            _mockCluster.Verify(c => c.Dispose(), Times.Never);
        }
        public void Dispose_should_not_call_dispose_on_the_cluster()
        {
            var subject = new ReadPreferenceBinding(_cluster, ReadPreference.Primary);

            subject.Dispose();

            _cluster.DidNotReceive().Dispose();
        }
        public void GetReadConnectionSourceAsync_should_use_a_read_preference_server_selector_to_select_the_server_from_the_cluster()
        {
            var subject = new ReadPreferenceBinding(_cluster, ReadPreference.Primary);

            subject.GetReadConnectionSourceAsync(Timeout.InfiniteTimeSpan, CancellationToken.None).Wait();

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

            subject.Dispose();

            _cluster.DidNotReceive().Dispose();
        }
        public void Session_should_return_expected_result()
        {
            var session = new Mock <ICoreSessionHandle>().Object;
            var subject = new ReadPreferenceBinding(_mockCluster.Object, ReadPreference.Primary, session);

            var result = subject.Session;

            result.Should().BeSameAs(session);
        }
        public void Dispose_should_call_dispose_on_the_session()
        {
            var mockSession = new Mock <ICoreSessionHandle>();
            var subject     = new ReadPreferenceBinding(_mockCluster.Object, ReadPreference.Primary, mockSession.Object);

            subject.Dispose();

            mockSession.Verify(c => c.Dispose(), Times.Once);
        }
        public void GetReadConnectionSourceAsync_should_throw_if_disposed()
        {
            var subject = new ReadPreferenceBinding(_cluster, ReadPreference.Primary);
            subject.Dispose();

            Action act = () => subject.GetReadConnectionSourceAsync(Timeout.InfiniteTimeSpan, CancellationToken.None).GetAwaiter().GetResult();

            act.ShouldThrow<ObjectDisposedException>();
        }
        public void Execute_should_return_expected_result()
        {
            var subject = new CurrentOpUsingCommandOperation(_adminDatabaseNamespace, _messageEncoderSettings);
            using (var binding = new ReadPreferenceBinding(CoreTestConfiguration.Cluster, ReadPreference.PrimaryPreferred))
            {
                var result = subject.Execute(binding, CancellationToken.None);

                result.Contains("inprog");
            }
        }
        public void GetReadConnectionSourceAsync_should_throw_if_disposed()
        {
            var subject = new ReadPreferenceBinding(_cluster, ReadPreference.Primary);

            subject.Dispose();

            Action act = () => subject.GetReadConnectionSourceAsync(Timeout.InfiniteTimeSpan, CancellationToken.None).GetAwaiter().GetResult();

            act.ShouldThrow <ObjectDisposedException>();
        }
        public void Execute_should_return_expected_result()
        {
            RequireServer.Where(clusterTypes: ClusterTypes.StandaloneOrReplicaSet, storageEngines: "mmapv1");
            var subject = new CurrentOpUsingFindOperation(_adminDatabaseNamespace, _messageEncoderSettings);
            using (var binding = new ReadPreferenceBinding(CoreTestConfiguration.Cluster, ReadPreference.PrimaryPreferred))
            {
                var result = subject.Execute(binding, CancellationToken.None);

                result.Contains("inprog");
            }
        }
Beispiel #14
0
        public void constructor_with_read_and_write_bindings_should_throw_if_bindings_have_different_sessions()
        {
            var cluster      = new Mock <ICluster>().Object;
            var session1     = new Mock <ICoreSessionHandle>().Object;
            var session2     = new Mock <ICoreSessionHandle>().Object;
            var readBinding  = new ReadPreferenceBinding(cluster, ReadPreference.Secondary, session1);
            var writeBinding = new WritableServerBinding(cluster, session2);

            var exception = Record.Exception(() => new SplitReadWriteBinding(readBinding, writeBinding));

            exception.Should().BeOfType <ArgumentException>();
        }
        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);
            }
        }
        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>();
        }
Beispiel #19
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>();
        }
Beispiel #20
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);
            }
        }
        /// <inheritdoc/>
        public sealed override async Task<IAsyncCursor<BsonDocument>> ListDatabasesAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new ListDatabasesOperation(messageEncoderSettings);

            using (var binding = new ReadPreferenceBinding(_cluster, _settings.ReadPreference))
            {
                return await _operationExecutor.ExecuteReadOperationAsync(binding, operation, cancellationToken).ConfigureAwait(false);
            }
        }
        /// <inheritdoc/>
        public sealed override IAsyncCursor<BsonDocument> ListDatabases(CancellationToken cancellationToken = default(CancellationToken))
        {
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new ListDatabasesOperation(messageEncoderSettings);

            using (var binding = new ReadPreferenceBinding(_cluster, _settings.ReadPreference))
            {
                return _operationExecutor.ExecuteReadOperation(binding, operation, cancellationToken);
            }
        }
        /// <summary>
        /// Gets the database names.
        /// </summary>
        /// <param name="timeout">The timeout.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A list of the database on the server.</returns>
        public async Task<IReadOnlyList<string>> GetDatabaseNamesAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new ListDatabaseNamesOperation(messageEncoderSettings);

            using(var binding = new ReadPreferenceBinding(_cluster, _settings.ReadPreference))
            {
                return await _operationExecutor.ExecuteReadOperationAsync(binding, operation, timeout ?? _settings.OperationTimeout, cancellationToken);
            }
        }
        public void Execute_should_return_expected_result()
        {
            RequireServer.Where(minimumVersion: "3.1.2");
            var subject = new CurrentOpOperation(_adminDatabaseNamespace, _messageEncoderSettings);
            using (var binding = new ReadPreferenceBinding(CoreTestConfiguration.Cluster, ReadPreference.PrimaryPreferred))
            {
                var result = subject.Execute(binding);

                result.Contains("inprog");
            }
        }
        public static ICluster _cluster(this ReadPreferenceBinding obj)
        {
            var fieldInfo = typeof(ReadPreferenceBinding).GetField("_cluster", BindingFlags.NonPublic | BindingFlags.Instance);

            return((ICluster)fieldInfo.GetValue(obj));
        }
        public void Dispose_should_not_call_dispose_on_the_cluster()
        {
            var subject = new ReadPreferenceBinding(_mockCluster.Object, ReadPreference.Primary);

            subject.Dispose();

            _mockCluster.Verify(c => c.Dispose(), Times.Never);
        }