// constructors
 public BinaryConnectionFactory(ConnectionSettings settings, IStreamFactory streamFactory, IEventSubscriber eventSubscriber)
 {
     _settings = Ensure.IsNotNull(settings, nameof(settings));
     _streamFactory = Ensure.IsNotNull(streamFactory, nameof(streamFactory));
     _eventSubscriber = Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber));
     _connectionInitializer = new ConnectionInitializer(settings.ApplicationName);
 }
        public void Authenticate_should_invoke_authenticators_when_they_exist(
            [Values(false, true)]
            bool async)
        {
            var description = new ConnectionDescription(
                new ConnectionId(new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27017))),
                new IsMasterResult(new BsonDocument("ok", 1)),
                new BuildInfoResult(new BsonDocument("version", "2.8.0")));

            var authenticator = Substitute.For<IAuthenticator>();
            var settings = new ConnectionSettings(authenticators: new[] { authenticator });

            var connection = Substitute.For<IConnection>();
            connection.Description.Returns(description);
            connection.Settings.Returns(settings);

            if (async)
            {
                AuthenticationHelper.AuthenticateAsync(connection, description, CancellationToken.None).GetAwaiter().GetResult();

                authenticator.ReceivedWithAnyArgs().AuthenticateAsync(null, null, CancellationToken.None);
            }
            else
            {
                AuthenticationHelper.Authenticate(connection, description, CancellationToken.None);

                authenticator.ReceivedWithAnyArgs().Authenticate(null, null, CancellationToken.None);
            }
        }
        // constructors
        public BinaryConnection(ServerId serverId, EndPoint endPoint, ConnectionSettings settings, IStreamFactory streamFactory, IConnectionInitializer connectionInitializer, IEventSubscriber eventSubscriber)
        {
            Ensure.IsNotNull(serverId, nameof(serverId));
            _endPoint = Ensure.IsNotNull(endPoint, nameof(endPoint));
            _settings = Ensure.IsNotNull(settings, nameof(settings));
            _streamFactory = Ensure.IsNotNull(streamFactory, nameof(streamFactory));
            _connectionInitializer = Ensure.IsNotNull(connectionInitializer, nameof(connectionInitializer));
            Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber));

            _backgroundTaskCancellationTokenSource = new CancellationTokenSource();
            _backgroundTaskCancellationToken = _backgroundTaskCancellationTokenSource.Token;

            _connectionId = new ConnectionId(serverId);
            _receiveLock = new SemaphoreSlim(1);
            _sendLock = new SemaphoreSlim(1);
            _state = new InterlockedInt32(State.Initial);

            _commandEventHelper = new CommandEventHelper(eventSubscriber);
            eventSubscriber.TryGetEventHandler(out _failedEventHandler);
            eventSubscriber.TryGetEventHandler(out _closingEventHandler);
            eventSubscriber.TryGetEventHandler(out _closedEventHandler);
            eventSubscriber.TryGetEventHandler(out _openingEventHandler);
            eventSubscriber.TryGetEventHandler(out _openedEventHandler);
            eventSubscriber.TryGetEventHandler(out _failedOpeningEventHandler);
            eventSubscriber.TryGetEventHandler(out _receivingMessageEventHandler);
            eventSubscriber.TryGetEventHandler(out _receivedMessageEventHandler);
            eventSubscriber.TryGetEventHandler(out _failedReceivingMessageEventHandler);
            eventSubscriber.TryGetEventHandler(out _sendingMessagesEventHandler);
            eventSubscriber.TryGetEventHandler(out _sentMessagesEventHandler);
            eventSubscriber.TryGetEventHandler(out _failedSendingMessagesEvent);
        }
        public void Authenticate_should_invoke_authenticators_when_they_exist(
            [Values(false, true)]
            bool async)
        {
            var description = new ConnectionDescription(
                new ConnectionId(new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27017))),
                new IsMasterResult(new BsonDocument("ok", 1)),
                new BuildInfoResult(new BsonDocument("version", "2.8.0")));

            var mockAuthenticator = new Mock<IAuthenticator>();
            var settings = new ConnectionSettings(authenticators: new[] { mockAuthenticator.Object });

            var mockConnection = new Mock<IConnection>();
            mockConnection.SetupGet(c => c.Description).Returns(description);
            mockConnection.SetupGet(c => c.Settings).Returns(settings);

            if (async)
            {
                AuthenticationHelper.AuthenticateAsync(mockConnection.Object, description, CancellationToken.None).GetAwaiter().GetResult();

                mockAuthenticator.Verify(a => a.AuthenticateAsync(mockConnection.Object, description, CancellationToken.None), Times.Once);
            }
            else
            {
                AuthenticationHelper.Authenticate(mockConnection.Object, description, CancellationToken.None);

                mockAuthenticator.Verify(a => a.Authenticate(mockConnection.Object, description, CancellationToken.None), Times.Once);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConnectionOpenedEvent" /> struct.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="connectionSettings">The connection settings.</param>
 /// <param name="duration">The duration of time it took to open the connection.</param>
 /// <param name="operationId">The operation identifier.</param>
 public ConnectionOpenedEvent(ConnectionId connectionId, ConnectionSettings connectionSettings, TimeSpan duration, long? operationId)
 {
     _connectionId = connectionId;
     _connectionSettings = connectionSettings;
     _duration = duration;
     _operationId = operationId;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConnectionOpeningFailedEvent" /> struct.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="connectionSettings">The connection settings.</param>
 /// <param name="exception">The exception.</param>
 /// <param name="operationId">The operation identifier.</param>
 public ConnectionOpeningFailedEvent(ConnectionId connectionId, ConnectionSettings connectionSettings, Exception exception, long? operationId)
 {
     _connectionId = connectionId;
     _connectionSettings = connectionSettings;
     _exception = exception;
     _operationId = operationId;
 }
        public void constructor_should_initialize_instance()
        {
            var subject = new ConnectionSettings();

            subject.Authenticators.Should().BeEmpty();
            subject.MaxIdleTime.Should().Be(TimeSpan.FromMinutes(10));
            subject.MaxLifeTime.Should().Be(TimeSpan.FromMinutes(30));
        }
 // constructors
 public ClusterBuilder()
 {
     _clusterSettings = new ClusterSettings();
     _serverSettings = new ServerSettings();
     _connectionPoolSettings = new ConnectionPoolSettings();
     _connectionSettings = new ConnectionSettings();
     _tcpStreamSettings = new TcpStreamSettings();
     _streamFactoryWrapper = inner => inner;
 }
        public void constructor_with_applicationName_should_initialize_instance()
        {
            var subject = new ConnectionSettings(applicationName: "app");

            subject.ApplicationName.Should().Be("app");
            subject.Authenticators.Should().Equal(__defaults.Authenticators);
            subject.MaxIdleTime.Should().Be(__defaults.MaxIdleTime);
            subject.MaxLifeTime.Should().Be(__defaults.MaxLifeTime);
        }
        public void constructor_with_maxIdleTime_should_initialize_instance()
        {
            var maxIdleTime = TimeSpan.FromSeconds(123);

            var subject = new ConnectionSettings(maxIdleTime: maxIdleTime);

            subject.Authenticators.Should().Equal(__defaults.Authenticators);
            subject.MaxIdleTime.Should().Be(maxIdleTime);
            subject.MaxLifeTime.Should().Be(__defaults.MaxLifeTime);
        }
        public void constructor_with_authenticators_should_initialize_instance()
        {
            var authenticators = new[] { new MongoDBCRAuthenticator(new UsernamePasswordCredential("source", "username", "password")) };

            var subject = new ConnectionSettings(authenticators: authenticators);

            subject.Authenticators.Should().Equal(authenticators);
            subject.MaxIdleTime.Should().Be(__defaults.MaxIdleTime);
            subject.MaxLifeTime.Should().Be(__defaults.MaxLifeTime);
        }
        public void AuthenticateAsync_should_not_invoke_authenticators_when_connected_to_an_arbiter()
        {
            var description = new ConnectionDescription(
                new ConnectionId(new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27017))),
                new IsMasterResult(new BsonDocument("ok", 1).Add("setName", "rs").Add("arbiterOnly", true)),
                new BuildInfoResult(new BsonDocument("version", "2.8.0")));

            var authenticator = Substitute.For<IAuthenticator>();
            var settings = new ConnectionSettings(authenticators: new[] { authenticator });

            var connection = Substitute.For<IConnection>();
            connection.Description.Returns(description);
            connection.Settings.Returns(settings);

            AuthenticationHelper.AuthenticateAsync(connection, description, CancellationToken.None).Wait();

            authenticator.DidNotReceiveWithAnyArgs().AuthenticateAsync(null, null, CancellationToken.None);
        }
        public void With_maxLifeTime_should_return_expected_result()
        {
            var oldMaxLifeTime = TimeSpan.FromSeconds(1);
            var newMaxLifeTime = TimeSpan.FromSeconds(2);
            var subject = new ConnectionSettings(maxLifeTime: oldMaxLifeTime);

            var result = subject.With(maxLifeTime: newMaxLifeTime);

            result.Authenticators.Should().Equal(subject.Authenticators);
            result.MaxIdleTime.Should().Be(subject.MaxIdleTime);
            result.MaxLifeTime.Should().Be(newMaxLifeTime);
        }
 // constructors
 public BinaryConnectionFactory(ConnectionSettings settings, IStreamFactory streamFactory, IEventSubscriber eventSubscriber)
 {
     _settings = Ensure.IsNotNull(settings, nameof(settings));
     _streamFactory = Ensure.IsNotNull(streamFactory, nameof(streamFactory));
     _eventSubscriber = Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber));
 }
 // constructors
 public BinaryConnectionFactory(ConnectionSettings settings, IStreamFactory streamFactory, IEventSubscriber eventSubscriber)
 {
     _settings = Ensure.IsNotNull(settings, "settings");
     _streamFactory = Ensure.IsNotNull(streamFactory, "streamFactory");
     _eventSubscriber = Ensure.IsNotNull(eventSubscriber, "eventSubscriber");
 }
