public void GetReadConnectionSourceAsync_should_use_a_writable_server_selector_to_select_the_server_from_the_cluster()
        {
            var subject = new WritableServerBinding(_cluster);

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

            _cluster.Received().SelectServerAsync(Arg.Any<WritableServerSelector>(), Timeout.InfiniteTimeSpan, CancellationToken.None);
        }
 private TResult ExecuteEndTransactionOnPrimary <TResult>(IReadOperation <TResult> operation, CancellationToken cancellationToken)
 {
     using (var sessionHandle = new NonDisposingCoreSessionHandle(this))
         using (var binding = new WritableServerBinding(_cluster, sessionHandle))
         {
             return(operation.Execute(binding, cancellationToken));
         }
 }
        public void GetWriteConnectionSourceAsync_should_throw_if_disposed()
        {
            var subject = new WritableServerBinding(_cluster);
            subject.Dispose();

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

            act.ShouldThrow<ObjectDisposedException>();
        }
        public void Dispose_should_call_dispose_on_owned_resources()
        {
            var mockSession = new Mock <ICoreSessionHandle>();
            var subject     = new WritableServerBinding(_mockCluster.Object, mockSession.Object);

            subject.Dispose();

            _mockCluster.Verify(c => c.Dispose(), Times.Never);
            mockSession.Verify(m => m.Dispose(), Times.Once);
        }
        public void Session_should_return_expected_result()
        {
            var cluster = new Mock <ICluster>().Object;
            var session = new Mock <ICoreSessionHandle>().Object;
            var subject = new WritableServerBinding(cluster, session);

            var result = subject.Session;

            result.Should().BeSameAs(session);
        }
        public void GetWriteConnectionSourceAsync_should_throw_if_disposed()
        {
            var subject = new WritableServerBinding(_cluster);

            subject.Dispose();

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

            act.ShouldThrow <ObjectDisposedException>();
        }
Exemple #7
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_writable_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject = new WritableServerBinding(_cluster);

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

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

                _cluster.Received().SelectServer(Arg.Any <WritableServerSelector>(), CancellationToken.None);
            }
        }
        public void GetReadChannelSource_should_throw_if_disposed(
            [Values(false, true)]
            bool async)
        {
            var subject = new WritableServerBinding(_mockCluster.Object);
            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_writable_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject = new WritableServerBinding(_cluster);

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

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

                _cluster.Received().SelectServer(Arg.Any<WritableServerSelector>(), CancellationToken.None);
            }
        }
Exemple #11
0
        public void GetWriteChannelSource_with_mayUseSecondary_should_pass_mayUseSecondary_to_server_selector(
            [Values(false, true)]
            bool async)
        {
            var subject        = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());
            var selectedServer = new Mock <IServer>().Object;

            var clusterId = new ClusterId();
            var endPoint  = new DnsEndPoint("localhost", 27017);

#pragma warning disable CS0618 // Type or member is obsolete
            var initialClusterDescription = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.Automatic,
                ClusterType.Unknown,
                new[] { new ServerDescription(new ServerId(clusterId, endPoint), endPoint) });
#pragma warning restore CS0618 // Type or member is obsolete
            var finalClusterDescription = initialClusterDescription.WithType(ClusterType.Standalone);
            _mockCluster.SetupSequence(c => c.Description).Returns(initialClusterDescription).Returns(finalClusterDescription);

            var mockMayUseSecondary = new Mock <IMayUseSecondaryCriteria>();
            mockMayUseSecondary.SetupGet(x => x.ReadPreference).Returns(ReadPreference.SecondaryPreferred);
            mockMayUseSecondary.Setup(x => x.CanUseSecondary(It.IsAny <ServerDescription>())).Returns(true);
            var mayUseSecondary = mockMayUseSecondary.Object;

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

                subject.GetWriteChannelSourceAsync(mayUseSecondary, CancellationToken.None).GetAwaiter().GetResult();

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

                subject.GetWriteChannelSource(mayUseSecondary, CancellationToken.None);

                _mockCluster.Verify(c => c.SelectServer(It.Is <WritableServerSelector>(s => s.MayUseSecondary == mayUseSecondary), CancellationToken.None), Times.Once);
            }
        }
        public void GetReadChannelSource_should_throw_if_disposed(
            [Values(false, true)]
            bool async)
        {
            var subject = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());

            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_writable_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject        = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());
            var selectedServer = new Mock <IServer>().Object;

            var clusterId = new ClusterId();
            var endPoint  = new DnsEndPoint("localhost", 27017);

#pragma warning disable CS0618 // Type or member is obsolete
            var initialClusterDescription = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.Automatic,
                ClusterType.Unknown,
                new[] { new ServerDescription(new ServerId(clusterId, endPoint), endPoint) });
#pragma warning restore CS0618 // Type or member is obsolete
            var finalClusterDescription = initialClusterDescription.WithType(ClusterType.Standalone);
            _mockCluster.SetupSequence(c => c.Description).Returns(initialClusterDescription).Returns(finalClusterDescription);


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

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

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

                subject.GetReadChannelSource(CancellationToken.None);

                _mockCluster.Verify(c => c.SelectServer(It.IsAny <WritableServerSelector>(), CancellationToken.None), Times.Once);
            }
        }
        public void GetWriteChannelSourceAsync_should_use_a_writable_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject        = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());
            var selectedServer = new Mock <IServer>().Object;

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

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

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

                subject.GetWriteChannelSource(CancellationToken.None);

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

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

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

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

                subject.GetReadChannelSource(CancellationToken.None);

                _mockCluster.Verify(c => c.SelectServer(It.IsAny<WritableServerSelector>(), CancellationToken.None), Times.Once);
            }
        }
Exemple #16
0
 private async static Task ClearData(ICluster cluster)
 {
     using (var binding = new WritableServerBinding(cluster))
     {
         var commandOp = new DropDatabaseOperation(_database);
         await commandOp.ExecuteAsync(binding);
     }
 }
        public void ReadPreference_should_be_primary()
        {
            var subject = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());

            subject.ReadPreference.Should().Be(ReadPreference.Primary);
        }
        public static ICluster _cluster(this WritableServerBinding obj)
        {
            var fieldInfo = typeof(WritableServerBinding).GetField("_cluster", BindingFlags.NonPublic | BindingFlags.Instance);

            return((ICluster)fieldInfo.GetValue(obj));
        }
        private async static Task DoWork(ICluster cluster, CancellationToken cancellationToken)
        {
            var rand = new Random();
            using (var binding = new WritableServerBinding(cluster))
            while (!cancellationToken.IsCancellationRequested)
            {
                var i = rand.Next(0, 10000);
                IReadOnlyList<BsonDocument> docs;
                Cursor<BsonDocument> result = null;
                try
                {
                    result = await Query(binding, new BsonDocument("i", i)).ConfigureAwait(false);
                    if (await result.MoveNextAsync().ConfigureAwait(false))
                    {
                        docs = result.Current;
                    }
                    else
                    {
                        docs = null;
                    }
                    //Console.Write(".");
                }
                catch (Exception)
                {
                    Console.Write("+");
                    continue;
                }
                finally
                {
                    if (result != null)
                    {
                        result.Dispose();
                    }
                }

                if (docs == null || docs.Count == 0)
                {
                    try
                    {
                        await Insert(binding, new BsonDocument().Add("i", i)).ConfigureAwait(false);
                        //Console.Write(".");
                    }
                    catch (Exception)
                    {
                        Console.Write("*");
                    }
                }
                else
                {
                    try
                    {
                        var query = new BsonDocument("_id", docs[0]["_id"]);
                        var update = new BsonDocument("$set", new BsonDocument("i", i + 1));
                        await Update(binding, query, update).ConfigureAwait(false);
                        //Console.Write(".");
                    }
                    catch (Exception)
                    {
                        Console.Write("*");
                    }
                }
            }
        }
