Skip to content

Commit 941a018

Browse files
authored
CSHARP-4017: Fix flaky Aggregate_with__out_includes_read_preference_for_5.0__server error (#1379)
1 parent ddf36cf commit 941a018

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

tests/MongoDB.Driver.Tests/UnifiedTestOperations/UnifiedTestRunner.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using MongoDB.Bson;
2323
using MongoDB.Bson.TestHelpers.JsonDrivenTests;
2424
using MongoDB.Driver.Core;
25+
using MongoDB.Driver.Core.Clusters;
2526
using MongoDB.Driver.Core.Logging;
2627
using MongoDB.Driver.Core.Misc;
2728
using MongoDB.Driver.Core.TestHelpers;
@@ -195,29 +196,35 @@ private void AddInitialData(IMongoClient client, BsonArray initialData, UnifiedE
195196
}
196197
#pragma warning restore CS0618 // Type or member is obsolete
197198

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+
198208
BsonDocument serverTime = null;
199209
foreach (var dataItem in initialData)
200210
{
201211
var collectionName = dataItem["collectionName"].AsString;
202212
var databaseName = dataItem["databaseName"].AsString;
203213
var documents = dataItem["documents"].AsBsonArray.Cast<BsonDocument>().ToList();
204214

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);
209217

210218
_logger.LogDebug("Dropping {0}", collectionName);
211-
212-
database.DropCollection(collectionName);
213219
var session = client.StartSession();
220+
database.DropCollection(session, collectionName);
214221
if (documents.Any())
215222
{
216223
collection.InsertMany(session, documents);
217224
}
218225
else
219226
{
220-
database.WithWriteConcern(WriteConcern.WMajority).CreateCollection(session, collectionName);
227+
database.CreateCollection(session, collectionName);
221228
}
222229

223230
serverTime = session.ClusterTime;

0 commit comments

Comments
 (0)