Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 798dd7d

Browse files
CDRIVER-5945 mongoc_collection_create_indexes_with_opts does not apply write command behavior
1 parent f000f77 commit 798dd7d

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

‎src/libmongoc/src/mongoc/mongoc-collection.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,8 +2634,7 @@ mongoc_collection_create_indexes_with_opts(mongoc_collection_t *collection,
26342634
}
26352635
BSON_ASSERT(bson_append_array_builder_end(&cmd, indexes));
26362636

2637-
ok = mongoc_client_command_with_opts(
2638-
collection->client, collection->db, &cmd, NULL /* read_prefs */, opts, reply_ptr, error);
2637+
ok = mongoc_collection_write_command_with_opts(collection, &cmd, opts, reply_ptr, error);
26392638

26402639
fail:
26412640
mongoc_server_stream_cleanup(server_stream);

‎src/libmongoc/tests/test-mongoc-collection.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4883,6 +4883,76 @@ test_insert_one_reports_id(void)
48834883

48844884
#undef ASSERT_INDEX_EXISTS
48854885

4886+
static void
4887+
test_create_indexes_acts_as_write_command(void *unused)
4888+
{
4889+
BSON_UNUSED(unused);
4890+
4891+
mongoc_client_t *client = test_framework_new_default_client();
4892+
mongoc_collection_t *coll = get_test_collection(client, "test_create_indexes_with_opts");
4893+
bson_error_t error;
4894+
bson_t reply;
4895+
4896+
// Test write concern is inherited from collection:
4897+
{
4898+
// Set a non-default write concern on collection:
4899+
{
4900+
mongoc_write_concern_t *wc = mongoc_write_concern_new();
4901+
mongoc_write_concern_set_w(wc, -1); // Set a write concern that fails.
4902+
mongoc_collection_set_write_concern(coll, wc);
4903+
mongoc_write_concern_destroy(wc);
4904+
}
4905+
4906+
// Create index:
4907+
{
4908+
const bson_t *keys = tmp_bson("{'x': 1}");
4909+
mongoc_index_model_t *im = mongoc_index_model_new(keys, NULL);
4910+
bool ok =
4911+
mongoc_collection_create_indexes_with_opts(coll, &im, 1, NULL /* options */, NULL /* reply */, &error);
4912+
ASSERT(!ok);
4913+
ASSERT_ERROR_CONTAINS(error, 5, 9, "w has to be a non-negative number and not greater than 50");
4914+
mongoc_index_model_destroy(im);
4915+
}
4916+
}
4917+
4918+
// Test a server reply with "writeConcernError" is considered an error:
4919+
{
4920+
// Set the default write concern on collection:
4921+
{
4922+
mongoc_write_concern_t *wc = mongoc_write_concern_new(); // Default write concern.
4923+
mongoc_collection_set_write_concern(coll, wc);
4924+
mongoc_write_concern_destroy(wc);
4925+
}
4926+
4927+
// Set a failpoint to fail with "writeConcernError":
4928+
{
4929+
const char *cmd_str = BSON_STR({
4930+
"configureFailPoint" : "failCommand",
4931+
"mode" : {"times" : 1},
4932+
"data" : {"failCommands" : ["createIndexes"], "writeConcernError" : {"code" : 123, "errmsg" : "foo"}}
4933+
});
4934+
bson_t *failpoint_cmd = bson_new_from_json((const uint8_t *)cmd_str, -1, &error);
4935+
ASSERT_OR_PRINT(failpoint_cmd, error);
4936+
bool ok = mongoc_client_command_simple(client, "admin", failpoint_cmd, NULL /* read_prefs */, &reply, &error);
4937+
ASSERT_OR_PRINT(ok, error);
4938+
bson_destroy(failpoint_cmd);
4939+
bson_destroy(&reply);
4940+
}
4941+
4942+
{
4943+
const bson_t *keys = tmp_bson("{'x': 1}");
4944+
mongoc_index_model_t *im = mongoc_index_model_new(keys, NULL);
4945+
bool ok = mongoc_collection_create_indexes_with_opts(coll, &im, 1, NULL /* opts */, NULL /* reply */, &error);
4946+
ASSERT(!ok);
4947+
ASSERT_ERROR_CONTAINS(error, MONGOC_ERROR_WRITE_CONCERN, 123, "foo");
4948+
mongoc_index_model_destroy(im);
4949+
}
4950+
}
4951+
4952+
mongoc_collection_destroy(coll);
4953+
mongoc_client_destroy(client);
4954+
}
4955+
48864956

48874957
void
48884958
test_collection_install(TestSuite *suite)
@@ -5016,4 +5086,14 @@ test_collection_install(TestSuite *suite)
50165086
// requires failpoint
50175087
test_framework_skip_if_no_failpoint);
50185088
TestSuite_AddLive(suite, "/Collection/insert_one_reports_id", test_insert_one_reports_id);
5089+
5090+
TestSuite_AddFull(suite,
5091+
"/Collection/test_create_indexes_acts_as_write_command",
5092+
test_create_indexes_acts_as_write_command,
5093+
NULL /* _dtor */,
5094+
NULL /* _ctx */,
5095+
// requires failpoint
5096+
test_framework_skip_if_no_failpoint,
5097+
// Server Version 4.4 has Wire Version 9 - w < 0 does not error on earlier versions.
5098+
test_framework_skip_if_max_wire_version_less_than_9);
50195099
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /