@@ -45,7 +45,6 @@ test_transactions_cb (bson_t *scenario)
45
45
static void
46
46
test_transactions_supported (void * ctx )
47
47
{
48
- bool supported ;
49
48
mongoc_client_t * client ;
50
49
mongoc_client_session_t * session ;
51
50
mongoc_database_t * db ;
@@ -59,8 +58,6 @@ test_transactions_supported (void *ctx)
59
58
return ;
60
59
}
61
60
62
- supported = test_framework_max_wire_version_at_least (7 ) &&
63
- test_framework_is_replset ();
64
61
client = test_framework_client_new ();
65
62
mongoc_client_set_error_api (client , 2 );
66
63
db = mongoc_client_get_database (client , "transaction-tests" );
@@ -75,29 +72,24 @@ test_transactions_supported (void *ctx)
75
72
session = mongoc_client_start_session (client , NULL , & error );
76
73
ASSERT_OR_PRINT (session , error );
77
74
78
- /* Transactions Spec says "startTransaction SHOULD report an error if the
79
- * driver can detect that transactions are not supported by the deployment",
80
- * but we take advantage of the wiggle room and don't error here. */
81
- r = mongoc_client_session_start_transaction (session , NULL , & error );
82
- ASSERT_OR_PRINT (r , error );
75
+ if ((r = mongoc_client_session_start_transaction (session , NULL , & error ))) {
76
+ r = mongoc_client_session_append (session , & opts , & error );
77
+ ASSERT_OR_PRINT (r , error );
83
78
84
- r = mongoc_client_session_append (session , & opts , & error );
85
- ASSERT_OR_PRINT (r , error );
86
- r = mongoc_collection_insert_one (
87
- collection , tmp_bson ("{}" ), & opts , NULL , & error );
79
+ r = mongoc_collection_insert_one (
80
+ collection , tmp_bson ("{}" ), & opts , NULL , & error );
88
81
89
- if ( supported ) {
90
- ASSERT_OR_PRINT (r , error );
82
+ /* insert should fail if replset has no members */
83
+ BSON_ASSERT (r == test_framework_is_replset () );
91
84
} else {
92
- BSON_ASSERT (!r );
93
- ASSERT_CMPINT32 (error .domain , = = , MONGOC_ERROR_SERVER );
85
+ ASSERT_CMPINT32 (error .domain , = = , MONGOC_ERROR_TRANSACTION );
94
86
ASSERT_CONTAINS (error .message , "transaction" );
95
87
}
96
88
97
89
bson_destroy (& opts );
98
90
mongoc_collection_destroy (collection );
99
91
100
- if (!supported ) {
92
+ if (!r ) {
101
93
/* suppress "error in abortTransaction" warning from session_destroy */
102
94
capture_logs (true);
103
95
}
@@ -533,6 +525,38 @@ test_inherit_from_client (void *ctx)
533
525
mongoc_client_destroy (client );
534
526
}
535
527
528
+ void
529
+ test_transaction_fails_on_unsupported_version_or_sharded_cluster (void * ctx )
530
+ {
531
+ bson_error_t error ;
532
+ mongoc_client_session_t * session ;
533
+ mongoc_client_t * client ;
534
+ bool r ;
535
+
536
+ client = test_framework_client_new ();
537
+ session = mongoc_client_start_session (client , NULL , & error );
538
+ ASSERT_OR_PRINT (session , error );
539
+
540
+ r = mongoc_client_session_start_transaction (session , NULL , & error );
541
+ if (test_framework_is_mongos ()) {
542
+ BSON_ASSERT (!r );
543
+ ASSERT_CONTAINS (error .message ,
544
+ "Multi-document transactions on sharded clusters are "
545
+ "not supported by this version of libmongoc" );
546
+ } else if (!test_framework_max_wire_version_at_least (7 ) ||
547
+ (test_framework_is_mongos () &&
548
+ !test_framework_max_wire_version_at_least (8 ))) {
549
+ BSON_ASSERT (!r );
550
+ ASSERT_CONTAINS (error .message ,
551
+ "Multi-document transactions are not supported by this "
552
+ "server version" );
553
+ } else {
554
+ ASSERT_OR_PRINT (r , error );
555
+ }
556
+
557
+ mongoc_client_session_destroy (session );
558
+ mongoc_client_destroy (client );
559
+ }
536
560
537
561
void
538
562
test_transactions_install (TestSuite * suite )
@@ -582,4 +606,13 @@ test_transactions_install (TestSuite *suite)
582
606
NULL ,
583
607
NULL ,
584
608
test_framework_skip_if_no_txns );
609
+ TestSuite_AddFull (
610
+ suite ,
611
+ "/transactions/"
612
+ "transaction_fails_on_unsupported_version_or_sharded_cluster" ,
613
+ test_transaction_fails_on_unsupported_version_or_sharded_cluster ,
614
+ NULL ,
615
+ NULL ,
616
+ test_framework_skip_if_no_sessions ,
617
+ test_framework_skip_if_no_crypto );
585
618
}
0 commit comments