@@ -3485,19 +3485,21 @@ test_numerous_unordered (void *ctx)
3485
3485
3486
3486
3487
3487
static void
3488
- test_bulk_split (void )
3488
+ test_bulk_split (void * ctx )
3489
3489
{
3490
+ BSON_UNUSED (ctx );
3491
+
3490
3492
mongoc_client_t * client ;
3491
3493
mongoc_collection_t * collection ;
3492
3494
bson_t opts = BSON_INITIALIZER ;
3493
3495
mongoc_bulk_operation_t * bulk_op ;
3494
- mongoc_write_concern_t * wc = mongoc_write_concern_new ();
3495
3496
bson_iter_t iter , error_iter , indexnum ;
3496
3497
bson_t doc , result ;
3497
3498
bson_error_t error ;
3498
3499
int n_docs ;
3499
3500
int i ;
3500
3501
uint32_t r ;
3502
+ mongoc_client_session_t * session ;
3501
3503
3502
3504
/* ensure we need two batches */
3503
3505
n_docs = (int ) test_framework_max_write_batch_size () + 10 ;
@@ -3508,10 +3510,35 @@ test_bulk_split (void)
3508
3510
collection = get_test_collection (client , "split" );
3509
3511
BSON_ASSERT (collection );
3510
3512
3511
- mongoc_write_concern_set_w (wc , 1 );
3513
+ // Apply settings to guarantee "read-your-own-writes" semantics.
3514
+ // Intended to address undercounts reading results reported in CDRIVER-4346.
3515
+ {
3516
+ // https://www.mongodb.com/docs/manual/core/read-isolation-consistency-recency/#client-sessions-and-causal-consistency-guarantees
3517
+ // describes how to guarantee "read-your-own-writes".
3518
+
3519
+ // Start a causally consistent session.
3520
+ mongoc_session_opt_t * sopts = mongoc_session_opts_new ();
3521
+ mongoc_session_opts_set_causal_consistency (sopts , true);
3522
+ session = mongoc_client_start_session (client , sopts , NULL );
3523
+ mongoc_session_opts_destroy (sopts );
3524
+
3525
+ // Apply read concern majority.
3526
+ mongoc_read_concern_t * rc = mongoc_read_concern_new ();
3527
+ mongoc_read_concern_set_level (rc , MONGOC_READ_CONCERN_LEVEL_MAJORITY );
3528
+ mongoc_collection_set_read_concern (collection , rc );
3529
+ mongoc_read_concern_destroy (rc );
3530
+
3531
+ // Apply write concern majority.
3532
+ mongoc_write_concern_t * wc = mongoc_write_concern_new ();
3533
+ mongoc_write_concern_set_w (wc , MONGOC_WRITE_CONCERN_W_MAJORITY );
3534
+ mongoc_collection_set_write_concern (collection , wc );
3535
+ mongoc_write_concern_destroy (wc );
3536
+ }
3537
+
3512
3538
3513
- mongoc_write_concern_append (wc , & opts );
3514
3539
bson_append_bool (& opts , "ordered" , 7 , false);
3540
+ ASSERT_OR_PRINT (mongoc_client_session_append (session , & opts , & error ),
3541
+ error );
3515
3542
bulk_op =
3516
3543
mongoc_collection_create_bulk_operation_with_opts (collection , & opts );
3517
3544
@@ -3548,7 +3575,20 @@ test_bulk_split (void)
3548
3575
BSON_ASSERT (!r );
3549
3576
3550
3577
/* all 100,010 docs were inserted, either by the first or second bulk op */
3551
- ASSERT_COUNT (n_docs , collection );
3578
+ {
3579
+ bson_t count_opts = BSON_INITIALIZER ;
3580
+ ASSERT_OR_PRINT (
3581
+ mongoc_client_session_append (session , & count_opts , & error ), error );
3582
+ int64_t count = mongoc_collection_count_documents (collection ,
3583
+ tmp_bson ("{}" ),
3584
+ & count_opts ,
3585
+ NULL /* read_prefs */ ,
3586
+ NULL /* reply */ ,
3587
+ & error );
3588
+ ASSERT_OR_PRINT (count != -1 , error );
3589
+ ASSERT_CMPINT64 (count , = = , 100010 );
3590
+ bson_destroy (& count_opts );
3591
+ }
3552
3592
3553
3593
/* result like {writeErrors: [{index: i, code: n, errmsg: ''}, ... ]} */
3554
3594
bson_iter_init_find (& iter , & result , "writeErrors" );
@@ -3574,9 +3614,8 @@ test_bulk_split (void)
3574
3614
bson_destroy (& opts );
3575
3615
bson_destroy (& result );
3576
3616
3577
- mongoc_write_concern_destroy (wc );
3578
-
3579
3617
mongoc_collection_destroy (collection );
3618
+ mongoc_client_session_destroy (session );
3580
3619
mongoc_client_destroy (client );
3581
3620
}
3582
3621
@@ -5293,7 +5332,12 @@ test_bulk_install (TestSuite *suite)
5293
5332
suite , "/BulkOperation/OP_MSG/max_batch_size" , test_bulk_max_batch_size );
5294
5333
TestSuite_AddLive (
5295
5334
suite , "/BulkOperation/OP_MSG/max_msg_size" , test_bulk_max_msg_size );
5296
- TestSuite_AddLive (suite , "/BulkOperation/split" , test_bulk_split );
5335
+ TestSuite_AddFull (suite ,
5336
+ "/BulkOperation/split" ,
5337
+ test_bulk_split ,
5338
+ NULL /* dtor */ ,
5339
+ NULL /* ctx */ ,
5340
+ test_framework_skip_if_no_sessions );
5297
5341
TestSuite_AddFull (suite ,
5298
5342
"/BulkOperation/write_concern/split" ,
5299
5343
test_bulk_write_concern_split ,
0 commit comments