コード例 #1
0
        public void Wrapped_should_return_the_document_passed_in_the_constructor()
        {
            var doc     = new BsonDocument();
            var subject = new BuildInfoResult(doc);

            subject.Wrapped.Should().BeSameAs(doc);
        }
コード例 #2
0
        public async Task <ConnectionDescription> InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            var isMasterCommand  = CreateInitialIsMasterCommand(connection.Settings.Authenticators);
            var isMasterProtocol = IsMasterHelper.CreateProtocol(isMasterCommand);
            var isMasterResult   = await IsMasterHelper.GetResultAsync(connection, isMasterProtocol, cancellationToken).ConfigureAwait(false);

            var buildInfoProtocol = CreateBuildInfoProtocol();
            var buildInfoResult   = new BuildInfoResult(await buildInfoProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false));

            var description = new ConnectionDescription(connection.ConnectionId, isMasterResult, buildInfoResult);

            await AuthenticationHelper.AuthenticateAsync(connection, description, cancellationToken).ConfigureAwait(false);

            try
            {
                var getLastErrorProtocol = CreateGetLastErrorProtocol();
                var getLastErrorResult   = await getLastErrorProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false);

                description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
            }
            catch
            {
                // if we couldn't get the server's connection id, so be it.
            }

            return(description);
        }
コード例 #3
0
        public ConnectionDescription InitializeConnection(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));

            var isMasterProtocol = CreateIsMasterProtocol();
            var isMasterResult   = new IsMasterResult(isMasterProtocol.Execute(connection, cancellationToken));

            var buildInfoProtocol = CreateBuildInfoProtocol();
            var buildInfoResult   = new BuildInfoResult(buildInfoProtocol.Execute(connection, cancellationToken));

            var description = new ConnectionDescription(connection.ConnectionId, isMasterResult, buildInfoResult);

            AuthenticationHelper.Authenticate(connection, description, cancellationToken);

            try
            {
                var getLastErrorProtocol = CreateGetLastErrorProtocol();
                var getLastErrorResult   = getLastErrorProtocol.Execute(connection, cancellationToken);

                description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
            }
            catch
            {
                // if we couldn't get the server's connection id, so be it.
            }

            return(description);
        }
コード例 #4
0
        public void Equals_should_be_false_when_both_have_different_results()
        {
            var subject1 = new BuildInfoResult(new BsonDocument("x", 1));
            var subject2 = new BuildInfoResult(new BsonDocument("x", 2));

            subject1.Equals(subject2).Should().BeFalse();
        }
コード例 #5
0
        public void Equals_should_be_true_when_both_have_the_same_result()
        {
            var subject1 = new BuildInfoResult(new BsonDocument("x", 1));
            var subject2 = new BuildInfoResult(new BsonDocument("x", 1));

            subject1.Equals(subject2).Should().BeTrue();
        }
コード例 #6
0
        public BinaryConnectionTests()
        {
            _capturedEvents    = new EventCapturer();
            _mockStreamFactory = new Mock <IStreamFactory>();

            _endPoint = new DnsEndPoint("localhost", 27017);
            var serverId       = new ServerId(new ClusterId(), _endPoint);
            var connectionId   = new ConnectionId(serverId);
            var isMasterResult = new IsMasterResult(new BsonDocument {
                { "ok", 1 }, { "maxMessageSizeBytes", 48000000 }
            });
            var buildInfoResult = new BuildInfoResult(new BsonDocument {
                { "ok", 1 }, { "version", "2.6.3" }
            });
            var connectionDescription = new ConnectionDescription(connectionId, isMasterResult, buildInfoResult);

            _mockConnectionInitializer = new Mock <IConnectionInitializer>();
            _mockConnectionInitializer
            .Setup(i => i.InitializeConnection(It.IsAny <IConnection>(), CancellationToken.None))
            .Returns(connectionDescription);
            _mockConnectionInitializer
            .Setup(i => i.InitializeConnectionAsync(It.IsAny <IConnection>(), CancellationToken.None))
            .ReturnsAsync(connectionDescription);

            _subject = new BinaryConnection(
                serverId: serverId,
                endPoint: _endPoint,
                settings: new ConnectionSettings(),
                streamFactory: _mockStreamFactory.Object,
                connectionInitializer: _mockConnectionInitializer.Object,
                eventSubscriber: _capturedEvents);
        }
