2222using MongoDB . Bson ;
2323using MongoDB . Bson . TestHelpers . JsonDrivenTests ;
2424using MongoDB . Driver . Core ;
25+ using MongoDB . Driver . Core . Bindings ;
2526using MongoDB . Driver . Core . Logging ;
2627using MongoDB . Driver . Core . Misc ;
28+ using MongoDB . Driver . Core . Operations ;
29+ using MongoDB . Driver . Core . Servers ;
2730using MongoDB . Driver . Core . TestHelpers . Logging ;
2831using MongoDB . Driver . Core . TestHelpers . XunitExtensions ;
32+ using MongoDB . Driver . Core . WireProtocol . Messages . Encoders ;
2933using MongoDB . Driver . Tests . UnifiedTestOperations . Matchers ;
3034using MongoDB . TestHelpers . XunitExtensions ;
3135using Xunit . Sdk ;
@@ -85,7 +89,9 @@ public void Run(JsonDrivenTestCase testCase)
8589 var outcome = testCase . Test . GetValue ( "outcome" , null ) ? . AsBsonArray ;
8690 var async = testCase . Test [ "async" ] . AsBoolean ; // cannot be null
8791
88- Run ( schemaVersion , testSetRunOnRequirements , entities , initialData , runOnRequirements , skipReason , operations , expectEvents , expectedLogs , expectTracingMessages , outcome , async ) ;
92+ var validateInitialDataPropagation = testCase . Name . Contains ( "Aggregate with $out includes read preference for 5.0+ server" ) ;
93+
94+ Run ( schemaVersion , testSetRunOnRequirements , entities , initialData , runOnRequirements , skipReason , operations , expectEvents , expectedLogs , expectTracingMessages , outcome , async , validateInitialDataPropagation ) ;
8995 }
9096
9197 public void Run (
@@ -100,7 +106,8 @@ public void Run(
100106 BsonArray expectedLogs ,
101107 BsonArray expectTracingMessages ,
102108 BsonArray outcome ,
103- bool async )
109+ bool async ,
110+ bool validateInitialDataPropagation )
104111 {
105112 if ( _runHasBeenCalled )
106113 {
@@ -127,7 +134,7 @@ public void Run(
127134 throw new SkipException ( $ "Test skipped because '{ skipReason } '.") ;
128135 }
129136
130- var lastKnownClusterTime = AddInitialData ( DriverTestConfiguration . Client , initialData ) ;
137+ var lastKnownClusterTime = AddInitialData ( DriverTestConfiguration . Client , initialData , validateInitialDataPropagation ) ;
131138 _entityMap = UnifiedEntityMap . Create ( _eventFormatters , _loggingService . LoggingSettings , async, lastKnownClusterTime ) ;
132139 _entityMap . AddRange ( entities ) ;
133140
@@ -176,7 +183,7 @@ public void Dispose()
176183 }
177184
178185 // private methods
179- private BsonDocument AddInitialData ( IMongoClient client , BsonArray initialData )
186+ private BsonDocument AddInitialData ( IMongoClient client , BsonArray initialData , bool validateInitialDataPropagation )
180187 {
181188 if ( initialData == null )
182189 {
@@ -210,7 +217,7 @@ private BsonDocument AddInitialData(IMongoClient client, BsonArray initialData)
210217 }
211218
212219 _logger . LogDebug ( "Dropping {0}" , collectionName ) ;
213- using var session = client . StartSession ( ) ;
220+ using var session = client . StartSession ( new ClientSessionOptions { CausalConsistency = true } ) ;
214221
215222 // For some QE spec tests we need to drop QE state collections (enxcol_.*.esc, enxcol_.*.ecoc).
216223 // DropCollection with EncryptedFields automatically handles cleanup of those QE state collections
@@ -224,6 +231,22 @@ private BsonDocument AddInitialData(IMongoClient client, BsonArray initialData)
224231 collection . InsertMany ( session , documents ) ;
225232 }
226233
234+ // do read from all secondaries in case of replica set to make sure the data is available there already
235+ if ( validateInitialDataPropagation )
236+ {
237+ var cluster = client . GetClusterInternal ( ) ;
238+ if ( DriverTestConfiguration . IsReplicaSet ( cluster ) )
239+ {
240+ var collection = database . GetCollection < BsonDocument > ( collectionName ) ;
241+ var runCommandOperation = new CountOperation ( collection . CollectionNamespace , new MessageEncoderSettings ( ) ) { ReadConcern = ReadConcern . Local , } ;
242+ foreach ( var server in cluster . Description . Servers . Where ( s => s . Type == ServerType . ReplicaSetSecondary ) )
243+ {
244+ using var singleServerBinding = new SingleServerReadBinding ( cluster , server . EndPoint , ReadPreference . Secondary , session . WrappedCoreSession . Fork ( ) ) ;
245+ runCommandOperation . Execute ( OperationContext . NoTimeout , singleServerBinding ) ;
246+ }
247+ }
248+ }
249+
227250 lastKnownClusterTime = session . ClusterTime ;
228251 }
229252
@@ -422,4 +445,4 @@ private void KillOpenTransactions(IMongoClient client)
422445 }
423446
424447 }
425- }
448+ }
0 commit comments