Skip to content

Commit e74e14f

Browse files
authored
CXX-3204 add URI setter for server_selection_try_once (#1374)
1 parent 4d2be83 commit e74e14f

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/mongocxx/include/mongocxx/v_noabi/mongocxx/uri.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,16 @@ class uri {
298298
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<bool>)
299299
server_selection_try_once() const;
300300

301+
///
302+
/// Sets the value of the option "serverSelectionTryOnce" in the uri.
303+
///
304+
/// @param val The new value to apply to as "serverSelectionTryOnce".
305+
///
306+
/// @throws mongocxx::v_noabi::exception if there is an error setting the option.
307+
///
308+
MONGOCXX_ABI_EXPORT_CDECL(void)
309+
server_selection_try_once(bool val);
310+
301311
///
302312
/// Returns the value of the option "socketTimeoutMS" if present in the uri.
303313
///

src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ bsoncxx::v_noabi::stdx::optional<bool> uri::server_selection_try_once() const {
253253
return _bool_option(_impl->uri_t, "serverSelectionTryOnce");
254254
}
255255

256+
void uri::server_selection_try_once(bool val) {
257+
if (!mongoc_uri_set_option_as_bool(_impl->uri_t, "serverSelectionTryOnce", val)) {
258+
throw exception{error_code::k_invalid_uri, "failed to set 'serverSelectionTryOnce' option"};
259+
}
260+
}
261+
256262
bsoncxx::v_noabi::stdx::optional<std::int32_t> uri::socket_timeout_ms() const {
257263
return _int32_option(_impl->uri_t, "socketTimeoutMS");
258264
}

src/mongocxx/test/uri.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,46 @@ TEST_CASE("uri::zlib_compression_level()", "[uri]") {
331331

332332
// End special cases.
333333

334+
TEST_CASE("can set server_selection_try_once", "[uri]") {
335+
SECTION("URI with no option") {
336+
auto uri = mongocxx::uri{"mongodb://host"};
337+
338+
SECTION("can set to true") {
339+
CHECK(!uri.server_selection_try_once());
340+
uri.server_selection_try_once(true);
341+
CHECK(uri.server_selection_try_once());
342+
CHECK(*uri.server_selection_try_once());
343+
}
344+
345+
SECTION("can set to false") {
346+
CHECK(!uri.server_selection_try_once());
347+
uri.server_selection_try_once(false);
348+
CHECK(uri.server_selection_try_once());
349+
CHECK(!*uri.server_selection_try_once());
350+
}
351+
}
352+
353+
SECTION("URI with serverSelectionTryOnce=true") {
354+
auto uri = mongocxx::uri{"mongodb://host/?serverSelectionTryOnce=true"};
355+
SECTION("can overwrite to false") {
356+
CHECK(uri.server_selection_try_once());
357+
CHECK(*uri.server_selection_try_once());
358+
uri.server_selection_try_once(false);
359+
CHECK(uri.server_selection_try_once());
360+
CHECK(!*uri.server_selection_try_once());
361+
}
362+
}
363+
364+
SECTION("Test URI with serverSelectionTryOnce=false") {
365+
auto uri = mongocxx::uri{"mongodb://host/?serverSelectionTryOnce=false"};
366+
SECTION("can overwrite to true") {
367+
CHECK(uri.server_selection_try_once());
368+
CHECK(!*uri.server_selection_try_once());
369+
uri.server_selection_try_once(true);
370+
CHECK(uri.server_selection_try_once());
371+
CHECK(*uri.server_selection_try_once());
372+
}
373+
}
374+
}
375+
334376
} // namespace

0 commit comments

Comments
 (0)