Esempio n. 16
0
        public ClusterBuilder ConfigureConnection(Func<ConnectionSettings, ConnectionSettings> configure)
        {
            Ensure.IsNotNull(configure, "configure");

            _connectionSettings = configure(_connectionSettings);
            return this;
        }
 // constructors
 public Builder(ConnectionSettings other)
 {
     _authenticators = other._authenticators;
     _maxIdleTime = other._maxIdleTime;
     _maxLifeTime = other._maxLifeTime;
 }
 public BinaryConnectionFactory(ConnectionSettings settings, IStreamFactory streamFactory, IConnectionListener listener)
 {
     _settings = Ensure.IsNotNull(settings, "settings");
     _streamFactory = Ensure.IsNotNull(streamFactory, "streamFactory");
     _listener = listener;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConnectionOpeningEvent"/> struct.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="connectionSettings">The connection settings.</param>
 public ConnectionOpeningEvent(ConnectionId connectionId, ConnectionSettings connectionSettings)
 {
     _connectionId = connectionId;
     _connectionSettings = connectionSettings;
 }
 private ConnectionSettings ConfigureConnection(ConnectionSettings settings, ClusterKey clusterKey)
 {
     var authenticators = clusterKey.Credentials.Select(c => c.ToAuthenticator());
     return settings.With(
         authenticators: Optional.Enumerable(authenticators),
         maxIdleTime: clusterKey.MaxConnectionIdleTime,
         maxLifeTime: clusterKey.MaxConnectionLifeTime,
         applicationName: clusterKey.ApplicationName);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConnectionOpeningEvent" /> struct.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="connectionSettings">The connection settings.</param>
 /// <param name="operationId">The operation identifier.</param>
 public ConnectionOpeningEvent(ConnectionId connectionId, ConnectionSettings connectionSettings, long? operationId)
 {
     _connectionId = connectionId;
     _connectionSettings = connectionSettings;
     _operationId = operationId;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConnectionOpeningFailedEvent"/> struct.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="connectionSettings">The connection settings.</param>
 /// <param name="exception">The exception.</param>
 public ConnectionOpeningFailedEvent(ConnectionId connectionId, ConnectionSettings connectionSettings, Exception exception)
 {
     _connectionId = connectionId;
     _connectionSettings = connectionSettings;
     _exception = exception;
 }
        public void With_applicationName_should_return_expected_result()
        {
            var oldApplicationName = "app1";
            var newApplicationName = "app2";
            var subject = new ConnectionSettings(applicationName: oldApplicationName);

            var result = subject.With(applicationName: newApplicationName);

            result.ApplicationName.Should().Be(newApplicationName);
            result.Authenticators.Should().Equal(subject.Authenticators);
            result.MaxIdleTime.Should().Be(subject.MaxIdleTime);
            result.MaxLifeTime.Should().Be(subject.MaxLifeTime);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConnectionOpenedEvent"/> struct.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="connectionSettings">The connection settings.</param>
 /// <param name="duration">The duration of time it took to open the connection.</param>
 public ConnectionOpenedEvent(ConnectionId connectionId, ConnectionSettings connectionSettings, TimeSpan duration)
 {
     _connectionId = connectionId;
     _connectionSettings = connectionSettings;
     _duration = duration;
 }
        public void With_authenticators_should_return_expected_result()
        {
            var oldAuthenticators = new[] { new MongoDBCRAuthenticator(new UsernamePasswordCredential("source", "username1", "password1")) };
            var newAuthenticators = new[] { new MongoDBCRAuthenticator(new UsernamePasswordCredential("source", "username2", "password2")) };
            var subject = new ConnectionSettings(authenticators: oldAuthenticators);

            var result = subject.With(authenticators: newAuthenticators);

            result.Authenticators.Should().Equal(newAuthenticators);
            result.MaxIdleTime.Should().Be(subject.MaxIdleTime);
            result.MaxLifeTime.Should().Be(subject.MaxLifeTime);
        }