private ICluster BuildCluster(BsonDocument definition)
        {
            var connectionString = new ConnectionString(definition["uri"].AsString);
            var settings         = new ClusterSettings(
#pragma warning disable CS0618
                connectionModeSwitch: connectionString.ConnectionModeSwitch,
#pragma warning restore CS0618
                endPoints: Optional.Enumerable(connectionString.Hosts),
                replicaSetName: connectionString.ReplicaSet);

#pragma warning disable CS0618
            if (connectionString.ConnectionModeSwitch == ConnectionModeSwitch.UseDirectConnection)
            {
                settings = settings.With(directConnection: connectionString.DirectConnection);
            }
            else
            {
                settings = settings.With(connectionMode: connectionString.Connect);
            }
#pragma warning restore CS0618

            _eventSubscriber = new EventCapturer();
            _eventSubscriber.Capture <ClusterOpeningEvent>(e => true);
            _eventSubscriber.Capture <ClusterDescriptionChangedEvent>(e => true);
            _eventSubscriber.Capture <ServerOpeningEvent>(e => true);
            _eventSubscriber.Capture <ServerDescriptionChangedEvent>(e => true);
            _eventSubscriber.Capture <ServerClosedEvent>(e => true);
            _serverFactory = new MockClusterableServerFactory(_eventSubscriber);
            return(new ClusterFactory(settings, _serverFactory, _eventSubscriber)
                   .CreateCluster());
        }
Ejemplo n.º 2
0
        private ICluster BuildCluster(BsonDocument definition)
        {
            var connectionString = new ConnectionString(definition["uri"].AsString);
            var settings         = new ClusterSettings(
                endPoints: Optional.Enumerable(connectionString.Hosts),
                connectionMode: connectionString.Connect,
                replicaSetName: connectionString.ReplicaSet);

            _eventSubscriber = new EventCapturer();
            _eventSubscriber.Capture <ClusterOpeningEvent>(e => true);
            _eventSubscriber.Capture <ClusterDescriptionChangedEvent>(e => true);
            _eventSubscriber.Capture <ServerOpeningEvent>(e => true);
            _eventSubscriber.Capture <ServerDescriptionChangedEvent>(e => true);
            _eventSubscriber.Capture <ServerClosedEvent>(e => true);
            _serverFactory = new MockClusterableServerFactory(_eventSubscriber);
            return(new ClusterFactory(settings, _serverFactory, _eventSubscriber)
                   .CreateCluster());
        }
        protected override EventCapturer InitializeEventCapturer(EventCapturer eventCapturer)
        {
            var doNotCaptureEvents = DefaultCommandsToNotCapture;

            doNotCaptureEvents.Add("configureFailPoint");
            doNotCaptureEvents.Add("replSetStepDown");
            return(eventCapturer.Capture <CommandStartedEvent>(e => !doNotCaptureEvents.Contains(e.CommandName))
                   .Capture <ServerDescriptionChangedEvent>()
                   .Capture <ConnectionPoolClearedEvent>());
        }
