Skip to content

Commit f3a3185

Browse files
committed
CDRIVER-3296 fix APM checks
1 parent 8c003b9 commit f3a3185

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

src/libmongoc/tests/json-test-monitoring.c

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ apm_match_visitor (match_ctx_t *ctx,
385385
void
386386
check_json_apm_events (json_test_ctx_t *ctx, const bson_t *expectations)
387387
{
388-
bson_iter_t expectations_iter;
389-
bson_iter_t events_iter;
388+
bson_iter_t expectations_iter, actual_iter;
390389
bool allow_subset;
391390
match_ctx_t match_ctx = {{0}};
392391
apm_match_visitor_ctx_t apm_match_visitor_ctx = {0};
@@ -407,46 +406,60 @@ check_json_apm_events (json_test_ctx_t *ctx, const bson_t *expectations)
407406
allow_subset = ctx->config->command_monitoring_allow_subset;
408407

409408
BSON_ASSERT (bson_iter_init (&expectations_iter, expectations));
410-
BSON_ASSERT (bson_iter_init (&events_iter, &ctx->events));
411-
i = 0;
409+
BSON_ASSERT (bson_iter_init (&actual_iter, &ctx->events));
412410

411+
/* Compare the captured actual events against the expectations. */
413412
while (bson_iter_next (&expectations_iter)) {
414-
bson_t expectation;
415-
bson_iter_bson (&expectations_iter, &expectation);
416-
417-
for (; i < ctx->n_events; i++) {
418-
bson_t event;
419-
bool matched;
420-
421-
if (!bson_iter_next (&events_iter)) {
422-
test_error ("could not match APM event in empty array\n"
423-
"\texpected: %s\n\n",
424-
bson_as_canonical_extended_json (&expectation, NULL));
425-
}
426-
427-
bson_iter_bson (&events_iter, &event);
413+
bson_t expectation, actual;
414+
bool matched = false;
428415

429-
matched = match_bson_with_ctx (&event, &expectation, &match_ctx);
416+
bson_iter_bson (&expectations_iter, &expectation);
417+
/* match against the current actual event, and possibly skip actual events
418+
* if we allow subset matching. */
419+
while (bson_iter_next (&actual_iter)) {
420+
bson_iter_bson (&actual_iter, &actual);
421+
matched = match_bson_with_ctx (&actual, &expectation, &match_ctx);
430422
apm_match_visitor_ctx_reset (&apm_match_visitor_ctx);
431-
bson_destroy (&event);
423+
bson_destroy (&actual);
432424

433425
if (matched) {
434426
break;
435427
}
436428

437-
if (!allow_subset || i == ctx->n_events - 1) {
429+
if (allow_subset) {
430+
/* if we allow matching only a subset of actual events, skip
431+
* non-matching ones */
432+
continue;
433+
} else {
438434
test_error ("could not match APM event\n"
439435
"\texpected: %s\n\n"
440436
"\tactual : %s\n\n"
441437
"\terror : %s\n\n",
442438
bson_as_canonical_extended_json (&expectation, NULL),
443-
bson_as_canonical_extended_json (&event, NULL),
439+
bson_as_canonical_extended_json (&actual, NULL),
444440
match_ctx.errmsg);
445441
}
446442
}
443+
444+
if (!matched) {
445+
test_error ("expectation unmatched\n"
446+
"\texpected: %s\n\n",
447+
bson_as_canonical_extended_json (&expectation, NULL));
448+
}
449+
447450
bson_destroy (&expectation);
448451
}
449452

453+
/* If we do not allow matching against a subset of actual events, check if
454+
* there are extra "actual" events */
455+
if (!allow_subset && bson_iter_next (&actual_iter)) {
456+
bson_t extra;
457+
458+
bson_iter_bson (&actual_iter, &extra);
459+
test_error ("extra actual event was not found in expectations: %s\n",
460+
bson_as_canonical_extended_json (&extra, NULL));
461+
}
462+
450463
for (i = 0; i < 2; i++) {
451464
bson_destroy (&apm_match_visitor_ctx.lsids[i]);
452465
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ test_crud_cb (bson_t *scenario)
2525
{
2626
json_test_config_t config = JSON_TEST_CONFIG_INIT;
2727
config.run_operation_cb = crud_test_operation_cb;
28+
config.command_started_events_only = true;
2829
config.scenario = scenario;
2930
run_json_general_test (&config);
3031
}

src/libmongoc/tests/test-mongoc-retryable-writes.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ test_retryable_writes_cb (bson_t *scenario)
4343
config.ctx = &explicit_session;
4444
config.run_operation_cb = retryable_writes_test_run_operation;
4545
config.scenario = scenario;
46+
config.command_started_events_only = true;
4647
explicit_session = true;
4748
run_json_general_test (&config);
4849
explicit_session = false;

0 commit comments

Comments
 (0)