Skip to content

Commit 415ce38

Browse files
authored
CSHARP-5545: Fix flaky test .TransactionsConvenientApi_testCase__transaction-options.json_withTransaction_explicit_transaction_options_override_client_options (mongodb#1654)
1 parent e98e457 commit 415ce38

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

src/MongoDB.Driver/Core/Clusters/ServerSelectors/WritableServerSelector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public IEnumerable<ServerDescription> SelectServers(ClusterDescription cluster,
7979
var serversList = servers.ToList(); // avoid multiple enumeration
8080
if (CanUseSecondaries(cluster, serversList))
8181
{
82+
_mayUseSecondary.EffectiveReadPreference = _mayUseSecondary.ReadPreference;
8283
var readPreferenceSelector = new ReadPreferenceServerSelector(_mayUseSecondary.ReadPreference);
8384
return readPreferenceSelector.SelectServers(cluster, serversList);
8485
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Collections;
1818
using System.Collections.Generic;
1919
using System.Linq;
20+
using System.Threading;
2021
using System.Threading.Tasks;
2122
using MongoDB.Bson;
2223
using MongoDB.Driver.Authentication.Oidc;
@@ -38,15 +39,16 @@ namespace MongoDB.Driver.Tests.UnifiedTestOperations
3839
public sealed class UnifiedEntityMap : IDisposable
3940
{
4041
#region static
41-
public static UnifiedEntityMap Create(Dictionary<string, IEventFormatter> eventFormatters, LoggingSettings loggingSettings, bool async)
42-
=> new(eventFormatters, loggingSettings, async);
42+
public static UnifiedEntityMap Create(Dictionary<string, IEventFormatter> eventFormatters, LoggingSettings loggingSettings, bool async, BsonDocument lastKnownClusterTime)
43+
=> new(eventFormatters, loggingSettings, async, lastKnownClusterTime);
4344

4445
#endregion
4546

4647
// private fields
4748
private readonly bool _async;
4849
private readonly Dictionary<string, IEventFormatter> _eventFormatters;
4950
private readonly LoggingSettings _loggingSettings;
51+
private readonly BsonDocument _lastKnownClusterTime;
5052

5153
private readonly Dictionary<string, IGridFSBucket> _buckets = new();
5254
private readonly Dictionary<string, IEnumerator<ChangeStreamDocument<BsonDocument>>> _changeStreams = new();
@@ -73,11 +75,13 @@ public static UnifiedEntityMap Create(Dictionary<string, IEventFormatter> eventF
7375
private UnifiedEntityMap(
7476
Dictionary<string, IEventFormatter> eventFormatters,
7577
LoggingSettings loggingSettings,
76-
bool async)
78+
bool async,
79+
BsonDocument lastKnownClusterTime)
7780
{
7881
_eventFormatters = eventFormatters ?? new();
7982
_loggingSettings = loggingSettings;
8083
_async = async;
84+
_lastKnownClusterTime = lastKnownClusterTime;
8185
}
8286

8387
// public properties
@@ -1011,6 +1015,10 @@ private IClientSessionHandle CreateSession(BsonDocument entity, Dictionary<strin
10111015
}
10121016

10131017
var session = client.StartSession(options);
1018+
if (_lastKnownClusterTime != null)
1019+
{
1020+
session.WrappedCoreSession.AdvanceClusterTime(_lastKnownClusterTime);
1021+
}
10141022

10151023
return session;
10161024
}

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

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,10 @@ public void Run(
132132
KillOpenTransactions(DriverTestConfiguration.Client);
133133
}
134134

135-
_entityMap = UnifiedEntityMap.Create(_eventFormatters, _loggingService.LoggingSettings, async);
135+
BsonDocument lastKnownClusterTime = AddInitialData(DriverTestConfiguration.Client, initialData);
136+
_entityMap = UnifiedEntityMap.Create(_eventFormatters, _loggingService.LoggingSettings, async, lastKnownClusterTime);
136137
_entityMap.AddRange(entities);
137138

138-
if (initialData != null)
139-
{
140-
AddInitialData(DriverTestConfiguration.Client, initialData, _entityMap);
141-
}
142-
143139
foreach (var operation in operations)
144140
{
145141
var cancellationToken = CancellationToken.None;
@@ -181,45 +177,36 @@ public void Dispose()
181177
}
182178

183179
// private methods
184-
private void AddInitialData(IMongoClient client, BsonArray initialData, UnifiedEntityMap entityMap)
180+
private BsonDocument AddInitialData(IMongoClient client, BsonArray initialData)
185181
{
186-
var mongoCollectionSettings = new MongoCollectionSettings();
187-
188-
var writeConcern = WriteConcern.WMajority;
189-
if (DriverTestConfiguration.IsReplicaSet(client))
182+
if (initialData == null)
190183
{
191-
// Makes server to wait for ack from all data nodes to make sure the test data availability before running the test itself.
192-
// It's limited to replica set only because there is no simple way to calculate proper w for sharded cluster.
193-
var dataBearingServersCount = DriverTestConfiguration.GetReplicaSetNumberOfDataBearingMembers(client);
194-
writeConcern = WriteConcern.Acknowledged.With(w: dataBearingServersCount, journal:true);
184+
return null;
195185
}
196186

197-
BsonDocument serverTime = null;
187+
BsonDocument lastKnownClusterTime = null;
198188
foreach (var dataItem in initialData)
199189
{
200190
var collectionName = dataItem["collectionName"].AsString;
201191
var databaseName = dataItem["databaseName"].AsString;
202192
var documents = dataItem["documents"].AsBsonArray.Cast<BsonDocument>().ToList();
203193

204-
var database = client.GetDatabase(databaseName).WithWriteConcern(writeConcern);
205-
var collection = database.GetCollection<BsonDocument>(collectionName, mongoCollectionSettings);
194+
var database = client.GetDatabase(databaseName).WithWriteConcern(WriteConcern.WMajority);
206195

207196
_logger.LogDebug("Dropping {0}", collectionName);
208-
var session = client.StartSession();
197+
using var session = client.StartSession();
209198
database.DropCollection(session, collectionName);
199+
database.CreateCollection(session, collectionName);
210200
if (documents.Any())
211201
{
202+
var collection = database.GetCollection<BsonDocument>(collectionName);
212203
collection.InsertMany(session, documents);
213204
}
214-
else
215-
{
216-
database.CreateCollection(session, collectionName);
217-
}
218205

219-
serverTime = session.ClusterTime;
206+
lastKnownClusterTime = session.ClusterTime;
220207
}
221208

222-
entityMap.AdjustSessionsClusterTime(serverTime);
209+
return lastKnownClusterTime;
223210
}
224211

225212
private void AssertEvents(BsonArray eventItems, UnifiedEntityMap entityMap)

0 commit comments

Comments
 (0)