|
22 | 22 | using MongoDB.Bson;
|
23 | 23 | using MongoDB.Bson.TestHelpers.JsonDrivenTests;
|
24 | 24 | using MongoDB.Driver.Core;
|
| 25 | +using MongoDB.Driver.Core.Clusters; |
25 | 26 | using MongoDB.Driver.Core.Logging;
|
26 | 27 | using MongoDB.Driver.Core.Misc;
|
27 | 28 | using MongoDB.Driver.Core.TestHelpers;
|
@@ -195,29 +196,35 @@ private void AddInitialData(IMongoClient client, BsonArray initialData, UnifiedE
|
195 | 196 | }
|
196 | 197 | #pragma warning restore CS0618 // Type or member is obsolete
|
197 | 198 |
|
| 199 | + var writeConcern = WriteConcern.WMajority; |
| 200 | + if (client.Cluster.Description.Type == ClusterType.ReplicaSet) |
| 201 | + { |
| 202 | + // Makes server to wait for ack from all data nodes to make sure the test data availability before running the test itself. |
| 203 | + // It's limited to replica set only because there is no simple way to calculate proper w for sharded cluster. |
| 204 | + var dataBearingServersCount = client.Cluster.Description.Servers.Count(s => s.IsDataBearing); |
| 205 | + writeConcern = WriteConcern.Acknowledged.With(w: dataBearingServersCount, journal:true); |
| 206 | + } |
| 207 | + |
198 | 208 | BsonDocument serverTime = null;
|
199 | 209 | foreach (var dataItem in initialData)
|
200 | 210 | {
|
201 | 211 | var collectionName = dataItem["collectionName"].AsString;
|
202 | 212 | var databaseName = dataItem["databaseName"].AsString;
|
203 | 213 | var documents = dataItem["documents"].AsBsonArray.Cast<BsonDocument>().ToList();
|
204 | 214 |
|
205 |
| - var database = client.GetDatabase(databaseName); |
206 |
| - var collection = database |
207 |
| - .GetCollection<BsonDocument>(collectionName, mongoCollectionSettings) |
208 |
| - .WithWriteConcern(WriteConcern.WMajority); |
| 215 | + var database = client.GetDatabase(databaseName).WithWriteConcern(writeConcern); |
| 216 | + var collection = database.GetCollection<BsonDocument>(collectionName, mongoCollectionSettings); |
209 | 217 |
|
210 | 218 | _logger.LogDebug("Dropping {0}", collectionName);
|
211 |
| - |
212 |
| - database.DropCollection(collectionName); |
213 | 219 | var session = client.StartSession();
|
| 220 | + database.DropCollection(session, collectionName); |
214 | 221 | if (documents.Any())
|
215 | 222 | {
|
216 | 223 | collection.InsertMany(session, documents);
|
217 | 224 | }
|
218 | 225 | else
|
219 | 226 | {
|
220 |
| - database.WithWriteConcern(WriteConcern.WMajority).CreateCollection(session, collectionName); |
| 227 | + database.CreateCollection(session, collectionName); |
221 | 228 | }
|
222 | 229 |
|
223 | 230 | serverTime = session.ClusterTime;
|
|
0 commit comments