@@ -55,18 +55,27 @@ using bsoncxx::builder::basic::make_document;
5555
5656namespace {
5757// These frequently used network calls are cached to avoid bottlenecks during tests.
58- document::value get_is_master (client const & client) {
59- static auto reply = client[" admin" ].run_command (make_document (kvp (" isMaster" , 1 )));
58+ document::value get_is_master () {
59+ static auto reply = []() {
60+ auto client = mongocxx::client{mongocxx::uri{}, test_util::add_test_server_api ()};
61+ return client[" admin" ].run_command (make_document (kvp (" isMaster" , 1 )));
62+ }();
6063 return reply;
6164}
6265
63- document::value get_server_status (client const & client) {
64- static auto status = client[" admin" ].run_command (make_document (kvp (" serverStatus" , 1 )));
66+ document::value get_server_status () {
67+ static auto status = []() {
68+ auto client = mongocxx::client{mongocxx::uri{}, test_util::add_test_server_api ()};
69+ return client[" admin" ].run_command (make_document (kvp (" serverStatus" , 1 )));
70+ }();
6571 return status;
6672}
6773
68- bsoncxx::stdx::optional<document::value> get_shards (client const & client) {
69- static auto shards = client[" config" ][" shards" ].find_one ({});
74+ bsoncxx::stdx::optional<document::value> get_shards () {
75+ static auto shards = []() {
76+ auto client = mongocxx::client{mongocxx::uri{}, test_util::add_test_server_api ()};
77+ return client[" config" ][" shards" ].find_one ({});
78+ }();
7079 return (shards) ? shards.value () : bsoncxx::stdx::optional<document::value>{};
7180}
7281} // namespace
@@ -221,8 +230,8 @@ std::int32_t compare_versions(std::string version1, std::string version2) {
221230 return 0 ;
222231}
223232
224- bool newer_than (client const & client, std::string version) {
225- auto server_version = get_server_version (client );
233+ bool newer_than (std::string version) {
234+ auto server_version = get_server_version ();
226235 return (compare_versions (server_version, version) >= 0 );
227236}
228237
@@ -248,8 +257,8 @@ options::client add_test_server_api(options::client opts) {
248257 return opts;
249258}
250259
251- std::int32_t get_max_wire_version (client const & client ) {
252- auto reply = get_is_master (client );
260+ std::int32_t get_max_wire_version () {
261+ auto reply = get_is_master ();
253262 auto max_wire_version = reply.view ()[" maxWireVersion" ];
254263 if (!max_wire_version) {
255264 // If wire version is not available (i.e. server version too old), it is assumed to be
@@ -262,18 +271,23 @@ std::int32_t get_max_wire_version(client const& client) {
262271 return max_wire_version.get_int32 ().value ;
263272}
264273
265- std::string get_server_version (client const & client ) {
266- auto output = get_server_status (client );
274+ std::string get_server_version () {
275+ auto output = get_server_status ();
267276 return bsoncxx::string::to_string (output.view ()[" version" ].get_string ().value );
268277}
269278
270- document::value get_server_params (client const & client) {
271- auto reply = client[" admin" ].run_command (make_document (kvp (" getParameter" , " *" )));
279+ document::value get_server_params () {
280+ // Cache reply.
281+ static auto reply = []() {
282+ auto client = mongocxx::client{mongocxx::uri{}, test_util::add_test_server_api ()};
283+ return client[" admin" ].run_command (make_document (kvp (" getParameter" , " *" )));
284+ }();
285+
272286 return reply;
273287}
274288
275- std::string replica_set_name (client const & client ) {
276- auto reply = get_is_master (client );
289+ std::string replica_set_name () {
290+ auto reply = get_is_master ();
277291 auto name = reply.view ()[" setName" ];
278292 if (name) {
279293 return bsoncxx::string::to_string (name.get_string ().value );
@@ -286,8 +300,8 @@ static bool is_replica_set(document::view reply) {
286300 return static_cast <bool >(reply[" setName" ]);
287301}
288302
289- bool is_replica_set (client const & client ) {
290- return is_replica_set (get_is_master (client ));
303+ bool is_replica_set () {
304+ return is_replica_set (get_is_master ());
291305}
292306
293307static bool is_sharded_cluster (document::view reply) {
@@ -300,19 +314,19 @@ static bool is_sharded_cluster(document::view reply) {
300314 return msg.get_string ().value == " isdbgrid" ;
301315}
302316
303- bool is_sharded_cluster (client const & client ) {
304- return is_sharded_cluster (get_is_master (client ));
317+ bool is_sharded_cluster () {
318+ return is_sharded_cluster (get_is_master ());
305319}
306320
307- std::string get_hosts (client const & client ) {
308- auto shards = get_shards (client );
321+ std::string get_hosts () {
322+ auto shards = get_shards ();
309323 if (shards)
310324 return string::to_string (shards->view ()[" host" ].get_string ().value );
311325 return " " ;
312326}
313327
314- std::string get_topology (client const & client ) {
315- auto const reply = get_is_master (client );
328+ std::string get_topology () {
329+ auto const reply = get_is_master ();
316330
317331 if (is_replica_set (reply)) {
318332 return " replicaset" ;
@@ -528,8 +542,8 @@ void check_outcome_collection(mongocxx::collection* coll, bsoncxx::document::vie
528542 REQUIRE (begin (actual) == end (actual));
529543}
530544
531- bool server_has_sessions_impl (client const & conn ) {
532- auto result = get_is_master (conn );
545+ bool server_has_sessions_impl () {
546+ auto result = get_is_master ();
533547 auto result_view = result.view ();
534548
535549 if (result_view[" logicalSessionTimeoutMinutes" ]) {
0 commit comments