@@ -385,8 +385,7 @@ apm_match_visitor (match_ctx_t *ctx,
385
385
void
386
386
check_json_apm_events (json_test_ctx_t * ctx , const bson_t * expectations )
387
387
{
388
- bson_iter_t expectations_iter ;
389
- bson_iter_t events_iter ;
388
+ bson_iter_t expectations_iter , actual_iter ;
390
389
bool allow_subset ;
391
390
match_ctx_t match_ctx = {{0 }};
392
391
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)
407
406
allow_subset = ctx -> config -> command_monitoring_allow_subset ;
408
407
409
408
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 ));
412
410
411
+ /* Compare the captured actual events against the expectations. */
413
412
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;
428
415
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 );
430
422
apm_match_visitor_ctx_reset (& apm_match_visitor_ctx );
431
- bson_destroy (& event );
423
+ bson_destroy (& actual );
432
424
433
425
if (matched ) {
434
426
break ;
435
427
}
436
428
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 {
438
434
test_error ("could not match APM event\n"
439
435
"\texpected: %s\n\n"
440
436
"\tactual : %s\n\n"
441
437
"\terror : %s\n\n" ,
442
438
bson_as_canonical_extended_json (& expectation , NULL ),
443
- bson_as_canonical_extended_json (& event , NULL ),
439
+ bson_as_canonical_extended_json (& actual , NULL ),
444
440
match_ctx .errmsg );
445
441
}
446
442
}
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
+
447
450
bson_destroy (& expectation );
448
451
}
449
452
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
+
450
463
for (i = 0 ; i < 2 ; i ++ ) {
451
464
bson_destroy (& apm_match_visitor_ctx .lsids [i ]);
452
465
}
0 commit comments