Skip to content

Commit 35f7ec5

Browse files
CDRIVER-4352 unskip /Samples tests (#1692)
* use `MONGOC_DEBUG` instead of `MONGOC_INFO` Fixes test failure in `test_sample_txn_commands` expecting no captured logs. Debug logs are not captured. * unskip `/Samples` tests * skip snapshot sessions tests if server too old Snapshot sessions require server 5.0. Addresses driver error "Snapshot reads require MongoDB 5.0 or later". * insert with majority write concern Resolves two observed errors: - On server 5.0 and 6.0, `aggregate` sometimes returns the error SnapshotUnavailable(246) with message "Unable to read from a snapshot due to pending collection catalog changes; please retry the operation". - On server 7.0 and 8.0, `aggregate` sometimes returns no error but does not return the just-inserted document. * fix typo * stop capturing logs after asserting To restore the default behavior (print to stdout). * rename snapshot query examples The numbers 1-58 are associated with the examples added for DRIVERS-356. 59 and 60 have no such reference. * describe rationale for reordering * reduce scope of captured logs --------- Co-authored-by: Ezra Chung <[email protected]>
1 parent 4676c8c commit 35f7ec5

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

.evergreen/etc/skip-tests.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@
2828

2929
/transactions/legacy/mongos-recovery-token/"commitTransaction retry fails on new mongos" # fails with server selection timeout (CDRIVER-4268)
3030
/transactions/legacy/pin-mongos/"unpin after transient error within a transaction and commit" # (CDRIVER-4351) server selection timeout (on ASAN Tests Ubuntu 18.04 build variant)
31-
/Samples # (CDRIVER-4352) strange "heartbeat failed" error
3231

3332
/client_side_encryption/bypass_spawning_mongocryptd/mongocryptdBypassSpawn # Fails if crypt_shared is visible

src/libmongoc/tests/test-mongoc-sample-commands.c

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ test_sample_command (sample_command_fn_t fn,
4747
bool drop_collection)
4848
{
4949
char *example_name = bson_strdup_printf ("example %d", exampleno);
50-
capture_logs (true);
5150

51+
capture_logs (true);
5252
fn (db);
53-
53+
capture_logs (false);
5454
ASSERT_NO_CAPTURED_LOGS (example_name);
5555

5656
if (drop_collection) {
@@ -2483,13 +2483,23 @@ insert_pet (mongoc_collection_t *collection, bool is_adoptable)
24832483

24842484
doc = BCON_NEW ("adoptable", BCON_BOOL (is_adoptable));
24852485

2486-
rc = mongoc_collection_insert_one (collection, doc, NULL, NULL, &error);
2486+
// Insert with majority write concern. Snapshot read concern reads from majority committed data.
2487+
bson_t opts = BSON_INITIALIZER;
2488+
{
2489+
mongoc_write_concern_t *wc = mongoc_write_concern_new ();
2490+
mongoc_write_concern_set_w (wc, MONGOC_WRITE_CONCERN_W_MAJORITY);
2491+
mongoc_write_concern_append (wc, &opts);
2492+
mongoc_write_concern_destroy (wc);
2493+
}
2494+
2495+
rc = mongoc_collection_insert_one (collection, doc, &opts, NULL, &error);
24872496
if (!rc) {
24882497
MONGOC_ERROR ("insert into pets.%s failed: %s", mongoc_collection_get_name (collection), error.message);
24892498
goto cleanup;
24902499
}
24912500

24922501
cleanup:
2502+
bson_destroy (&opts);
24932503
bson_destroy (doc);
24942504
return rc;
24952505
}
@@ -2590,18 +2600,18 @@ accumulate_adoptable_count (const mongoc_client_session_t *cs,
25902600
return rc;
25912601
}
25922602

2593-
/*
2594-
* JIRA: https://jira.mongodb.org/browse/DRIVERS-2181
2595-
*/
25962603
static void
2597-
test_example_59 (mongoc_database_t *db)
2604+
test_snapshot_query_example_1 (void)
25982605
{
2599-
BSON_UNUSED (db);
2600-
26012606
if (!test_framework_skip_if_no_txns ()) {
26022607
return;
26032608
}
26042609

2610+
if (!test_framework_max_wire_version_at_least (WIRE_VERSION_SNAPSHOT_READS)) {
2611+
MONGOC_DEBUG ("Skipping test. Server does not support snapshot reads\n");
2612+
return;
2613+
}
2614+
26052615
mongoc_client_t *client = NULL;
26062616
client = test_framework_new_default_client ();
26072617

@@ -2661,7 +2671,7 @@ test_example_59 (mongoc_database_t *db)
26612671
/* End Snapshot Query Example 1 */
26622672

26632673
if (adoptable_pets_count != 2) {
2664-
MONGOC_ERROR ("there should be exatly 2 adoptable_pets_count, found: %" PRId64, adoptable_pets_count);
2674+
MONGOC_ERROR ("there should be exactly 2 adoptable_pets_count, found: %" PRId64, adoptable_pets_count);
26652675
}
26662676

26672677
/* Start Snapshot Query Example 1 Post */
@@ -2681,6 +2691,7 @@ retail_setup (mongoc_collection_t *sales_collection)
26812691
bson_error_t error;
26822692
struct timeval tv;
26832693
int64_t unix_time_now = 0;
2694+
bson_t opts = BSON_INITIALIZER;
26842695

26852696
if (bson_gettimeofday (&tv)) {
26862697
MONGOC_ERROR ("could not get time of day");
@@ -2693,27 +2704,39 @@ retail_setup (mongoc_collection_t *sales_collection)
26932704
doc =
26942705
BCON_NEW ("shoeType", BCON_UTF8 ("boot"), "price", BCON_INT64 (30), "saleDate", BCON_DATE_TIME (unix_time_now));
26952706

2696-
ok = mongoc_collection_insert_one (sales_collection, doc, NULL, NULL, &error);
2707+
// Insert with majority write concern. Snapshot read concern reads from majority committed data.
2708+
{
2709+
mongoc_write_concern_t *wc = mongoc_write_concern_new ();
2710+
mongoc_write_concern_set_w (wc, MONGOC_WRITE_CONCERN_W_MAJORITY);
2711+
mongoc_write_concern_append (wc, &opts);
2712+
mongoc_write_concern_destroy (wc);
2713+
}
2714+
2715+
ok = mongoc_collection_insert_one (sales_collection, doc, &opts, NULL, &error);
26972716
if (!ok) {
26982717
MONGOC_ERROR ("insert into retail.sales failed: %s", error.message);
26992718
goto cleanup;
27002719
}
27012720

27022721
cleanup:
2722+
bson_destroy (&opts);
27032723
bson_destroy (doc);
27042724
return ok;
27052725
}
27062726

27072727

27082728
static void
2709-
test_example_60 (mongoc_database_t *db)
2729+
test_snapshot_query_example_2 (void)
27102730
{
2711-
BSON_UNUSED (db);
2712-
27132731
if (!test_framework_skip_if_no_txns ()) {
27142732
return;
27152733
}
27162734

2735+
if (!test_framework_max_wire_version_at_least (WIRE_VERSION_SNAPSHOT_READS)) {
2736+
MONGOC_DEBUG ("Skipping test. Server does not support snapshot reads\n");
2737+
return;
2738+
}
2739+
27172740
mongoc_client_t *client = NULL;
27182741
client = test_framework_new_default_client ();
27192742

@@ -2822,6 +2845,21 @@ test_example_60 (mongoc_database_t *db)
28222845
/* End Snapshot Query Example 2 Post */
28232846
}
28242847

2848+
// `test_snapshot_query_examples` examples for DRIVERS-2181.
2849+
static void
2850+
test_snapshot_query_examples (void)
2851+
{
2852+
capture_logs (true);
2853+
test_snapshot_query_example_1 ();
2854+
capture_logs (false);
2855+
ASSERT_NO_CAPTURED_LOGS ("test_snapshot_query_example_1");
2856+
2857+
capture_logs (true);
2858+
test_snapshot_query_example_2 ();
2859+
capture_logs (false);
2860+
ASSERT_NO_CAPTURED_LOGS ("test_snapshot_query_example_2");
2861+
}
2862+
28252863
/* clang-format off */
28262864

28272865
typedef struct {
@@ -2883,6 +2921,7 @@ test_sample_change_stream_command (sample_command_fn_t fn,
28832921
capture_logs (true);
28842922
fn (db);
28852923
ASSERT_NO_CAPTURED_LOGS ("change stream examples");
2924+
capture_logs (false);
28862925

28872926
bson_mutex_lock (&ctx.lock);
28882927
ctx.done = true;
@@ -3678,7 +3717,7 @@ commit_with_retry (mongoc_client_session_t *cs, bson_error_t *error)
36783717
* mongoc_transaction_opts_set_write_concern */
36793718
r = mongoc_client_session_commit_transaction (cs, &reply, error);
36803719
if (r) {
3681-
MONGOC_INFO ("Transaction committed");
3720+
MONGOC_DEBUG ("Transaction committed");
36823721
break;
36833722
}
36843723

@@ -3826,6 +3865,7 @@ test_sample_txn_commands (mongoc_client_t *client)
38263865
/* test transactions retry example 3 */
38273866
example_func (client);
38283867
ASSERT_NO_CAPTURED_LOGS ("transactions retry example 3");
3868+
capture_logs (false);
38293869
find_and_match (employees, "{'employee': 3}", "{'status': 'Inactive'}");
38303870

38313871
mongoc_collection_destroy (employees);
@@ -4383,16 +4423,16 @@ test_sample_commands (void)
43834423
test_sample_command (test_example_55, 55, db, collection, false);
43844424
test_sample_command (test_example_57, 57, db, collection, false);
43854425
test_sample_command (test_example_58, 58, db, collection, false);
4426+
// Run 56 after 57 and 58. 56 deletes all data. 57 and 58 expect data present.
43864427
test_sample_command (test_example_56, 56, db, collection, true);
4387-
test_sample_command (test_example_59, 59, db, collection, true);
4388-
test_sample_command (test_example_60, 60, db, collection, true);
43894428
test_sample_change_stream_command (test_example_change_stream, db);
43904429
test_sample_causal_consistency (client);
43914430
test_sample_aggregation (db);
43924431
test_sample_projection_with_aggregation_expressions (db);
43934432
test_sample_indexes (db);
43944433
test_sample_run_command (db);
43954434
test_sample_txn_commands (client);
4435+
test_snapshot_query_examples ();
43964436

43974437
if (test_framework_max_wire_version_at_least (WIRE_VERSION_4_9)) {
43984438
test_sample_versioned_api ();

0 commit comments

Comments
 (0)