Skip to content

Commit 0d6a8ba

Browse files
authored
CDRIVER-4093 add versioned API strict examples 5-8 (#843)
CDRIVER-4120 update failpoint error message checks.
1 parent b80d00f commit 0d6a8ba

File tree

3 files changed

+138
-10
lines changed

3 files changed

+138
-10
lines changed

src/libmongoc/tests/test-mongoc-primary-stepdown.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,7 @@ test_not_primary_keep_pool (mongoc_client_t *client)
241241
coll, tmp_bson ("{'test': 1}"), NULL, NULL, &error);
242242
ASSERT (!res);
243243
ASSERT_CMPINT (error.code, ==, 10107);
244-
ASSERT_CONTAINS (error.message,
245-
"Failing command due to 'failCommand' failpoint");
244+
ASSERT_CONTAINS (error.message, "failpoint");
246245

247246
/* Execute a second insert, verify that it succeeds */
248247
res = mongoc_collection_insert_one (
@@ -308,8 +307,7 @@ test_not_primary_reset_pool (mongoc_client_t *client)
308307
coll, tmp_bson ("{'test': 1}"), NULL, NULL, &error);
309308
ASSERT (!res);
310309
ASSERT_CMPINT (error.code, ==, 10107);
311-
ASSERT_CONTAINS (error.message,
312-
"Failing command due to 'failCommand' failpoint");
310+
ASSERT_CONTAINS (error.message, "failpoint");
313311

314312
/* Verify that the pool has been cleared */
315313
ASSERT_CMPINT ((conn_count + 1), ==, _connection_count (client, primary_id));
@@ -376,8 +374,7 @@ test_shutdown_reset_pool (mongoc_client_t *client)
376374
coll, tmp_bson ("{'test': 1}"), NULL, NULL, &error);
377375
ASSERT (!res);
378376
ASSERT_CMPINT (error.code, ==, 91);
379-
ASSERT_CONTAINS (error.message,
380-
"Failing command due to 'failCommand' failpoint");
377+
ASSERT_CONTAINS (error.message, "failpoint");
381378

382379
/* Verify that the pool has been cleared */
383380
ASSERT_CMPINT ((conn_count + 1), ==, _connection_count (client, primary_id));
@@ -444,8 +441,7 @@ test_interrupted_shutdown_reset_pool (mongoc_client_t *client)
444441
coll, tmp_bson ("{'test': 1}"), NULL, NULL, &error);
445442
ASSERT (!res);
446443
ASSERT_CMPINT (error.code, ==, 11600);
447-
ASSERT_CONTAINS (error.message,
448-
"Failing command due to 'failCommand' failpoint");
444+
ASSERT_CONTAINS (error.message, "failpoint");
449445

450446
/* Verify that the pool has been cleared */
451447
ASSERT_CMPINT ((conn_count + 1), ==, _connection_count (client, primary_id));

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ test_retry_reads_off (void *ctx)
263263
res = mongoc_collection_read_command_with_opts (
264264
collection, cmd, NULL, NULL, NULL, &error);
265265
ASSERT (!res);
266-
ASSERT_CONTAINS (error.message,
267-
"Failing command due to 'failCommand' failpoint");
266+
ASSERT_CONTAINS (error.message, "failpoint");
268267

269268
deactivate_fail_points (client, server_id);
270269

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

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,13 +3703,146 @@ _test_sample_versioned_api_example_4 (void)
37033703
mongoc_server_api_destroy (server_api);
37043704
}
37053705