Exemple #20
0
 private async Task InsertData(ICluster cluster)
 {
     using (var binding = new WritableServerBinding(cluster))
     {
         for (int i = 0; i < 100; i++)
         {
             await Insert(binding, new BsonDocument("i", i));
         }
     }
 }
        public void ReadPreference_should_be_primary()
        {
            var subject = new WritableServerBinding(_cluster);

            subject.ReadPreference.Should().Be(ReadPreference.Primary);
        }
        public void Dispose_should_call_dispose_on_read_binding_and_write_binding()
        {
            var subject = new WritableServerBinding(_mockCluster.Object);

            subject.Dispose();

            _mockCluster.Verify(c => c.Dispose(), Times.Never);
        }
        public void GetWriteChannelSourceAsync_should_use_a_writable_server_selector_to_select_the_server_from_the_cluster()
        {
            var subject = new WritableServerBinding(_cluster);

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

            _cluster.Received().SelectServerAsync(Arg.Any<WritableServerSelector>(), CancellationToken.None);
        }
        // public methods
        /// <inheritdoc/>
        public sealed override void DropDatabase(string name, CancellationToken cancellationToken = default(CancellationToken))
        {
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new DropDatabaseOperation(new DatabaseNamespace(name), messageEncoderSettings)
            {
                WriteConcern = _settings.WriteConcern
            };

            using (var binding = new WritableServerBinding(_cluster))
            {
                _operationExecutor.ExecuteWriteOperation(binding, operation, cancellationToken);
            }
        }
        public void ReadPreference_should_be_primary()
        {
            var subject = new WritableServerBinding(_cluster);

            subject.ReadPreference.Should().Be(ReadPreference.Primary);
        }
        public void Dispose_should_call_dispose_on_read_binding_and_write_binding()
        {
            var subject = new WritableServerBinding(_cluster);

            subject.Dispose();

            _cluster.DidNotReceive().Dispose();
        }
Exemple #27
0
        private async Task DoWork(ICluster cluster)
        {
            var rand = new Random();
            using (var binding = new WritableServerBinding(cluster))
            {
                while (!_cancellationTokenSource.IsCancellationRequested)
                {
                    var i = rand.Next(0, 10000);
                    IReadOnlyList<BsonDocument> docs;
                    using (var cursor = await Query(binding, new BsonDocument("i", i)))
                    {
                        try
                        {
                            if (await cursor.MoveNextAsync(_cancellationTokenSource.Token))
                            {
                                docs = cursor.Current.ToList();
                            }
                            else
                            {
                                docs = null;
                            }
                            //Console.Write(".");
                        }
                        catch
                        {
                            Console.Write("+");
                            continue;
                        }
                    }


                    if (docs == null || docs.Count == 0)
                    {
                        try
                        {
                            await Insert(binding, new BsonDocument().Add("i", i));
                            //Console.Write(".");
                        }
                        catch (Exception)
                        {
                            Console.Write("*");
                        }
                    }
                    else
                    {
                        try
                        {
                            var filter = new BsonDocument("_id", docs[0]["_id"]);
                            var update = new BsonDocument("$set", new BsonDocument("i", i + 1));
                            await Update(binding, filter, update);
                            //Console.Write(".");
                        }
                        catch (Exception)
                        {
                            Console.Write("*");
                        }
                    }
                }
            }
        }
        /// <inheritdoc/>
        public sealed override async Task DropDatabaseAsync(string name, CancellationToken cancellationToken = default(CancellationToken))
        {
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new DropDatabaseOperation(new DatabaseNamespace(name), messageEncoderSettings)
            {
                WriteConcern = _settings.WriteConcern
            };

            using (var binding = new WritableServerBinding(_cluster))
            {
                await _operationExecutor.ExecuteWriteOperationAsync(binding, operation, cancellationToken).ConfigureAwait(false);
            }
        }
Exemple #29
0
 private async Task ClearData(ICluster cluster)
 {
     using (var binding = new WritableServerBinding(cluster))
     {
         var commandOp = new DropDatabaseOperation(_collection.DatabaseNamespace, _messageEncoderSettings);
         await commandOp.ExecuteAsync(binding, CancellationToken.None);
     }
 }