@@ -4883,6 +4883,76 @@ test_insert_one_reports_id(void)
4883
4883
4884
4884
#undef ASSERT_INDEX_EXISTS
4885
4885
4886
+ static void
4887
+ test_create_indexes_acts_as_write_command (void * unused )
4888
+ {
4889
+ BSON_UNUSED (unused );
4890
+
4891
+ mongoc_client_t * client = test_framework_new_default_client ();
4892
+ mongoc_collection_t * coll = get_test_collection (client , "test_create_indexes_with_opts" );
4893
+ bson_error_t error ;
4894
+ bson_t reply ;
4895
+
4896
+ // Test write concern is inherited from collection:
4897
+ {
4898
+ // Set a non-default write concern on collection:
4899
+ {
4900
+ mongoc_write_concern_t * wc = mongoc_write_concern_new ();
4901
+ mongoc_write_concern_set_w (wc , -1 ); // Set a write concern that fails.
4902
+ mongoc_collection_set_write_concern (coll , wc );
4903
+ mongoc_write_concern_destroy (wc );
4904
+ }
4905
+
4906
+ // Create index:
4907
+ {
4908
+ const bson_t * keys = tmp_bson ("{'x': 1}" );
4909
+ mongoc_index_model_t * im = mongoc_index_model_new (keys , NULL );
4910
+ bool ok =
4911
+ mongoc_collection_create_indexes_with_opts (coll , & im , 1 , NULL /* options */ , NULL /* reply */ , & error );
4912
+ ASSERT (!ok );
4913
+ ASSERT_ERROR_CONTAINS (error , 5 , 9 , "w has to be a non-negative number and not greater than 50" );
4914
+ mongoc_index_model_destroy (im );
4915
+ }
4916
+ }
4917
+
4918
+ // Test a server reply with "writeConcernError" is considered an error:
4919
+ {
4920
+ // Set the default write concern on collection:
4921
+ {
4922
+ mongoc_write_concern_t * wc = mongoc_write_concern_new (); // Default write concern.
4923
+ mongoc_collection_set_write_concern (coll , wc );
4924
+ mongoc_write_concern_destroy (wc );
4925
+ }
4926
+
4927
+ // Set a failpoint to fail with "writeConcernError":
4928
+ {
4929
+ const char * cmd_str = BSON_STR ({
4930
+ "configureFailPoint" : "failCommand ",
4931
+ " mode " : {" times " : 1},
4932
+ " data " : {" failCommands " : [" createIndexes "], " writeConcernError " : {" code " : 123, " errmsg " : " foo "}}
4933
+ });
4934
+ bson_t * failpoint_cmd = bson_new_from_json ((const uint8_t * )cmd_str , -1 , & error );
4935
+ ASSERT_OR_PRINT (failpoint_cmd , error );
4936
+ bool ok = mongoc_client_command_simple (client , "admin" , failpoint_cmd , NULL /* read_prefs */ , & reply , & error );
4937
+ ASSERT_OR_PRINT (ok , error );
4938
+ bson_destroy (failpoint_cmd );
4939
+ bson_destroy (& reply );
4940
+ }
4941
+
4942
+ {
4943
+ const bson_t * keys = tmp_bson ("{'x': 1}" );
4944
+ mongoc_index_model_t * im = mongoc_index_model_new (keys , NULL );
4945
+ bool ok = mongoc_collection_create_indexes_with_opts (coll , & im , 1 , NULL /* opts */ , NULL /* reply */ , & error );
4946
+ ASSERT (!ok );
4947
+ ASSERT_ERROR_CONTAINS (error , MONGOC_ERROR_WRITE_CONCERN , 123 , "foo" );
4948
+ mongoc_index_model_destroy (im );
4949
+ }
4950
+ }
4951
+
4952
+ mongoc_collection_destroy (coll );
4953
+ mongoc_client_destroy (client );
4954
+ }
4955
+
4886
4956
4887
4957
void
4888
4958
test_collection_install (TestSuite * suite )
@@ -5016,4 +5086,14 @@ test_collection_install(TestSuite *suite)
5016
5086
// requires failpoint
5017
5087
test_framework_skip_if_no_failpoint );
5018
5088
TestSuite_AddLive (suite , "/Collection/insert_one_reports_id" , test_insert_one_reports_id );
5089
+
5090
+ TestSuite_AddFull (suite ,
5091
+ "/Collection/test_create_indexes_acts_as_write_command" ,
5092
+ test_create_indexes_acts_as_write_command ,
5093
+ NULL /* _dtor */ ,
5094
+ NULL /* _ctx */ ,
5095
+ // requires failpoint
5096
+ test_framework_skip_if_no_failpoint ,
5097
+ // Server Version 4.4 has Wire Version 9 - w < 0 does not error on earlier versions.
5098
+ test_framework_skip_if_max_wire_version_less_than_9 );
5019
5099
}
0 commit comments