コード例 #7
0
        public void ServerVersion_should_get_the_semantic_version()
        {
            var doc = new BsonDocument
            {
                { "version", "2.6.3" }
            };
            var subject = new BuildInfoResult(doc);

            subject.ServerVersion.Should().Be(new SemanticVersion(2, 6, 3));
        }
コード例 #8
0
        // constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="ConnectionDescription"/> class.
        /// </summary>
        /// <param name="connectionId">The connection identifier.</param>
        /// <param name="isMasterResult">The issMaster result.</param>
        /// <param name="buildInfoResult">The buildInfo result.</param>
        public ConnectionDescription(ConnectionId connectionId, IsMasterResult isMasterResult, BuildInfoResult buildInfoResult)
        {
            _connectionId    = Ensure.IsNotNull(connectionId, nameof(connectionId));
            _buildInfoResult = Ensure.IsNotNull(buildInfoResult, nameof(buildInfoResult));
            _isMasterResult  = Ensure.IsNotNull(isMasterResult, nameof(isMasterResult));

            _maxBatchCount   = isMasterResult.MaxBatchCount;
            _maxDocumentSize = isMasterResult.MaxDocumentSize;
            _maxMessageSize  = isMasterResult.MaxMessageSize;
            _serverVersion   = buildInfoResult.ServerVersion;
        }
コード例 #9
0
        public async Task <ConnectionDescription> InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));

            var isMasterCommand  = new BsonDocument("isMaster", 1);
            var isMasterProtocol = new CommandWireProtocol <BsonDocument>(
                DatabaseNamespace.Admin,
                isMasterCommand,
                true,
                BsonDocumentSerializer.Instance,
                null);
            var isMasterResult = new IsMasterResult(await isMasterProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false));

            var buildInfoCommand  = new BsonDocument("buildInfo", 1);
            var buildInfoProtocol = new CommandWireProtocol <BsonDocument>(
                DatabaseNamespace.Admin,
                buildInfoCommand,
                true,
                BsonDocumentSerializer.Instance,
                null);
            var buildInfoResult = new BuildInfoResult(await buildInfoProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false));

            var connectionId = connection.ConnectionId;
            var description  = new ConnectionDescription(connectionId, isMasterResult, buildInfoResult);

            await AuthenticationHelper.AuthenticateAsync(connection, description, cancellationToken).ConfigureAwait(false);

            try
            {
                var getLastErrorCommand  = new BsonDocument("getLastError", 1);
                var getLastErrorProtocol = new CommandWireProtocol <BsonDocument>(
                    DatabaseNamespace.Admin,
                    getLastErrorCommand,
                    true,
                    BsonDocumentSerializer.Instance,
                    null);
                var getLastErrorResult = await getLastErrorProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false);

                BsonValue connectionIdBsonValue;
                if (getLastErrorResult.TryGetValue("connectionId", out connectionIdBsonValue))
                {
                    connectionId = connectionId.WithServerValue(connectionIdBsonValue.ToInt32());
                    description  = description.WithConnectionId(connectionId);
                }
            }
            catch
            {
                // if we couldn't get the server's connection id, so be it.
            }

            return(description);
        }
コード例 #10
0
        // constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="ConnectionDescription"/> class.
        /// </summary>
        /// <param name="connectionId">The connection identifier.</param>
        /// <param name="helloResult">The hello result.</param>
        /// <param name="buildInfoResult">The buildInfo result.</param>
        public ConnectionDescription(ConnectionId connectionId, HelloResult helloResult, BuildInfoResult buildInfoResult)
        {
            _connectionId    = Ensure.IsNotNull(connectionId, nameof(connectionId));
            _buildInfoResult = Ensure.IsNotNull(buildInfoResult, nameof(buildInfoResult));
            _helloResult     = Ensure.IsNotNull(helloResult, nameof(helloResult));

            _compressors     = Ensure.IsNotNull(_helloResult.Compressions, "compressions");
            _maxBatchCount   = helloResult.MaxBatchCount;
            _maxDocumentSize = helloResult.MaxDocumentSize;
            _maxMessageSize  = helloResult.MaxMessageSize;
            _serviceId       = helloResult.ServiceId;
            _serverVersion   = buildInfoResult.ServerVersion;
        }
コード例 #11
0
        public void WithConnectionId_should_return_new_instance_even_when_only_the_serverValue_differs()
        {
            var clusterId       = new ClusterId();
            var serverId        = new ServerId(clusterId, new DnsEndPoint("localhost", 1));
            var connectionId1   = new ConnectionId(serverId, 1);
            var connectionId2   = new ConnectionId(serverId, 1).WithServerValue(2);
            var isMasterResult  = new IsMasterResult(new BsonDocument());
            var buildInfoResult = new BuildInfoResult(new BsonDocument("version", "2.6.0"));
            var subject         = new ConnectionDescription(connectionId1, isMasterResult, buildInfoResult);

            var result = subject.WithConnectionId(connectionId2);

            result.Should().NotBeSameAs(subject);
            result.ConnectionId.Should().BeSameAs(connectionId2);
        }
コード例 #12
0
        public async Task <ConnectionDescription> SendHelloAsync(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            var authenticators = GetAuthenticators(connection.Settings);
            var helloCommand   = CreateInitialHelloCommand(authenticators, connection.Settings.LoadBalanced);
            var helloProtocol  = HelloHelper.CreateProtocol(helloCommand, _serverApi);
            var helloResult    = await HelloHelper.GetResultAsync(connection, helloProtocol, cancellationToken).ConfigureAwait(false);

            if (connection.Settings.LoadBalanced && !helloResult.ServiceId.HasValue)
            {
                throw new InvalidOperationException("Driver attempted to initialize in load balancing mode, but the server does not support this mode.");
            }

            var buildInfoProtocol = CreateBuildInfoProtocol(_serverApi);
            var buildInfoResult   = new BuildInfoResult(await buildInfoProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false));

            return(new ConnectionDescription(connection.ConnectionId, helloResult, buildInfoResult));
        }
コード例 #13
0
        public void Equals_should_return_correct_results()
        {
            var connectionId1    = new ConnectionId(new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27018)), 10);
            var connectionId2    = new ConnectionId(new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27018)), 10);
            var isMasterResult1  = new IsMasterResult(new BsonDocument("x", 1));
            var isMasterResult2  = new IsMasterResult(new BsonDocument("x", 2));
            var buildInfoResult1 = new BuildInfoResult(new BsonDocument("version", "2.6.3"));
            var buildInfoResult2 = new BuildInfoResult(new BsonDocument("version", "2.4.10"));

            var subject1 = new ConnectionDescription(connectionId1, isMasterResult1, buildInfoResult1);
            var subject2 = new ConnectionDescription(connectionId1, isMasterResult1, buildInfoResult1);
            var subject3 = new ConnectionDescription(connectionId1, isMasterResult1, buildInfoResult2);
            var subject4 = new ConnectionDescription(connectionId1, isMasterResult2, buildInfoResult1);
            var subject5 = new ConnectionDescription(connectionId2, isMasterResult1, buildInfoResult1);

            subject1.Equals(subject2).Should().BeTrue();
            subject1.Equals(subject3).Should().BeFalse();
            subject1.Equals(subject4).Should().BeFalse();
            subject1.Equals(subject5).Should().BeFalse();
        }
コード例 #14
0
        public async Task <ConnectionDescription> InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            var authenticators = connection.Settings.AuthenticatorFactories.Select(f => f.Create()).ToList();
            var helloCommand   = CreateInitialHelloCommand(authenticators);
            var helloProtocol  = HelloHelper.CreateProtocol(helloCommand, _serverApi);
            var helloResult    = await HelloHelper.GetResultAsync(connection, helloProtocol, cancellationToken).ConfigureAwait(false);

            var buildInfoProtocol = CreateBuildInfoProtocol(_serverApi);
            var buildInfoResult   = new BuildInfoResult(await buildInfoProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false));

            var description = new ConnectionDescription(connection.ConnectionId, helloResult, buildInfoResult);

            await AuthenticationHelper.AuthenticateAsync(connection, description, authenticators, cancellationToken).ConfigureAwait(false);

            var connectionIdServerValue = helloResult.ConnectionIdServerValue;

            if (connectionIdServerValue.HasValue)
            {
                description = UpdateConnectionIdWithServerValue(description, connectionIdServerValue.Value);
            }
            else
            {
                try
                {
                    var getLastErrorProtocol = CreateGetLastErrorProtocol(_serverApi);
                    var getLastErrorResult   = await getLastErrorProtocol
                                               .ExecuteAsync(connection, cancellationToken)
                                               .ConfigureAwait(false);

                    description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
                }
                catch
                {
                    // if we couldn't get the server's connection id, so be it.
                }
            }

            return(description);
        }
コード例 #15
0
        public async Task <ConnectionDescription> InitializeConnectionAsync(IConnection connection, ConnectionId connectionId, TimeSpan timeout, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, "connection");
            Ensure.IsNotNull(connectionId, "connectionId");
            Ensure.IsInfiniteOrGreaterThanOrEqualToZero(timeout, "timeout");

            var slidingTimeout = new SlidingTimeout(timeout);

            var isMasterCommand  = new BsonDocument("isMaster", 1);
            var isMasterProtocol = new CommandWireProtocol("admin", isMasterCommand, true);
            var isMasterResult   = new IsMasterResult(await isMasterProtocol.ExecuteAsync(connection, slidingTimeout, cancellationToken));

            // authentication is currently broken on arbiters
            if (!isMasterResult.IsArbiter)
            {
                foreach (var authenticator in connection.Settings.Authenticators)
                {
                    await authenticator.AuthenticateAsync(connection, slidingTimeout, cancellationToken);
                }
            }

            var buildInfoCommand  = new BsonDocument("buildInfo", 1);
            var buildInfoProtocol = new CommandWireProtocol("admin", buildInfoCommand, true);
            var buildInfoResult   = new BuildInfoResult(await buildInfoProtocol.ExecuteAsync(connection, slidingTimeout, cancellationToken));

            var getLastErrorCommand  = new BsonDocument("getLastError", 1);
            var getLastErrorProtocol = new CommandWireProtocol("admin", getLastErrorCommand, true);
            var getLastErrorResult   = await getLastErrorProtocol.ExecuteAsync(connection, slidingTimeout, cancellationToken);

            BsonValue connectionIdBsonValue;

            if (getLastErrorResult.TryGetValue("connectionId", out connectionIdBsonValue))
            {
                connectionId = connectionId.WithServerValue(connectionIdBsonValue.ToInt32());
            }

            return(new ConnectionDescription(connectionId, isMasterResult, buildInfoResult));
        }
コード例 #16
0
        public ConnectionDescription InitializeConnection(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            var authenticators   = connection.Settings.AuthenticatorFactories.Select(f => f.Create()).ToList();
            var isMasterCommand  = CreateInitialIsMasterCommand(authenticators);
            var isMasterProtocol = IsMasterHelper.CreateProtocol(isMasterCommand, _serverApi);
            var isMasterResult   = IsMasterHelper.GetResult(connection, isMasterProtocol, cancellationToken);

            var buildInfoProtocol = CreateBuildInfoProtocol(_serverApi);
            var buildInfoResult   = new BuildInfoResult(buildInfoProtocol.Execute(connection, cancellationToken));

            var description = new ConnectionDescription(connection.ConnectionId, isMasterResult, buildInfoResult);

            AuthenticationHelper.Authenticate(connection, description, authenticators, cancellationToken);

            var connectionIdServerValue = isMasterResult.ConnectionIdServerValue;

            if (connectionIdServerValue.HasValue)
            {
                description = UpdateConnectionIdWithServerValue(description, connectionIdServerValue.Value);
            }
            else
            {
                try
                {
                    var getLastErrorProtocol = CreateGetLastErrorProtocol(_serverApi);
                    var getLastErrorResult   = getLastErrorProtocol.Execute(connection, cancellationToken);

                    description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
                }
                catch
                {
                    // if we couldn't get the server's connection id, so be it.
                }
            }

            return(description);
        }