|
| 1 | +/* Copyright 2019-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using FluentAssertions; |
| 17 | +using MongoDB.Bson; |
| 18 | +using MongoDB.Bson.TestHelpers.XunitExtensions; |
| 19 | +using MongoDB.Driver.Core; |
| 20 | +using MongoDB.Driver.Core.Clusters; |
| 21 | +using MongoDB.Driver.Core.Events; |
| 22 | +using MongoDB.Driver.Core.Misc; |
| 23 | +using MongoDB.Driver.TestHelpers; |
| 24 | +using Xunit; |
| 25 | + |
| 26 | +namespace MongoDB.Driver.Tests |
| 27 | +{ |
| 28 | + public class ReadPreferenceOnStandaloneTests |
| 29 | + { |
| 30 | + [Theory] |
| 31 | + [ParameterAttributeData] |
| 32 | + public void ReadPreference_should_not_be_sent_to_standalone_server( |
| 33 | + [Values(false, true)] bool async) |
| 34 | + { |
| 35 | + var eventCapturer = new EventCapturer().Capture<CommandStartedEvent>(e => |
| 36 | + e.CommandName.Equals("find") || e.CommandName.Equals("$query")); |
| 37 | + using (var client = CreateDisposableClient(eventCapturer, ReadPreference.PrimaryPreferred)) |
| 38 | + { |
| 39 | + var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName); |
| 40 | + var collection = database.GetCollection<BsonDocument>(DriverTestConfiguration.CollectionNamespace.CollectionName); |
| 41 | + |
| 42 | + if (async) |
| 43 | + { |
| 44 | + var _ = collection.FindAsync("{ x : 2 }").GetAwaiter().GetResult(); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + var _ = collection.FindSync("{ x : 2 }"); |
| 49 | + } |
| 50 | + |
| 51 | + CommandStartedEvent sentCommand = ((CommandStartedEvent) eventCapturer.Events[0]); |
| 52 | + var serverVersion = client.Cluster.Description.Servers[0].Version; |
| 53 | + var clusterType = client.Cluster.Description.Type; |
| 54 | + |
| 55 | + var expectedContainsReadPreference = clusterType == ClusterType.Standalone || |
| 56 | + (clusterType == ClusterType.ReplicaSet && serverVersion < Feature.CommandMessage.FirstSupportedVersion) |
| 57 | + ? false |
| 58 | + : true; |
| 59 | + var readPreferenceFieldName = sentCommand.Command.Contains("$readPreference") |
| 60 | + ? "$readPreference" |
| 61 | + : "readPreference"; |
| 62 | + |
| 63 | + sentCommand.Command.Contains(readPreferenceFieldName).Should().Be(expectedContainsReadPreference); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // private methods |
| 68 | + private DisposableMongoClient CreateDisposableClient(EventCapturer eventCapturer, ReadPreference readPreference) |
| 69 | + { |
| 70 | + return DriverTestConfiguration.CreateDisposableClient((MongoClientSettings settings) => |
| 71 | + { |
| 72 | + settings.ClusterConfigurator = c => c.Subscribe(eventCapturer); |
| 73 | + settings.ReadPreference = readPreference; |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments