@@ -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 ;
0 commit comments