Skip to content

Commit 9b00c8d

Browse files
committed
log command monitoring events on test failure
1 parent b36fdfd commit 9b00c8d

File tree

1 file changed

+70
-8
lines changed

1 file changed

+70
-8
lines changed

src/libmongoc/tests/test-mongoc-bulk.c

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4931,6 +4931,50 @@ test_bulk_let_multi (void)
49314931
mock_server_destroy (mock_server);
49324932
}
49334933

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+
49344978
// Test a bulk write operation that receives two error replies from two
49354979
// commands.
49364980
static void
@@ -4944,10 +4988,19 @@ test_bulk_write_multiple_errors (void *unused)
49444988
bson_t reply;
49454989
bson_error_t error;
49464990
bool r;
4991+
mongoc_apm_callbacks_t *cbs = mongoc_apm_callbacks_new ();
4992+
bson_string_t *event_log = bson_string_new (NULL);
49474993

49484994
client = test_framework_new_default_client ();
49494995
BSON_ASSERT (client);
49504996

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+
49515004
collection = get_test_collection (client, "test_bulk_write_multiple_errors");
49525005
BSON_ASSERT (collection);
49535006

@@ -4983,14 +5036,21 @@ test_bulk_write_multiple_errors (void *unused)
49835036
r = (bool) mongoc_bulk_operation_execute (bulk, &reply, &error);
49845037
BSON_ASSERT (!r);
49855038

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+
}
49945054

49955055
assert_write_error_count (1, &reply);
49965056
ASSERT_COUNT (1, collection);
@@ -5000,6 +5060,8 @@ test_bulk_write_multiple_errors (void *unused)
50005060
bson_destroy (&opts);
50015061
mongoc_collection_destroy (collection);
50025062
mongoc_client_destroy (client);
5063+
bson_string_free (event_log, true);
5064+
mongoc_apm_callbacks_destroy (cbs);
50035065
}
50045066

50055067

0 commit comments

Comments
 (0)