@@ -43,6 +43,7 @@ public static class CoreTestConfiguration
43
43
private static Lazy < ConnectionString > __connectionStringWithMultipleShardRouters = new Lazy < ConnectionString > (
44
44
GetConnectionStringWithMultipleShardRouters , isThreadSafe : true ) ;
45
45
private static Lazy < DatabaseNamespace > __databaseNamespace = new Lazy < DatabaseNamespace > ( GetDatabaseNamespace , isThreadSafe : true ) ;
46
+ private static Lazy < TimeSpan > __defaultServerSelectionTimeout = new Lazy < TimeSpan > ( GetDefaultServerSelectionTimeout , isThreadSafe : true ) ;
46
47
private static Lazy < int > __maxWireVersion = new Lazy < int > ( GetMaxWireVersion , isThreadSafe : true ) ;
47
48
private static MessageEncoderSettings __messageEncoderSettings = new MessageEncoderSettings ( ) ;
48
49
private static Lazy < int > __numberOfMongoses = new Lazy < int > ( GetNumberOfMongoses , isThreadSafe : true ) ;
@@ -75,6 +76,21 @@ public static DatabaseNamespace DatabaseNamespace
75
76
get { return __databaseNamespace . Value ; }
76
77
}
77
78
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
+
78
94
public static MessageEncoderSettings MessageEncoderSettings
79
95
{
80
96
get { return __messageEncoderSettings ; }
@@ -116,15 +132,9 @@ public static ClusterBuilder ConfigureCluster()
116
132
117
133
public static ClusterBuilder ConfigureCluster ( ClusterBuilder builder )
118
134
{
119
- var serverSelectionTimeoutString = Environment . GetEnvironmentVariable ( "MONGO_SERVER_SELECTION_TIMEOUT_MS" ) ;
120
- if ( serverSelectionTimeoutString == null )
121
- {
122
- serverSelectionTimeoutString = "30000" ;
123
- }
124
-
125
135
builder = builder
126
136
. ConfigureWithConnectionString ( __connectionString . Value , __serverApi . Value )
127
- . ConfigureCluster ( c => c . With ( serverSelectionTimeout : TimeSpan . FromMilliseconds ( int . Parse ( serverSelectionTimeoutString ) ) ) ) ;
137
+ . ConfigureCluster ( c => c . With ( serverSelectionTimeout : __defaultServerSelectionTimeout . Value ) ) ;
128
138
129
139
if ( __connectionString . Value . Tls . HasValue &&
130
140
__connectionString . Value . Tls . Value &&
@@ -446,6 +456,17 @@ private static int GetNumberOfMongoses()
446
456
}
447
457
}
448
458
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
+
449
470
private static string GetStorageEngine ( )
450
471
{
451
472
string result ;
0 commit comments