@@ -7,15 +7,9 @@ use tokio::sync::{mpsc, RwLock};
7
7
use crate :: {
8
8
bson:: { doc, Document } ,
9
9
client:: options:: ClientOptions ,
10
- concern:: { Acknowledgment , WriteConcern } ,
10
+ concern:: WriteConcern ,
11
11
gridfs:: GridFsBucket ,
12
- options:: {
13
- CollectionOptions ,
14
- CreateCollectionOptions ,
15
- ReadConcern ,
16
- ReadPreference ,
17
- SelectionCriteria ,
18
- } ,
12
+ options:: { CollectionOptions , ReadConcern , ReadPreference , SelectionCriteria } ,
19
13
runtime,
20
14
sdam:: { TopologyDescription , MIN_HEARTBEAT_FREQUENCY } ,
21
15
test:: {
@@ -35,6 +29,8 @@ use crate::{
35
29
SERVERLESS ,
36
30
SERVER_API ,
37
31
} ,
32
+ ClientSession ,
33
+ ClusterTime ,
38
34
Collection ,
39
35
Database ,
40
36
} ;
@@ -82,6 +78,7 @@ pub(crate) struct TestRunner {
82
78
pub ( crate ) internal_client : TestClient ,
83
79
pub ( crate ) entities : Arc < RwLock < EntityMap > > ,
84
80
pub ( crate ) fail_point_guards : Arc < RwLock < Vec < FailPointGuard > > > ,
81
+ pub ( crate ) cluster_time : Arc < RwLock < Option < ClusterTime > > > ,
85
82
}
86
83
87
84
impl TestRunner {
@@ -90,6 +87,7 @@ impl TestRunner {
90
87
internal_client : TestClient :: new ( ) . await ,
91
88
entities : Default :: default ( ) ,
92
89
fail_point_guards : Default :: default ( ) ,
90
+ cluster_time : Default :: default ( ) ,
93
91
}
94
92
}
95
93
@@ -99,6 +97,7 @@ impl TestRunner {
99
97
internal_client : TestClient :: with_options ( Some ( options) ) . await ,
100
98
entities : Arc :: new ( RwLock :: new ( EntityMap :: new ( ) ) ) ,
101
99
fail_point_guards : Arc :: new ( RwLock :: new ( Vec :: new ( ) ) ) ,
100
+ cluster_time : Default :: default ( ) ,
102
101
}
103
102
}
104
103
@@ -205,9 +204,11 @@ impl TestRunner {
205
204
log_uncaptured ( format ! ( "Executing {:?}" , & test_case. description) ) ;
206
205
207
206
if let Some ( ref initial_data) = test_file. initial_data {
207
+ let mut session = self . internal_client . start_session ( ) . await . unwrap ( ) ;
208
208
for data in initial_data {
209
- self . insert_initial_data ( data) . await ;
209
+ self . insert_initial_data ( data, & mut session ) . await ;
210
210
}
211
+ * self . cluster_time . write ( ) . await = session. cluster_time ( ) . cloned ( ) ;
211
212
}
212
213
213
214
self . entities . write ( ) . await . clear ( ) ;
@@ -370,33 +371,37 @@ impl TestRunner {
370
371
}
371
372
}
372
373
373
- pub ( crate ) async fn insert_initial_data ( & self , data : & CollectionData ) {
374
- let write_concern = WriteConcern :: builder ( ) . w ( Acknowledgment :: Majority ) . build ( ) ;
375
-
374
+ pub ( crate ) async fn insert_initial_data (
375
+ & self ,
376
+ data : & CollectionData ,
377
+ session : & mut ClientSession ,
378
+ ) {
376
379
if !data. documents . is_empty ( ) {
377
380
let collection_options = CollectionOptions :: builder ( )
378
- . write_concern ( write_concern )
381
+ . write_concern ( WriteConcern :: majority ( ) )
379
382
. build ( ) ;
383
+ let coll = self . internal_client . get_coll_with_options (
384
+ & data. database_name ,
385
+ & data. collection_name ,
386
+ collection_options,
387
+ ) ;
388
+ coll. drop ( ) . session ( & mut * session) . await . unwrap ( ) ;
389
+ coll. insert_many ( data. documents . clone ( ) )
390
+ . session ( session)
391
+ . await
392
+ . unwrap ( ) ;
393
+ } else {
380
394
let coll = self
381
395
. internal_client
382
- . init_db_and_coll_with_options (
383
- & data. database_name ,
384
- & data. collection_name ,
385
- collection_options,
386
- )
387
- . await ;
388
- coll. insert_many ( data. documents . clone ( ) ) . await . unwrap ( ) ;
389
- } else {
390
- let collection_options = CreateCollectionOptions :: builder ( )
391
- . write_concern ( write_concern)
392
- . build ( ) ;
396
+ . get_coll ( & data. database_name , & data. collection_name ) ;
397
+ coll. drop ( ) . session ( & mut * session) . await . unwrap ( ) ;
393
398
self . internal_client
394
- . create_fresh_collection (
395
- & data. database_name ,
396
- & data . collection_name ,
397
- collection_options ,
398
- )
399
- . await ;
399
+ . database ( & data . database_name )
400
+ . create_collection ( & data. collection_name )
401
+ . session ( & mut * session )
402
+ . write_concern ( WriteConcern :: majority ( ) )
403
+ . await
404
+ . unwrap ( ) ;
400
405
}
401
406
}
402
407
@@ -526,11 +531,14 @@ impl TestRunner {
526
531
TestFileEntity :: Session ( session) => {
527
532
let id = session. id . clone ( ) ;
528
533
let client = self . get_client ( & session. client ) . await ;
529
- let client_session = client
534
+ let mut client_session = client
530
535
. start_session ( )
531
536
. with_options ( session. session_options . clone ( ) )
532
537
. await
533
538
. unwrap ( ) ;
539
+ if let Some ( time) = & * self . cluster_time . read ( ) . await {
540
+ client_session. advance_cluster_time ( time) ;
541
+ }
534
542
( id, Entity :: Session ( SessionEntity :: new ( client_session) ) )
535
543
}
536
544
TestFileEntity :: Bucket ( bucket) => {
0 commit comments