@@ -4931,6 +4931,50 @@ test_bulk_let_multi (void)
4931
4931
mock_server_destroy (mock_server );
4932
4932
}
4933
4933
4934
+
4935
+ static void
4936
+ bulk_write_multiple_errors_started_cb (
4937
+ const mongoc_apm_command_started_t * event )
4938
+ {
4939
+ bson_string_t * event_log = mongoc_apm_command_started_get_context (event );
4940
+
4941
+ bson_string_append_printf (
4942
+ event_log ,
4943
+ "command %s started to %s: %s\n" ,
4944
+ mongoc_apm_command_started_get_command_name (event ),
4945
+ mongoc_apm_command_started_get_host (event )-> host_and_port ,
4946
+ tmp_json (mongoc_apm_command_started_get_command (event )));
4947
+ }
4948
+
4949
+ static void
4950
+ bulk_write_multiple_errors_succeeded_cb (
4951
+ const mongoc_apm_command_succeeded_t * event )
4952
+ {
4953
+ bson_string_t * event_log = mongoc_apm_command_succeeded_get_context (event );
4954
+
4955
+ bson_string_append_printf (
4956
+ event_log ,
4957
+ "command %s succeeded to %s in %" PRId64 "us: %s\n" ,
4958
+ mongoc_apm_command_succeeded_get_command_name (event ),
4959
+ mongoc_apm_command_succeeded_get_host (event )-> host_and_port ,
4960
+ mongoc_apm_command_succeeded_get_duration (event ),
4961
+ tmp_json (mongoc_apm_command_succeeded_get_reply (event )));
4962
+ }
4963
+
4964
+ static void
4965
+ bulk_write_multiple_errors_failed_cb (const mongoc_apm_command_failed_t * event )
4966
+ {
4967
+ bson_string_t * event_log = mongoc_apm_command_failed_get_context (event );
4968
+
4969
+ bson_string_append_printf (
4970
+ event_log ,
4971
+ "command %s failed to %s in %" PRId64 "us: %s\n" ,
4972
+ mongoc_apm_command_failed_get_command_name (event ),
4973
+ mongoc_apm_command_failed_get_host (event )-> host_and_port ,
4974
+ mongoc_apm_command_failed_get_duration (event ),
4975
+ tmp_json (mongoc_apm_command_failed_get_reply (event )));
4976
+ }
4977
+
4934
4978
// Test a bulk write operation that receives two error replies from two
4935
4979
// commands.
4936
4980
static void
@@ -4944,10 +4988,19 @@ test_bulk_write_multiple_errors (void *unused)
4944
4988
bson_t reply ;
4945
4989
bson_error_t error ;
4946
4990
bool r ;
4991
+ mongoc_apm_callbacks_t * cbs = mongoc_apm_callbacks_new ();
4992
+ bson_string_t * event_log = bson_string_new (NULL );
4947
4993
4948
4994
client = test_framework_new_default_client ();
4949
4995
BSON_ASSERT (client );
4950
4996
4997
+ mongoc_apm_set_command_started_cb (cbs ,
4998
+ bulk_write_multiple_errors_started_cb );
4999
+ mongoc_apm_set_command_succeeded_cb (
5000
+ cbs , bulk_write_multiple_errors_succeeded_cb );
5001
+ mongoc_apm_set_command_failed_cb (cbs , bulk_write_multiple_errors_failed_cb );
5002
+ mongoc_client_set_apm_callbacks (client , cbs , event_log );
5003
+
4951
5004
collection = get_test_collection (client , "test_bulk_write_multiple_errors" );
4952
5005
BSON_ASSERT (collection );
4953
5006
@@ -4983,14 +5036,21 @@ test_bulk_write_multiple_errors (void *unused)
4983
5036
r = (bool ) mongoc_bulk_operation_execute (bulk , & reply , & error );
4984
5037
BSON_ASSERT (!r );
4985
5038
4986
- ASSERT_MATCH (& reply ,
4987
- "{'nInserted': 2,"
4988
- " 'nMatched': 0,"
4989
- " 'nModified': 0,"
4990
- " 'nRemoved': 1,"
4991
- " 'nUpserted': 0,"
4992
- " 'errorReplies': [{'code': 8}, {'code': 8}],"
4993
- " 'writeErrors': [{ 'index' : 5 }]}" );
5039
+ const char * pattern = "{'nInserted': 2,"
5040
+ " 'nMatched': 0,"
5041
+ " 'nModified': 0,"
5042
+ " 'nRemoved': 1,"
5043
+ " 'nUpserted': 0,"
5044
+ " 'errorReplies': [{'code': 8}, {'code': 8}],"
5045
+ " 'writeErrors': [{ 'index' : 5 }]}" ;
5046
+
5047
+ if (!match_json (& reply , false, __FILE__ , __LINE__ , BSON_FUNC , pattern )) {
5048
+ // match_json prints the expected pattern and actual document.
5049
+ // Print the command monitoring events to help diagnose the flaky test
5050
+ // failure CDRIVER-4539.
5051
+ test_error ("Got the following command monitoring events:\n%s" ,
5052
+ event_log -> str );
5053
+ }
4994
5054
4995
5055
assert_write_error_count (1 , & reply );
4996
5056
ASSERT_COUNT (1 , collection );
@@ -5000,6 +5060,8 @@ test_bulk_write_multiple_errors (void *unused)
5000
5060
bson_destroy (& opts );
5001
5061
mongoc_collection_destroy (collection );
5002
5062
mongoc_client_destroy (client );
5063
+ bson_string_free (event_log , true);
5064
+ mongoc_apm_callbacks_destroy (cbs );
5003
5065
}
5004
5066
5005
5067
0 commit comments