Ejemplo n.º 4
0
        private EventCapturer CreateEventCapturer(IEnumerable <string> eventTypesToCapture, IEnumerable <string> commandNamesToSkip, IEventFormatter eventFormatter)
        {
            var eventCapturer = new EventCapturer(eventFormatter);

            foreach (var eventTypeToCapture in eventTypesToCapture)
            {
                switch (eventTypeToCapture.ToLowerInvariant())
                {
                case "commandstartedevent":
                    eventCapturer = eventCapturer.Capture <CommandStartedEvent>(x => !commandNamesToSkip.Contains(x.CommandName));
                    break;

                case "commandsucceededevent":
                    eventCapturer = eventCapturer.Capture <CommandSucceededEvent>(x => !commandNamesToSkip.Contains(x.CommandName));
                    break;

                case "commandfailedevent":
                    eventCapturer = eventCapturer.Capture <CommandFailedEvent>(x => !commandNamesToSkip.Contains(x.CommandName));
                    break;

                case "poolcreatedevent":
                    eventCapturer = eventCapturer.Capture <ConnectionPoolOpenedEvent>();
                    break;

                case "poolclearedevent":
                    eventCapturer = eventCapturer.Capture <ConnectionPoolClearedEvent>();
                    break;

                case "poolclosedevent":
                    eventCapturer = eventCapturer.Capture <ConnectionPoolClosedEvent>();
                    break;

                case "connectioncreatedevent":
                    eventCapturer = eventCapturer.Capture <ConnectionCreatedEvent>();
                    break;

                case "connectionclosedevent":
                    eventCapturer = eventCapturer.Capture <ConnectionClosedEvent>();
                    break;

                case "connectionreadyevent":
                    eventCapturer = eventCapturer.Capture <ConnectionOpenedEvent>();
                    break;

                case "connectioncheckoutstartedevent":
                    eventCapturer = eventCapturer.Capture <ConnectionPoolCheckingOutConnectionEvent>();
                    break;

                case "connectioncheckoutfailedevent":
                    eventCapturer = eventCapturer.Capture <ConnectionPoolCheckingOutConnectionFailedEvent>();
                    break;

                case "connectioncheckedoutevent":
                    eventCapturer = eventCapturer.Capture <ConnectionPoolCheckedOutConnectionEvent>();
                    break;

                case "connectioncheckedinevent":
                    eventCapturer = eventCapturer.Capture <ConnectionPoolCheckedInConnectionEvent>();
                    break;

                case "poolreadyevent":
                    // should be handled in the scope of CSHARP-3509
                    break;

                default:
                    throw new FormatException($"Invalid event name: {eventTypeToCapture}.");
                }
            }

            return(eventCapturer);
        }
        private void CreateClient(BsonDocument entity, out DisposableMongoClient client, out EventCapturer eventCapturer)
        {
            var eventTypesToCapture = new List <string>();
            var commandNamesToSkip  = new List <string>
            {
                "authenticate",
                "buildInfo",
                "configureFailPoint",
                "getLastError",
                "getnonce",
                "isMaster",
                "saslContinue",
                "saslStart"
            };

            var       readConcern             = ReadConcern.Default;
            var       retryReads              = true;
            var       retryWrites             = true;
            var       useMultipleShardRouters = false;
            var       writeConcern            = WriteConcern.Acknowledged;
            ServerApi serverApi = null;

            foreach (var element in entity)
            {
                switch (element.Name)
                {
                case "id":
                    // handled on higher level
                    break;

                case "uriOptions":
                    foreach (var option in element.Value.AsBsonDocument)
                    {
                        switch (option.Name)
                        {
                        case "retryWrites":
                            retryWrites = option.Value.AsBoolean;
                            break;

                        case "retryReads":
                            retryReads = option.Value.AsBoolean;
                            break;

                        case "readConcernLevel":
                            var levelValue = option.Value.AsString;
                            var level      = (ReadConcernLevel)Enum.Parse(typeof(ReadConcernLevel), levelValue, true);
                            readConcern = new ReadConcern(level);
                            break;

                        case "w":
                            writeConcern = new WriteConcern(option.Value.AsInt32);
                            break;

                        default:
                            throw new FormatException($"Unrecognized client uriOption name: '{option.Name}'.");
                        }
                    }
                    break;

                case "useMultipleMongoses":
                    useMultipleShardRouters = element.Value.AsBoolean;
                    break;

                case "observeEvents":
                    eventTypesToCapture.AddRange(element.Value.AsBsonArray.Select(x => x.AsString));
                    break;

                case "ignoreCommandMonitoringEvents":
                    commandNamesToSkip.AddRange(element.Value.AsBsonArray.Select(x => x.AsString));
                    break;

                case "serverApi":
                    ServerApiVersion serverApiVersion           = null;
                    bool?            serverApiStrict            = null;
                    bool?            serverApiDeprecationErrors = null;
                    foreach (var option in element.Value.AsBsonDocument)
                    {
                        switch (option.Name)
                        {
                        case "version":
                            var serverApiVersionString = option.Value.AsString;
                            switch (serverApiVersionString)
                            {
                            case "1":
                                serverApiVersion = ServerApiVersion.V1;
                                break;

                            default:
                                throw new FormatException($"Unrecognized serverApi version: '{serverApiVersionString}'.");
                            }
                            break;

                        case "strict":
                            serverApiStrict = option.Value.AsBoolean;
                            break;

                        case "deprecationErrors":
                            serverApiDeprecationErrors = option.Value.AsBoolean;
                            break;

                        default:
                            throw new FormatException($"Unrecognized client serverApi option name: '{option.Name}'.");
                        }
                    }
                    if (serverApiVersion != null)
                    {
                        serverApi = new ServerApi(serverApiVersion, serverApiStrict, serverApiDeprecationErrors);
                    }
                    break;

                default:
                    throw new FormatException($"Unrecognized client entity field: '{element.Name}'.");
                }
            }

            eventCapturer = null;
            if (eventTypesToCapture.Count > 0)
            {
                eventCapturer = new EventCapturer();
                foreach (var eventTypeToCapture in eventTypesToCapture)
                {
                    switch (eventTypeToCapture)
                    {
                    case "commandStartedEvent":
                        eventCapturer = eventCapturer.Capture <CommandStartedEvent>(x => !commandNamesToSkip.Contains(x.CommandName));
                        break;

                    case "commandSucceededEvent":
                        eventCapturer = eventCapturer.Capture <CommandSucceededEvent>(x => !commandNamesToSkip.Contains(x.CommandName));
                        break;

                    case "commandFailedEvent":
                        eventCapturer = eventCapturer.Capture <CommandFailedEvent>(x => !commandNamesToSkip.Contains(x.CommandName));
                        break;

                    default:
                        throw new FormatException($"Invalid event name: {eventTypeToCapture}.");
                    }
                }
            }

            var localEventCapturer = eventCapturer; // copy value of eventCapturer ref variable to a local variable (to avoid error CS1628)

            client = DriverTestConfiguration.CreateDisposableClient(
                settings =>
            {
                settings.RetryReads        = retryReads;
                settings.RetryWrites       = retryWrites;
                settings.ReadConcern       = readConcern;
                settings.WriteConcern      = writeConcern;
                settings.HeartbeatInterval = TimeSpan.FromMilliseconds(5);     // the default value for spec tests
                if (localEventCapturer != null)
                {
                    settings.ClusterConfigurator = c => c.Subscribe(localEventCapturer);
                }
                settings.ServerApi = serverApi;
            },
                useMultipleShardRouters);
        }
 protected virtual EventCapturer InitializeEventCapturer(EventCapturer eventCapturer)
 {
     return(eventCapturer.Capture <CommandStartedEvent>(e => !DefaultCommandsToNotCapture.Contains(e.CommandName)));
 }