3706+
static int64_t iso_to_unix (const char* iso_str) {
3707+
/* TODO (CDRIVER-2945) there is no convenient helper for converting ISO8601
3708+
* strings to Unix timestamps. This is not shown in the example. */
3709+
return 1628330345;
3710+
}
3711+
3712+
static void _test_sample_versioned_api_example_5_6_7_8 (void) {
3713+
#define N_DOCS 8
3714+
mongoc_client_t *client;
3715+
mongoc_server_api_t *server_api;
3716+
mongoc_server_api_version_t server_api_version;
3717+
bool ok;
3718+
bson_error_t error;
3719+
mongoc_database_t *db;
3720+
mongoc_collection_t *sales;
3721+
bson_t *docs[N_DOCS];
3722+
int i;
3723+
bson_t reply;
3724+
bson_t *cmd;
3725+
int64_t count;
3726+
bson_t *filter;
3727+
3728+
client = get_client ();
3729+
mongoc_client_set_error_api (client, MONGOC_ERROR_API_VERSION_2);
3730+
mongoc_server_api_version_from_string ("1", &server_api_version);
3731+
server_api = mongoc_server_api_new (server_api_version);
3732+
mongoc_server_api_strict (server_api, true);
3733+
ok = mongoc_client_set_server_api (client, server_api, &error);
3734+
ASSERT_OR_PRINT (ok, error);
3735+
db = mongoc_client_get_database (client, "db");
3736+
sales = mongoc_database_get_collection (db, "sales");
3737+
/* Drop db.sales in case the collection exists. */
3738+
ok = mongoc_collection_drop (sales, &error);
3739+
if (!ok && NULL == strstr (error.message, "ns not found")) {
3740+
/* Ignore an "ns not found" error on dropping the collection in case the
3741+
* namespace does not exist. */
3742+
ASSERT_OR_PRINT (ok, error);
3743+
}
3744+
3745+
/* Start Versioned API Example 5 */
3746+
docs[0] = BCON_NEW ("_id", BCON_INT32 (1),
3747+
"item", "abc",
3748+
"price", BCON_INT32 (10),
3749+
"quantity", BCON_INT32 (2),
3750+
"date", BCON_DATE_TIME (iso_to_unix ("2021-01-01T08:00:00Z")));
3751+
docs[1] = BCON_NEW ("_id", BCON_INT32 (2),
3752+
"item", "jkl",
3753+
"price", BCON_INT32 (20),
3754+
"quantity", BCON_INT32 (1),
3755+
"date", BCON_DATE_TIME (iso_to_unix ("2021-02-03T09:00:00Z")));
3756+
docs[2] = BCON_NEW ("_id", BCON_INT32 (3),
3757+
"item", "xyz",
3758+
"price", BCON_INT32 (5),
3759+
"quantity", BCON_INT32 (5),
3760+
"date", BCON_DATE_TIME (iso_to_unix ("2021-02-03T09:05:00Z")));
3761+
docs[3] = BCON_NEW ("_id", BCON_INT32 (4),
3762+
"item", "abc",
3763+
"price", BCON_INT32 (10),
3764+
"quantity", BCON_INT32 (10),
3765+
"date", BCON_DATE_TIME (iso_to_unix ("2021-02-15T08:00:00Z")));
3766+
docs[4] = BCON_NEW ("_id", BCON_INT32 (5),
3767+
"item", "xyz",
3768+
"price", BCON_INT32 (5),
3769+
"quantity",BCON_INT32 (10),
3770+
"date", BCON_DATE_TIME (iso_to_unix ("2021-02-15T09:05:00Z")));
3771+
docs[5] = BCON_NEW ("_id", BCON_INT32 (6),
3772+
"item", "xyz",
3773+
"price", BCON_INT32 (5),
3774+
"quantity",BCON_INT32 (5),
3775+
"date", BCON_DATE_TIME (iso_to_unix ("2021-02-15T12:05:10Z")));
3776+
docs[6] = BCON_NEW ("_id", BCON_INT32 (7),
3777+
"item", "xyz",
3778+
"price", BCON_INT32 (5),
3779+
"quantity",BCON_INT32 (10),
3780+
"date", BCON_DATE_TIME (iso_to_unix ("2021-02-15T14:12:12Z")));
3781+
docs[7] = BCON_NEW ("_id", BCON_INT32 (8),
3782+
"item", "abc",
3783+
"price", BCON_INT32 (10),
3784+
"quantity",BCON_INT32 (5),
3785+
"date", BCON_DATE_TIME (iso_to_unix ("2021-03-16T20:20:13Z")));
3786+
ok = mongoc_collection_insert_many (
3787+
sales, (const bson_t**) docs, N_DOCS, NULL /* opts */, &reply, &error);
3788+
/* End Versioned API Example 5 */
3789+
ASSERT_OR_PRINT (ok, error);
3790+
bson_destroy (&reply);
3791+
3792+
cmd = BCON_NEW ("count", "sales");
3793+
ok = mongoc_database_command_simple (
3794+
db, cmd, NULL /* read_prefs */, &reply, &error);
3795+
ASSERT_ERROR_CONTAINS (error,
3796+
MONGOC_ERROR_SERVER,
3797+
323,
3798+
"Provided apiStrict:true, but the command count is not in API Version 1");
3799+
ASSERT (!ok);
3800+
bson_destroy (&reply);
3801+
#if 0
3802+
/* This block not evaluated, but is inserted into documentation to represent the above reply.
3803+
* Don't delete me! */
3804+
/* Start Versioned API Example 6 */
3805+
char *str = bson_as_json (&reply, NULL /* length */);
3806+
printf ("%s", str);
3807+
/* Prints the server reply:
3808+
* { "ok" : 0, "errmsg" : "Provided apiStrict:true, but the command count is not in API Version 1", "code" : 323, "codeName" : "APIStrictError" } */
3809+
bson_free (str);
3810+
/* End Versioned API Example 6 */
3811+
#endif
3812+
3813+
/* Start Versioned API Example 7 */
3814+
filter = bson_new ();
3815+
count = mongoc_collection_count_documents (
3816+
sales, filter, NULL /* opts */, NULL /* read_prefs */, &reply, &error);
3817+
/* End Versioned API Example 7 */
3818+
if (N_DOCS != count) {
3819+
test_error ("expected %d documents, got %" PRId64, N_DOCS, count);
3820+
}
3821+
bson_destroy (&reply);
3822+
3823+
/* Start Versioned API Example 8 */
3824+
BSON_ASSERT (count == N_DOCS);
3825+
/* End Versioned API Example 8 */
3826+
3827+
bson_destroy (filter);
3828+
bson_destroy (cmd);
3829+
for (i = 0; i < N_DOCS; i++) {
3830+
bson_destroy (docs[i]);
3831+
}
3832+
mongoc_collection_destroy (sales);
3833+
mongoc_database_destroy (db);
3834+
mongoc_server_api_destroy (server_api);
3835+
mongoc_client_destroy (client);
3836+
}
3837+
37063838
static void
37073839
test_sample_versioned_api (void)
37083840
{
37093841
_test_sample_versioned_api_example_1 ();
37103842
_test_sample_versioned_api_example_2 ();
37113843
_test_sample_versioned_api_example_3 ();
37123844
_test_sample_versioned_api_example_4 ();
3845+
_test_sample_versioned_api_example_5_6_7_8 ();
37133846
}
37143847

37153848
static void

0 commit comments

Comments
 (0)