forked from mongodb/mongo-csharp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriverTestConfiguration.cs
266 lines (231 loc) · 10.7 KB
/
DriverTestConfiguration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.Extensions.Logging;
using MongoDB.Driver.Core;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.Configuration;
using MongoDB.Driver.Core.Logging;
using MongoDB.Driver.Core.Servers;
using MongoDB.Driver.Linq;
using MongoDB.Driver.TestHelpers;
namespace MongoDB.Driver.Tests
{
/// <summary>
/// A static class to handle online test configuration.
/// </summary>
public static class DriverTestConfiguration
{
// private static fields
private static Lazy<MongoClient> __clientWithMultipleShardRouters;
private static CollectionNamespace __collectionNamespace;
private static DatabaseNamespace __databaseNamespace;
private static Lazy<IReadOnlyList<IMongoClient>> __directClientsToShardRouters;
private static Lazy<MongoClient> __linq2Client;
private static Lazy<MongoClient> __linq3Client;
// static constructor
static DriverTestConfiguration()
{
__linq2Client = new Lazy<MongoClient>(CreateLinq2Client, isThreadSafe: true);
__linq3Client = new Lazy<MongoClient>(CreateLinq3Client, isThreadSafe: true);
__clientWithMultipleShardRouters = new Lazy<MongoClient>(() => CreateClient(useMultipleShardRouters: true), true);
__databaseNamespace = CoreTestConfiguration.DatabaseNamespace;
__directClientsToShardRouters = new Lazy<IReadOnlyList<IMongoClient>>(
() => CreateDirectClientsToHostsInConnectionString(CoreTestConfiguration.ConnectionStringWithMultipleShardRouters).ToList().AsReadOnly(),
isThreadSafe: true);
__collectionNamespace = new CollectionNamespace(__databaseNamespace, "testcollection");
}
// public static properties
/// <summary>
/// Gets the test client.
/// </summary>
public static MongoClient Client
{
get { return Linq3Client; }
}
/// <summary>
/// Gets the test client with multiple shard routers.
/// </summary>
public static MongoClient ClientWithMultipleShardRouters
{
get { return __clientWithMultipleShardRouters.Value; }
}
/// <summary>
/// Sequence of clients that connect directly to the shard routers
/// </summary>
public static IReadOnlyList<IMongoClient> DirectClientsToShardRouters
{
get => __directClientsToShardRouters.Value;
}
/// <summary>
/// Gets the collection namespace.
/// </summary>
/// <value>
/// The collection namespace.
/// </value>
public static CollectionNamespace CollectionNamespace
{
get { return __collectionNamespace; }
}
/// <summary>
/// Gets the database namespace.
/// </summary>
/// <value>
/// The database namespace.
/// </value>
public static DatabaseNamespace DatabaseNamespace
{
get { return __databaseNamespace; }
}
/// <summary>
/// Gets the LINQ2 test client.
/// </summary>
public static MongoClient Linq2Client
{
get { return __linq2Client.Value; }
}
/// <summary>
/// Gets the LINQ3 test client.
/// </summary>
public static MongoClient Linq3Client
{
get { return __linq3Client.Value; }
}
// public static methods
public static IEnumerable<IMongoClient> CreateDirectClientsToServersInClientSettings(MongoClientSettings settings)
{
foreach (var server in settings.Servers)
{
var singleServerSettings = settings.Clone();
singleServerSettings.Server = server;
yield return new MongoClient(singleServerSettings);
}
}
public static IEnumerable<IMongoClient> CreateDirectClientsToHostsInConnectionString(ConnectionString connectionString)
{
return CreateDirectClientsToServersInClientSettings(MongoClientSettings.FromConnectionString(connectionString.ToString()));
}
public static DisposableMongoClient CreateDisposableClient(LoggingSettings loggingSettings = null)
{
return CreateDisposableClient((MongoClientSettings s) => { }, loggingSettings);
}
public static DisposableMongoClient CreateDisposableClient(Action<ClusterBuilder> clusterConfigurator, LoggingSettings loggingSettings = null)
{
return CreateDisposableClient((MongoClientSettings s) => s.ClusterConfigurator = clusterConfigurator, loggingSettings);
}
public static MongoClient CreateClient(
Action<MongoClientSettings> clientSettingsConfigurator = null,
bool useMultipleShardRouters = false)
{
var clusterType = CoreTestConfiguration.Cluster.Description.Type;
if (clusterType != ClusterType.Sharded && clusterType != ClusterType.LoadBalanced)
{
// This option has no effect for non-sharded/load balanced topologies.
useMultipleShardRouters = false;
}
var connectionString = useMultipleShardRouters
? CoreTestConfiguration.ConnectionStringWithMultipleShardRouters.ToString()
: CoreTestConfiguration.ConnectionString.ToString();
var clientSettings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
clientSettings.ServerApi = CoreTestConfiguration.ServerApi;
clientSettingsConfigurator?.Invoke(clientSettings);
return new MongoClient(clientSettings);
}
public static DisposableMongoClient CreateDisposableClient(
Action<MongoClientSettings> clientSettingsConfigurator,
LoggingSettings loggingSettings,
bool useMultipleShardRouters = false)
{
Action<MongoClientSettings> compositeClientSettingsConfigurator = s =>
{
EnsureUniqueCluster(s);
s.LoggingSettings = loggingSettings;
clientSettingsConfigurator?.Invoke(s);
};
var client = CreateClient(compositeClientSettingsConfigurator, useMultipleShardRouters);
return new DisposableMongoClient(client, loggingSettings.ToInternalLoggerFactory()?.CreateLogger<DisposableMongoClient>());
}
public static DisposableMongoClient CreateDisposableClient(EventCapturer capturer, LoggingSettings loggingSettings = null)
{
return CreateDisposableClient((ClusterBuilder c) => c.Subscribe(capturer), loggingSettings);
}
public static DisposableMongoClient CreateDisposableClient(MongoClientSettings settings)
{
EnsureUniqueCluster(settings);
return new DisposableMongoClient(new MongoClient(settings), settings.LoggingSettings.ToInternalLoggerFactory()?.CreateLogger<DisposableMongoClient>());
}
private static MongoClient CreateLinq2Client()
{
var linq2ClientSettings = GetClientSettings();
linq2ClientSettings.LinqProvider = LinqProvider.V2;
return new MongoClient(linq2ClientSettings);
}
private static MongoClient CreateLinq3Client()
{
var linq3ClientSettings = GetClientSettings();
linq3ClientSettings.LinqProvider = LinqProvider.V3;
return new MongoClient(linq3ClientSettings);
}
public static MongoClientSettings GetClientSettings()
{
var connectionString = CoreTestConfiguration.ConnectionString.ToString();
var clientSettings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
var serverSelectionTimeoutString = Environment.GetEnvironmentVariable("MONGO_SERVER_SELECTION_TIMEOUT_MS");
if (serverSelectionTimeoutString == null)
{
serverSelectionTimeoutString = "30000";
}
clientSettings.ServerSelectionTimeout = TimeSpan.FromMilliseconds(int.Parse(serverSelectionTimeoutString));
clientSettings.ClusterConfigurator = cb => CoreTestConfiguration.ConfigureLogging(cb);
clientSettings.ServerApi = CoreTestConfiguration.ServerApi;
return clientSettings;
}
public static MongoClient GetLinqClient(LinqProvider linqProvider)
{
return linqProvider == LinqProvider.V2 ? Linq2Client : Linq3Client;
}
public static bool IsReplicaSet(IMongoClient client)
{
var clusterTypeIsKnown = SpinWait.SpinUntil(() => client.Cluster.Description.Type != ClusterType.Unknown, TimeSpan.FromSeconds(10));
if (!clusterTypeIsKnown)
{
throw new InvalidOperationException($"Unable to determine cluster type: {client.Cluster.Description}.");
}
return client.Cluster.Description.Type == ClusterType.ReplicaSet;
}
public static int GetReplicaSetNumberOfDataBearingMembers(IMongoClient client)
{
if (!IsReplicaSet(client))
{
throw new InvalidOperationException($"Cluster is not a replica set: {client.Cluster.Description}.");
}
var allServersAreConnected = SpinWait.SpinUntil(() => client.Cluster.Description.Servers.All(s => s.State == ServerState.Connected), TimeSpan.FromSeconds(10));
if (!allServersAreConnected)
{
throw new InvalidOperationException($"Unable to connect to all members of the replica set: {client.Cluster.Description}.");
}
return client.Cluster.Description.Servers.Count(s => s.IsDataBearing);
}
private static void EnsureUniqueCluster(MongoClientSettings settings)
{
// make the settings unique (as far as the ClusterKey is concerned) by instantiating a new ClusterConfigurator
var configurator = settings.ClusterConfigurator;
settings.ClusterConfigurator = b => { configurator?.Invoke(b); };
}
}
}