Skip to content

Commit 2455a68

Browse files
CSHARP-4069: Fix flaky Misbehaved_async_method_should_not_deadlock_server_selection test. (#742)
1 parent 21f8c16 commit 2455a68

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

tests/MongoDB.Driver.Core.TestHelpers/CoreTestConfiguration.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static class CoreTestConfiguration
4343
private static Lazy<ConnectionString> __connectionStringWithMultipleShardRouters = new Lazy<ConnectionString>(
4444
GetConnectionStringWithMultipleShardRouters, isThreadSafe: true);
4545
private static Lazy<DatabaseNamespace> __databaseNamespace = new Lazy<DatabaseNamespace>(GetDatabaseNamespace, isThreadSafe: true);
46+
private static Lazy<TimeSpan> __defaultServerSelectionTimeout = new Lazy<TimeSpan>(GetDefaultServerSelectionTimeout, isThreadSafe: true);
4647
private static Lazy<int> __maxWireVersion = new Lazy<int>(GetMaxWireVersion, isThreadSafe: true);
4748
private static MessageEncoderSettings __messageEncoderSettings = new MessageEncoderSettings();
4849
private static Lazy<int> __numberOfMongoses = new Lazy<int>(GetNumberOfMongoses, isThreadSafe: true);
@@ -75,6 +76,21 @@ public static DatabaseNamespace DatabaseNamespace
7576
get { return __databaseNamespace.Value; }
7677
}
7778

79+
public static ICluster ClusterWithConnectedPrimary
80+
{
81+
get
82+
{
83+
var timeout = __defaultServerSelectionTimeout.Value;
84+
var cluster = __cluster.Value;
85+
if (!SpinWait.SpinUntil(() => cluster.Description.Servers.Any(s => s.Type == ServerType.ReplicaSetPrimary), timeout))
86+
{
87+
throw new Exception($"The cluster didn't find a primary during {timeout}. The current cluster description: {cluster.Description}.");
88+
}
89+
90+
return cluster;
91+
}
92+
}
93+
7894
public static MessageEncoderSettings MessageEncoderSettings
7995
{
8096
get { return __messageEncoderSettings; }
@@ -116,15 +132,9 @@ public static ClusterBuilder ConfigureCluster()
116132

117133
public static ClusterBuilder ConfigureCluster(ClusterBuilder builder)
118134
{
119-
var serverSelectionTimeoutString = Environment.GetEnvironmentVariable("MONGO_SERVER_SELECTION_TIMEOUT_MS");
120-
if (serverSelectionTimeoutString == null)
121-
{
122-
serverSelectionTimeoutString = "30000";
123-
}
124-
125135
builder = builder
126136
.ConfigureWithConnectionString(__connectionString.Value, __serverApi.Value)
127-
.ConfigureCluster(c => c.With(serverSelectionTimeout: TimeSpan.FromMilliseconds(int.Parse(serverSelectionTimeoutString))));
137+
.ConfigureCluster(c => c.With(serverSelectionTimeout: __defaultServerSelectionTimeout.Value));
128138

129139
if (__connectionString.Value.Tls.HasValue &&
130140
__connectionString.Value.Tls.Value &&
@@ -446,6 +456,17 @@ private static int GetNumberOfMongoses()
446456
}
447457
}
448458

459+
private static TimeSpan GetDefaultServerSelectionTimeout()
460+
{
461+
var serverSelectionTimeoutString = Environment.GetEnvironmentVariable("MONGO_SERVER_SELECTION_TIMEOUT_MS");
462+
if (serverSelectionTimeoutString == null)
463+
{
464+
serverSelectionTimeoutString = "30000";
465+
}
466+
467+
return TimeSpan.FromMilliseconds(int.Parse(serverSelectionTimeoutString));
468+
}
469+
449470
private static string GetStorageEngine()
450471
{
451472
string result;

tests/MongoDB.Driver.Tests/Jira/CSharp2564Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public async Task Misbehaved_async_method_should_not_deadlock_server_selection()
3939
// but then the sync call to RunCommand blocks waiting for a primary and the call to TrySetResult never returns
4040
// which in turn prevents SDAM from unwinding back to process the next queued heartbeat event so the primary is never found
4141

42-
var primary = CoreTestConfiguration.Cluster.Description.Servers.Where(s => s.Type == ServerType.ReplicaSetPrimary).Single();
42+
var primary = CoreTestConfiguration.ClusterWithConnectedPrimary.Description.Servers.Where(s => s.Type == ServerType.ReplicaSetPrimary).Single();
4343
void clusterConfigurator(ClusterBuilder builder)
4444
{
4545
builder.Subscribe((ServerHeartbeatSucceededEvent heartbeatEvent) =>

0 commit comments

Comments
 (0)