コード例 #1
0
        private ExclusiveConnectionPool CreatePool(
            IEventSubscriber eventCapturer = null,
            TimeSpan?maintenanceInterval   = null,
            int minPoolSize = 0,
            Action <Mock <IConnectionFactory> > connectionFactoryConfigurator = null)
        {
            var mockConnectionFactory = new Mock <IConnectionFactory>();

            if (connectionFactoryConfigurator == null)
            {
                mockConnectionFactory
                .Setup(f => f.CreateConnection(__serverId, __endPoint))
                .Returns(() => new MockConnection(__serverId, new ConnectionSettings(), eventCapturer));
            }
            else
            {
                connectionFactoryConfigurator(mockConnectionFactory);
            }

            var exclusiveConnectionPool = new ExclusiveConnectionPool(
                __serverId,
                __endPoint,
                new ConnectionPoolSettings(maintenanceInterval: maintenanceInterval.GetValueOrDefault(defaultValue: __dummyInterval), minConnections: minPoolSize),
                mockConnectionFactory.Object,
                eventCapturer ?? Mock.Of <IEventSubscriber>(),
                Mock.Of <IConnectionExceptionHandler>());

            exclusiveConnectionPool.Initialize();
            exclusiveConnectionPool.SetReady(); // MaintenanceHelper is started

            return(exclusiveConnectionPool);
        }
        private IConnectionPool SetupConnectionPoolMock(BsonDocument test, IEventSubscriber eventSubscriber)
        {
            var endPoint = new DnsEndPoint("localhost", 27017);
            var serverId = new ServerId(new ClusterId(), endPoint);

            ParseSettings(test, out var connectionPoolSettings, out var connectionSettings);

            var connectionFactory = new Mock <IConnectionFactory>();
            var exceptionHandler  = new Mock <IConnectionExceptionHandler>();

            connectionFactory
            .Setup(c => c.CreateConnection(serverId, endPoint))
            .Returns(() =>
            {
                var connection = new MockConnection(serverId, connectionSettings, eventSubscriber);
                return(connection);
            });
            var connectionPool = new ExclusiveConnectionPool(
                serverId,
                endPoint,
                connectionPoolSettings,
                connectionFactory.Object,
                eventSubscriber,
                exceptionHandler.Object);

            return(connectionPool);
        }
コード例 #3
0
        public void GetChannel_should_update_topology_and_clear_connection_pool_on_network_error_or_timeout(
            [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 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 mockExceptionHandler   = new Mock <IConnectionExceptionHandler>();
            var connectionPoolSettings = new ConnectionPoolSettings();
            var connectionPool         = new ExclusiveConnectionPool(serverId, _endPoint, connectionPoolSettings, connectionFactory.Object, new EventAggregator(), mockExceptionHandler.Object);

            var mockConnectionPoolFactory = new Mock <IConnectionPoolFactory>();

            mockConnectionPoolFactory
            .Setup(f => f.CreateConnectionPool(It.IsAny <ServerId>(), _endPoint, It.IsAny <IConnectionExceptionHandler>()))
            .Returns(connectionPool);
            var mockMonitorServerDescription = new ServerDescription(serverId, _endPoint);
            var mockServerMonitor            = new Mock <IServerMonitor>();

            mockServerMonitor.SetupGet(m => m.Description).Returns(mockMonitorServerDescription);
            mockServerMonitor.SetupGet(m => m.Lock).Returns(new object());
            var mockServerMonitorFactory = new Mock <IServerMonitorFactory>();

            mockServerMonitorFactory.Setup(f => f.Create(It.IsAny <ServerId>(), _endPoint)).Returns(mockServerMonitor.Object);

            var subject = new DefaultServer(_clusterId, _clusterClock, _clusterConnectionMode, _connectionModeSwitch, _directConnection, _settings, _endPoint, mockConnectionPoolFactory.Object, mockServerMonitorFactory.Object, _capturedEvents, _serverApi);

            connectionPool._connectionExceptionHandler(subject);
            subject.Initialize();
            connectionPool.SetReady();

            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.Unknown);
            subject.Description.ReasonChanged.Should().Contain("ChannelException during handshake");
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        private (IConnectionPool, FailPoint, ICluster) SetupConnectionData(BsonDocument test, IEventSubscriber eventSubscriber, bool isUnit)
        {
            ParseSettings(test, out var connectionPoolSettings, out var connectionSettings);

            IConnectionPool connectionPool;
            ICluster        cluster   = null;
            FailPoint       failPoint = null;

            if (isUnit)
            {
                var endPoint = new DnsEndPoint("localhost", 27017);
                var serverId = new ServerId(new ClusterId(), endPoint);

                var connectionFactory = new Mock <IConnectionFactory>();
                connectionFactory
                .Setup(c => c.CreateConnection(serverId, endPoint))
                .Returns(() =>
                {
                    var connection = new MockConnection(serverId, connectionSettings, eventSubscriber);
                    connection.Open(CancellationToken.None);
                    return(connection);
                });

                connectionPool = new ExclusiveConnectionPool(
                    serverId,
                    endPoint,
                    connectionPoolSettings,
                    connectionFactory.Object,
                    eventSubscriber);

                connectionPool.Initialize();
            }
            else
            {
                failPoint = ConfigureFailPoint(test, CoreTestConfiguration.Cluster);

                cluster = CoreTestConfiguration.CreateCluster(b =>
                                                              b.ConfigureConnectionPool(c => c.With(
                                                                                            maxConnections: connectionPoolSettings.MaxConnections,
                                                                                            minConnections: connectionPoolSettings.MinConnections,
                                                                                            waitQueueTimeout: connectionPoolSettings.WaitQueueTimeout))
                                                              .Subscribe(eventSubscriber));

                var server = cluster.SelectServer(WritableServerSelector.Instance, CancellationToken.None);
                connectionPool = server._connectionPool();
            }

            return(connectionPool, failPoint, cluster);
        }
コード例 #6
0
 public static ServiceStates _serviceStates(this ExclusiveConnectionPool obj)
 {
     return((ServiceStates)Reflector.GetFieldValue(obj, nameof(_serviceStates)));
 }
コード例 #7
0
 public static Task MaintainSizeAsync(this ExclusiveConnectionPool obj)
 {
     return((Task)Reflector.Invoke(obj, nameof(MaintainSizeAsync)));
 }
コード例 #8
0
 public static CancellationTokenSource _maintenanceCancellationTokenSource(this ExclusiveConnectionPool obj)
 {
     return((CancellationTokenSource)Reflector.GetFieldValue(obj, nameof(_maintenanceCancellationTokenSource)));
 }
コード例 #9
0
 private void IncrementGeneration(ExclusiveConnectionPool pool) => pool._generation(pool._generation() + 1);
コード例 #10
0
 // private methods
 private MaintenanceHelper CreateSubject(ExclusiveConnectionPool pool)
 {
     return(new MaintenanceHelper(pool, __dummyInterval));
 }
        private (IConnectionPool, FailPoint, ICluster, Func <object, bool>) SetupConnectionData(BsonDocument test, EventCapturer eventCapturer, bool isUnit)
        {
            ParseSettings(test, out var connectionPoolSettings, out var connectionSettings);

            IConnectionPool     connectionPool;
            ICluster            cluster      = null;
            FailPoint           failPoint    = null;
            Func <object, bool> eventsFilter = _ => true;

            if (isUnit)
            {
                var endPoint = new DnsEndPoint("localhost", 27017);
                var serverId = new ServerId(new ClusterId(), endPoint);

                var connectionFactory          = new Mock <IConnectionFactory>();
                var connectionExceptionHandler = new Mock <IConnectionExceptionHandler>();

                connectionFactory
                .Setup(c => c.CreateConnection(serverId, endPoint))
                .Returns(() =>
                {
                    var connection = new MockConnection(serverId, connectionSettings, eventCapturer);
                    return(connection);
                });

                connectionPool = new ExclusiveConnectionPool(
                    serverId,
                    endPoint,
                    connectionPoolSettings,
                    connectionFactory.Object,
                    eventCapturer,
                    connectionExceptionHandler.Object);

                connectionPool.Initialize();
            }
            else
            {
                var async = test.GetValue(Schema.async).ToBoolean();
                cluster = CoreTestConfiguration.CreateCluster(b => b
                                                              .ConfigureServer(s => s.With(
                                                                                   heartbeatInterval: TimeSpan.FromMinutes(10)))
                                                              .ConfigureConnectionPool(c => c.With(
                                                                                           maxConnecting: connectionPoolSettings.MaxConnecting,
                                                                                           maxConnections: connectionPoolSettings.MaxConnections,
                                                                                           minConnections: connectionPoolSettings.MinConnections,
                                                                                           maintenanceInterval: connectionPoolSettings.MaintenanceInterval,
                                                                                           waitQueueTimeout: connectionPoolSettings.WaitQueueTimeout))
                                                              .ConfigureConnection(s => s.With(applicationName: $"{connectionSettings.ApplicationName}_async_{async}"))
                                                              .Subscribe(eventCapturer));

                var server = cluster.SelectServer(WritableServerSelector.Instance, CancellationToken.None);
                connectionPool = server._connectionPool();

                if (test.TryGetValue(Schema.Intergration.failPoint, out var failPointDocument))
                {
                    if (failPointDocument.AsBsonDocument.Contains("data"))
                    {
                        var data = failPointDocument["data"].AsBsonDocument;
                        if (data.TryGetValue("appName", out var appNameValue))
                        {
                            data["appName"] = $"{appNameValue}_async_{async}";
                        }
                    }

                    var resetPool = connectionPoolSettings.MinConnections > 0;

                    if (resetPool)
                    {
                        eventCapturer.WaitForOrThrowIfTimeout(events => events.Any(e => e is ConnectionCreatedEvent), TimeSpan.FromMilliseconds(500));

                        var connectionIdsToIgnore = new HashSet <int>(eventCapturer.Events
                                                                      .OfType <ConnectionCreatedEvent>()
                                                                      .Select(c => c.ConnectionId.LocalValue)
                                                                      .ToList());

                        eventsFilter = o =>
                        {
                            if (o is ConnectionOpenedEvent
                                or ConnectionClosedEvent
                                or ConnectionCreatedEvent
                                or ConnectionFailedEvent)
                            {
                                var connectionId = o.ConnectionId();
                                return(!connectionIdsToIgnore.Contains(connectionId.LocalValue) &&
                                       EndPointHelper.Equals(connectionId.ServerId.EndPoint, server.EndPoint));
                            }

                            if (o is ConnectionPoolReadyEvent
                                or ConnectionPoolClearedEvent)
                            {
                                var serverId = o.ServerId();
                                return(EndPointHelper.Equals(serverId.EndPoint, server.EndPoint));
                            }

                            return(true);
                        };

                        connectionPool.Clear(closeInUseConnections: false);
                        eventCapturer.WaitForOrThrowIfTimeout(events => events.Any(e => e is ConnectionPoolClearedEvent), TimeSpan.FromMilliseconds(500));
                    }

                    var failPointServer = CoreTestConfiguration.Cluster.SelectServer(new EndPointServerSelector(server.EndPoint), default);
                    failPoint = FailPoint.Configure(failPointServer, NoCoreSession.NewHandle(), failPointDocument.AsBsonDocument);

                    if (resetPool)
                    {
                        eventCapturer.Clear();
                        connectionPool.SetReady();
                    }
                }
            }

            return(connectionPool, failPoint, cluster, eventsFilter);
        }