public void GetChannel_should_get_a_connection([Values(false, true)] bool async)
        {
            _subject.Initialize();

            IChannelHandle channel;

            if (async)
            {
                channel = _subject.GetChannelAsync(CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                channel = _subject.GetChannel(CancellationToken.None);
            }

            channel.Should().NotBeNull();
        }
        public void GetChannel_should_clear_connection_pool_when_opening_connection_throws_MongoAuthenticationException(
            [Values(false, true)] bool async)
        {
            var connectionId                   = new ConnectionId(new ServerId(_clusterId, _endPoint));
            var mockConnectionHandle           = new Mock <IConnectionHandle>();
            var mockConnectionExceptionHandler = new Mock <IConnectionExceptionHandler>();

            LoadBalancedServer server = null;

            var mockConnectionPool      = new Mock <IConnectionPool>();
            var authenticationException = new MongoAuthenticationException(connectionId, "Invalid login.")
            {
                ServiceId = ObjectId.GenerateNewId()
            };

            mockConnectionPool
            .Setup(p => p.AcquireConnection(It.IsAny <CancellationToken>()))
            .Callback(() => server.HandleExceptionOnOpen(authenticationException))
            .Throws(authenticationException);
            mockConnectionPool
            .Setup(p => p.AcquireConnectionAsync(It.IsAny <CancellationToken>()))
            .Callback(() => server.HandleExceptionOnOpen(authenticationException))
            .Throws(authenticationException);
            mockConnectionPool.Setup(p => p.Clear(It.IsAny <ObjectId>()));

            var mockConnectionPoolFactory = new Mock <IConnectionPoolFactory>();

            mockConnectionPoolFactory
            .Setup(f => f.CreateConnectionPool(It.IsAny <ServerId>(), _endPoint, It.IsAny <IConnectionExceptionHandler>()))
            .Returns(mockConnectionPool.Object);

            server = new LoadBalancedServer(
                _clusterId,
                _clusterClock,
                _settings,
                _endPoint,
                mockConnectionPoolFactory.Object,
                _capturedEvents,
                _serverApi);
            server.Initialize();

            var exception = Record.Exception(() =>
            {
                if (async)
                {
                    server.GetChannelAsync(CancellationToken.None).GetAwaiter().GetResult();
                }
                else
                {
                    server.GetChannel(CancellationToken.None);
                }
            });

            exception.Should().BeOfType <MongoAuthenticationException>();
            mockConnectionPool.Verify(p => p.Clear(It.IsAny <ObjectId>()), Times.Once());
        }
        public void GetChannel_should_not_update_topology_and_clear_connection_pool_on_MongoConnectionException(
            [Values("TimedOutSocketException", "NetworkUnreachableSocketException")] string errorType,
            [Values(false, true)] bool async)
        {
            var serverId                       = new ServerId(_clusterId, _endPoint);
            var connectionId                   = new ConnectionId(serverId);
            var innerMostException             = CoreExceptionHelper.CreateException(errorType);
            var mockConnectionExceptionHandler = new Mock <IConnectionExceptionHandler>();

            var openConnectionException = new MongoConnectionException(connectionId, "Oops", new IOException("Cry", innerMostException));
            var mockConnection          = new Mock <IConnectionHandle>();

            mockConnection.Setup(c => c.Open(It.IsAny <CancellationToken>())).Throws(openConnectionException);
            mockConnection.Setup(c => c.OpenAsync(It.IsAny <CancellationToken>())).ThrowsAsync(openConnectionException);

            var connectionFactory = new Mock <IConnectionFactory>();

            connectionFactory.Setup(cf => cf.CreateConnection(serverId, _endPoint)).Returns(mockConnection.Object);

            var connectionPoolSettings = new ConnectionPoolSettings();
            var connectionPool         = new ExclusiveConnectionPool(serverId, _endPoint, connectionPoolSettings, connectionFactory.Object, new EventAggregator(), mockConnectionExceptionHandler.Object);

            var mockConnectionPoolFactory = new Mock <IConnectionPoolFactory>();

            mockConnectionPoolFactory
            .Setup(f => f.CreateConnectionPool(It.IsAny <ServerId>(), _endPoint, It.IsAny <IConnectionExceptionHandler>()))
            .Returns(connectionPool);

            var subject = new LoadBalancedServer(_clusterId, _clusterClock, _settings, _endPoint, mockConnectionPoolFactory.Object, _capturedEvents, _serverApi);

            subject.Initialize();

            IChannelHandle channel = null;
            Exception      exception;

            if (async)
            {
                exception = Record.Exception(() => channel = subject.GetChannelAsync(CancellationToken.None).GetAwaiter().GetResult());
            }
            else
            {
                exception = Record.Exception(() => channel = subject.GetChannel(CancellationToken.None));
            }

            channel.Should().BeNull();
            exception.Should().Be(openConnectionException);
            subject.Description.Type.Should().Be(ServerType.LoadBalanced);
            subject.Description.ReasonChanged.Should().Be("Initialized");
            subject.Description.State.Should().Be(ServerState.Connected);

            _mockConnectionPool.Verify(c => c.Clear(It.IsAny <bool>()), Times.Never);
        }