Skip to content

CXX-3204 add URI setter for server_selection_try_once #1374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/mongocxx/include/mongocxx/v_noabi/mongocxx/uri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ class uri {
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<bool>)
server_selection_try_once() const;

///
/// Sets the value of the option "serverSelectionTryOnce" in the uri.
///
/// @param val The new value to apply to as "serverSelectionTryOnce".
///
/// @throws mongocxx::v_noabi::exception if there is an error setting the option.
///
MONGOCXX_ABI_EXPORT_CDECL(void)
server_selection_try_once(bool val);

///
/// Returns the value of the option "socketTimeoutMS" if present in the uri.
///
Expand Down
6 changes: 6 additions & 0 deletions src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ bsoncxx::v_noabi::stdx::optional<bool> uri::server_selection_try_once() const {
return _bool_option(_impl->uri_t, "serverSelectionTryOnce");
}

void uri::server_selection_try_once(bool val) {
if (!mongoc_uri_set_option_as_bool(_impl->uri_t, "serverSelectionTryOnce", val)) {
throw exception{error_code::k_invalid_uri, "failed to set 'serverSelectionTryOnce' option"};
}
}

bsoncxx::v_noabi::stdx::optional<std::int32_t> uri::socket_timeout_ms() const {
return _int32_option(_impl->uri_t, "socketTimeoutMS");
}
Expand Down
42 changes: 42 additions & 0 deletions src/mongocxx/test/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,46 @@ TEST_CASE("uri::zlib_compression_level()", "[uri]") {

// End special cases.

TEST_CASE("can set server_selection_try_once", "[uri]") {
SECTION("URI with no option") {
auto uri = mongocxx::uri{"mongodb://host"};

SECTION("can set to true") {
CHECK(!uri.server_selection_try_once());
uri.server_selection_try_once(true);
CHECK(uri.server_selection_try_once());
CHECK(*uri.server_selection_try_once());
}

SECTION("can set to false") {
CHECK(!uri.server_selection_try_once());
uri.server_selection_try_once(false);
CHECK(uri.server_selection_try_once());
CHECK(!*uri.server_selection_try_once());
}
}

SECTION("URI with serverSelectionTryOnce=true") {
auto uri = mongocxx::uri{"mongodb://host/?serverSelectionTryOnce=true"};
SECTION("can overwrite to false") {
CHECK(uri.server_selection_try_once());
CHECK(*uri.server_selection_try_once());
uri.server_selection_try_once(false);
CHECK(uri.server_selection_try_once());
CHECK(!*uri.server_selection_try_once());
}
}

SECTION("Test URI with serverSelectionTryOnce=false") {
auto uri = mongocxx::uri{"mongodb://host/?serverSelectionTryOnce=false"};
SECTION("can overwrite to true") {
CHECK(uri.server_selection_try_once());
CHECK(!*uri.server_selection_try_once());
uri.server_selection_try_once(true);
CHECK(uri.server_selection_try_once());
CHECK(*uri.server_selection_try_once());
}
}
}

} // namespace