public async Task <ConnectionDescription> InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));

            var isMasterProtocol = CreateIsMasterProtocol();
            var isMasterResult   = new IsMasterResult(await isMasterProtocol.ExecuteAsync(connection, 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);
        }
        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);
        }
Exemplo n.º 3
0
        public void Me_should_parse_document_correctly(string json, string expectedEndPoint)
        {
            var endPoint = expectedEndPoint == null ? (EndPoint)null : EndPointHelper.Parse(expectedEndPoint);

            var subject = new IsMasterResult(BsonDocument.Parse(json));

            subject.Me.Should().Be(endPoint);
        }
Exemplo n.º 4
0
        public void Compression_should_parse_document_correctly(string json, CompressorType[] expectedCompression)
        {
            var subject = new IsMasterResult(BsonDocument.Parse(json));

            var result = subject.Compressions;

            result.Should().Equal(expectedCompression);
        }
Exemplo n.º 5
0
        public void LogicalSessionTimeout_should_parse_document_correctly(string json, int?expectedResultMinutes)
        {
            var subject        = new IsMasterResult(BsonDocument.Parse(json));
            var expectedResult = expectedResultMinutes == null ? (TimeSpan?)null : TimeSpan.FromMinutes(expectedResultMinutes.Value);

            var result = subject.LogicalSessionTimeout;

            result.Should().Be(expectedResult);
        }
Exemplo n.º 6
0
        public void Compression_should_throw_the_exception_for_an_unsupported_compression_type(string json, string expectedUnsupportedCompressor)
        {
            var subject = new IsMasterResult(BsonDocument.Parse(json));

            var exception = Record.Exception(() => subject.Compressions);

            var e = exception.Should().BeOfType <NotSupportedException>().Subject;

            e.Message.Should().Be($"The unsupported compressor name: '{expectedUnsupportedCompressor}'.");
        }
        public void ServiceId_should_parse_document_correctly(string json, bool shouldBeParsed)
        {
            var subject = new IsMasterResult(BsonDocument.Parse(json));

            subject.ServiceId.HasValue.Should().Be(shouldBeParsed);
            if (shouldBeParsed)
            {
                subject.ServiceId.Should().Be(ObjectId.Empty);
            }
        }
Exemplo n.º 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;
        }
Exemplo n.º 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);
        }
Exemplo n.º 10
0
        public void ServerType_should_parse_document_correctly(string json, ServerType expected)
        {
            var subject = new IsMasterResult(BsonDocument.Parse(json));

            subject.ServerType.Should().Be(expected);
        }
Exemplo n.º 11
0
        public void MinWireVersion_should_parse_document_correctly(string json, int expected)
        {
            var subject = new IsMasterResult(BsonDocument.Parse(json));

            subject.MinWireVersion.Should().Be(expected);
        }