Пример #1
0
        // private methods
        private Server SetupServer(bool exceptionOnConnectionOpen, bool exceptionOnConnectionAcquire)
        {
            var connectionId         = new ConnectionId(new ServerId(_clusterId, _endPoint));
            var mockConnectionHandle = new Mock <IConnectionHandle>();

            mockConnectionHandle
            .Setup(c => c.Fork())
            .Returns(mockConnectionHandle.Object);

            if (exceptionOnConnectionOpen)
            {
                mockConnectionHandle
                .Setup(c => c.Open(It.IsAny <CancellationToken>()))
                .Throws(new MongoAuthenticationException(connectionId, "Invalid login."));
                mockConnectionHandle
                .Setup(c => c.OpenAsync(It.IsAny <CancellationToken>()))
                .Throws(new MongoAuthenticationException(connectionId, "Invalid login."));
            }

            var mockConnectionPool = new Mock <IConnectionPool>();

            if (exceptionOnConnectionAcquire)
            {
                mockConnectionPool
                .Setup(p => p.AcquireConnection(It.IsAny <CancellationToken>()))
                .Throws(new TimeoutException("Timeout"));
                mockConnectionPool
                .Setup(p => p.AcquireConnectionAsync(It.IsAny <CancellationToken>()))
                .Throws(new TimeoutException("Timeout"));
                mockConnectionPool.Setup(p => p.Clear());
            }
            else
            {
                mockConnectionPool
                .Setup(p => p.AcquireConnection(It.IsAny <CancellationToken>()))
                .Returns(mockConnectionHandle.Object);
                mockConnectionPool
                .Setup(p => p.AcquireConnectionAsync(It.IsAny <CancellationToken>()))
                .Returns(Task.FromResult(mockConnectionHandle.Object));
                mockConnectionPool.Setup(p => p.Clear());
            }

            var mockConnectionPoolFactory = new Mock <IConnectionPoolFactory>();

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

            var server = new Server(
                _clusterId,
                _clusterClock,
                _clusterConnectionMode,
                _connectionModeSwitch,
                _directConnection,
                _settings,
                _endPoint,
                mockConnectionPoolFactory.Object,
                _mockServerMonitorFactory.Object,
                _capturedEvents,
                _serverApi);

            server.Initialize();

            return(server);
        }
Пример #2
0
        public void HandleChannelException_should_update_topology_as_expected_on_network_error_or_timeout(
            string errorType, bool shouldUpdateTopology)
        {
            var       serverId     = new ServerId(_clusterId, _endPoint);
            var       connectionId = new ConnectionId(serverId);
            Exception innerMostException;

            switch (errorType)
            {
            case "MongoConnectionExceptionWithSocketTimeout":
                innerMostException = new SocketException((int)SocketError.TimedOut);
                break;

            case nameof(MongoConnectionException):
                innerMostException = new SocketException((int)SocketError.NetworkUnreachable);
                break;

            default: throw new ArgumentException("Unknown error type.");
            }

            var operationUsingChannelException = new MongoConnectionException(connectionId, "Oops", new IOException("Cry", innerMostException));
            var mockConnection = new Mock <IConnectionHandle>();
            var isMasterResult = new IsMasterResult(new BsonDocument {
                { "compressors", new BsonArray() }
            });
            // the server version doesn't matter when we're not testing MongoNotPrimaryExceptions, but is needed when
            // Server calls ShouldClearConnectionPoolForException
            var buildInfoResult = new BuildInfoResult(new BsonDocument {
                { "version", "4.4.0" }
            });

            mockConnection.SetupGet(c => c.Description)
            .Returns(new ConnectionDescription(new ConnectionId(serverId, 0), isMasterResult, buildInfoResult));
            var mockConnectionPool = new Mock <IConnectionPool>();

            mockConnectionPool.Setup(p => p.AcquireConnection(It.IsAny <CancellationToken>())).Returns(mockConnection.Object);
            mockConnectionPool.Setup(p => p.AcquireConnectionAsync(It.IsAny <CancellationToken>())).ReturnsAsync(mockConnection.Object);
            var mockConnectionPoolFactory = new Mock <IConnectionPoolFactory>();

            mockConnectionPoolFactory
            .Setup(f => f.CreateConnectionPool(It.IsAny <ServerId>(), _endPoint))
            .Returns(mockConnectionPool.Object);
            var mockMonitorServerInitialDescription = new ServerDescription(serverId, _endPoint).With(reasonChanged: "Initial D", type: ServerType.Unknown);
            var mockServerMonitor = new Mock <IServerMonitor>();

            mockServerMonitor.SetupGet(m => m.Description).Returns(mockMonitorServerInitialDescription);
            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 Server(_clusterId, _clusterClock, _clusterConnectionMode, _connectionModeSwitch, _directConnection, _settings, _endPoint, mockConnectionPoolFactory.Object, mockServerMonitorFactory.Object, _capturedEvents, _serverApi);

            subject.Initialize();
            var heartbeatDescription = mockMonitorServerInitialDescription.With(reasonChanged: "Heartbeat", type: ServerType.Standalone);

            mockServerMonitor.Setup(m => m.Description).Returns(heartbeatDescription);
            mockServerMonitor.Raise(
                m => m.DescriptionChanged += null,
                new ServerDescriptionChangedEventArgs(mockMonitorServerInitialDescription, heartbeatDescription));
            subject.Description.Should().Be(heartbeatDescription);

            subject.HandleChannelException(mockConnection.Object, operationUsingChannelException);

            if (shouldUpdateTopology)
            {
                subject.Description.Type.Should().Be(ServerType.Unknown);
                subject.Description.ReasonChanged.Should().Contain("ChannelException");
            }
            else
            {
                subject.Description.Should().Be(heartbeatDescription);
            }
        }
Пример #3
0
 public static bool IsStateChangeError(this Server server, ServerErrorCode?code, string message)
 {
     return((bool)Reflector.Invoke(server, nameof(IsStateChangeError), code, message));
 }
Пример #4
0
 public static bool IsRecovering(this Server server, ServerErrorCode?code, string message)
 {
     return((bool)Reflector.Invoke(server, nameof(IsRecovering), code, message));
 }
Пример #5
0
 public static void HandleChannelException(this Server server, IConnection connection, Exception ex)
 {
     Reflector.Invoke(server, nameof(HandleChannelException), connection, ex);
 }