Skip to content

Commit 50cda18

Browse files
authored
Remove client arg from test helpers (#1372)
* remove `client` args from test helpers The replies to `isMaster`, `serverStatus`, and `find` on `config.shards` are cached. The `client` argument is (surprisingly) ignored after the first call. Remove the argument to signal that the default client is used. * cache reply in `get_server_params` for consistency
1 parent fe7bfd3 commit 50cda18

19 files changed

+154
-149
lines changed

src/mongocxx/test/change_streams.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ TEST_CASE("Change stream options") {
9595
instance::current();
9696
client mongodb_client{uri{}, test_util::add_test_server_api()};
9797

98-
if (!test_util::is_replica_set(mongodb_client)) {
98+
if (!test_util::is_replica_set()) {
9999
SKIP("change streams require replica set");
100100
}
101101

@@ -117,7 +117,7 @@ TEST_CASE("Spec Prose Tests") {
117117
instance::current();
118118
client client{uri{}, test_util::add_test_server_api()};
119119

120-
if (!test_util::is_replica_set(client)) {
120+
if (!test_util::is_replica_set()) {
121121
SKIP("change streams require replica set");
122122
}
123123

@@ -375,7 +375,7 @@ TEST_CASE("Mock streams and error-handling") {
375375
TEST_CASE("Create streams.events and assert we can read a single event", "[min36]") {
376376
instance::current();
377377
client mongodb_client{uri{}, test_util::add_test_server_api()};
378-
if (!test_util::is_replica_set(mongodb_client)) {
378+
if (!test_util::is_replica_set()) {
379379
SKIP("change streams require replica set");
380380
}
381381

@@ -395,7 +395,7 @@ TEST_CASE("Create streams.events and assert we can read a single event", "[min36
395395
TEST_CASE("Give an invalid pipeline", "[min36]") {
396396
instance::current();
397397
client mongodb_client{uri{}, test_util::add_test_server_api()};
398-
if (!test_util::is_replica_set(mongodb_client)) {
398+
if (!test_util::is_replica_set()) {
399399
SKIP("change streams require replica set");
400400
}
401401

@@ -425,7 +425,7 @@ TEST_CASE("Documentation Examples", "[min36]") {
425425
instance::current();
426426
mongocxx::pool pool{uri{}, options::pool(test_util::add_test_server_api())};
427427
auto mongodb_client = pool.acquire();
428-
if (!test_util::is_replica_set(*mongodb_client)) {
428+
if (!test_util::is_replica_set()) {
429429
SKIP("change streams require replica set");
430430
}
431431

@@ -531,7 +531,7 @@ TEST_CASE("Documentation Examples", "[min36]") {
531531
TEST_CASE("Watch 2 collections", "[min36]") {
532532
instance::current();
533533
client mongodb_client{uri{}, test_util::add_test_server_api()};
534-
if (!test_util::is_replica_set(mongodb_client)) {
534+
if (!test_util::is_replica_set()) {
535535
SKIP("change streams require replica set");
536536
}
537537

@@ -578,7 +578,7 @@ TEST_CASE("Watch 2 collections", "[min36]") {
578578
TEST_CASE("Watch a Collection", "[min36]") {
579579
instance::current();
580580
client mongodb_client{uri{}, test_util::add_test_server_api()};
581-
if (!test_util::is_replica_set(mongodb_client)) {
581+
if (!test_util::is_replica_set()) {
582582
SKIP("change streams require replica set");
583583
}
584584

src/mongocxx/test/client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ TEST_CASE("integration tests for client metadata handshake feature") {
387387

388388
found_op = true;
389389

390-
std::string server_version = test_util::get_server_version(client);
390+
std::string server_version = test_util::get_server_version();
391391

392392
REQUIRE(op_view["clientMetadata"]);
393393
auto metadata = op_view["clientMetadata"].get_document();

src/mongocxx/test/client_helpers.cpp

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,27 @@ using bsoncxx::builder::basic::make_document;
5555

5656
namespace {
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

293307
static 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"]) {

src/mongocxx/test/client_helpers.hh

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ namespace test_util {
5252
std::int32_t compare_versions(std::string version1, std::string version2);
5353

5454
//
55-
// Returns 'true' if the server version for 'client' is at least 'version',
55+
// Returns 'true' if the server version for the default client is at least 'version',
5656
// returns 'false' otherwise.
5757
//
58-
bool newer_than(client const& client, std::string version);
58+
bool newer_than(std::string version);
5959

6060
//
6161
// Converts a hexadecimal string to an string of bytes.
@@ -77,47 +77,47 @@ std::basic_string<std::uint8_t> convert_hex_string_to_bytes(bsoncxx::stdx::strin
7777
options::client add_test_server_api(options::client opts = {});
7878

7979
//
80-
// Determines the max wire version associated with the given client, by running the "hello"
80+
// Determines the max wire version associated with the default client, by running the "hello"
8181
// command.
8282
//
8383
// Throws mongocxx::operation_exception if the operation fails, or the server reply is malformed.
8484
//
85-
std::int32_t get_max_wire_version(client const& client);
85+
std::int32_t get_max_wire_version();
8686

8787
///
88-
/// Determines the server version number by running "serverStatus".
88+
/// Determines the server version number by running "serverStatus" with the default client.
8989
///
90-
std::string get_server_version(client const& client = {uri{}, add_test_server_api()});
90+
std::string get_server_version();
9191

9292
///
93-
/// Determines the setting of all server parameters by running "getParameter, *".
93+
/// Determines the setting of all server parameters by running "getParameter, *" with the default client.
9494
///
95-
bsoncxx::document::value get_server_params(client const& client = {uri{}, add_test_server_api()});
95+
bsoncxx::document::value get_server_params();
9696

9797
///
98-
/// Get replica set name, or empty string.
98+
/// Returns the replica set name or an empty string using the default client.
9999
///
100-
std::string replica_set_name(client const& client);
100+
std::string replica_set_name();
101101

102102
///
103-
/// Determines if the server is a replica set member.
103+
/// Determines if the server is a replica set member using the default client.
104104
///
105-
bool is_replica_set(client const& client = {uri{}, add_test_server_api()});
105+
bool is_replica_set();
106106

107107
///
108-
/// Determines if the server is a sharded cluster member.
108+
/// Determines if the server is a sharded cluster member using the default client.
109109
///
110-
bool is_sharded_cluster(client const& client = {uri{}, add_test_server_api()});
110+
bool is_sharded_cluster();
111111

112112
///
113-
/// Returns "standalone", "replicaset", or "sharded".
113+
/// Returns "standalone", "replicaset", or "sharded" using the default client.
114114
///
115-
std::string get_topology(client const& client = {uri{}, add_test_server_api()});
115+
std::string get_topology();
116116

117117
///
118-
/// Returns the "host" field of the config.shards collection.
118+
/// Returns the "host" field of the config.shards collection using the default client.
119119
///
120-
std::string get_hosts(client const& client = {uri{}, add_test_server_api()});
120+
std::string get_hosts();
121121

122122
///
123123
/// Parses a JSON file at a given path and return it as a BSON document value.
@@ -204,17 +204,14 @@ auto size(Container c) -> decltype(std::distance(std::begin(c), std::end(c))) {
204204
//
205205
// Require a topology that supports sessions (a post-3.6 replica set or cluster of them).
206206
//
207-
// @param client
208-
// A connected client.
207+
// @return Whether sessions are supported by the default client's topology.
209208
//
210-
// @return Whether sessions are supported by the client's topology.
211-
//
212-
bool server_has_sessions_impl(client const& conn);
209+
bool server_has_sessions_impl();
213210

214-
#define SERVER_HAS_SESSIONS_OR_SKIP(conn) \
215-
if (!mongocxx::test_util::server_has_sessions_impl(conn)) { \
216-
SKIP("server does not support session"); \
217-
} else \
211+
#define SERVER_HAS_SESSIONS_OR_SKIP() \
212+
if (!mongocxx::test_util::server_has_sessions_impl()) { \
213+
SKIP("server does not support session"); \
214+
} else \
218215
((void)0)
219216

220217
#if defined(MONGOC_ENABLE_CLIENT_SIDE_ENCRYPTION)

src/mongocxx/test/client_session.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ TEST_CASE("session options", "[session]") {
4646

4747
client c{uri{}, test_util::add_test_server_api()};
4848

49-
SERVER_HAS_SESSIONS_OR_SKIP(c);
49+
SERVER_HAS_SESSIONS_OR_SKIP();
5050

5151
SECTION("default") {
5252
// Make sure the defaults don't cause a client exception:
@@ -176,7 +176,7 @@ TEST_CASE("session", "[session]") {
176176

177177
client c{uri{}, test_util::add_test_server_api()};
178178

179-
SERVER_HAS_SESSIONS_OR_SKIP(c);
179+
SERVER_HAS_SESSIONS_OR_SKIP();
180180

181181
auto s = c.start_session();
182182

@@ -382,7 +382,7 @@ TEST_CASE("lsid", "[session]") {
382382

383383
session_test test;
384384

385-
SERVER_HAS_SESSIONS_OR_SKIP(test.client);
385+
SERVER_HAS_SESSIONS_OR_SKIP();
386386

387387
auto s = test.client.start_session();
388388
auto db = test.client["lsid"];
@@ -669,7 +669,7 @@ TEST_CASE("lsid", "[session]") {
669669
}
670670

671671
SECTION("collection::watch") {
672-
if (!test_util::is_replica_set(test.client)) {
672+
if (!test_util::is_replica_set()) {
673673
SKIP("watch() requires replica set");
674674
}
675675

@@ -815,15 +815,15 @@ TEST_CASE("with_transaction", "[session]") {
815815

816816
session_test test;
817817

818-
SERVER_HAS_SESSIONS_OR_SKIP(test.client);
818+
SERVER_HAS_SESSIONS_OR_SKIP();
819819

820820
auto session = test.client.start_session();
821821

822822
// The following three tests are prose tests from the with_transaction spec.
823823
SECTION("prose tests for with_transaction") {
824824
SECTION("callback raises a custom error") {
825825
// Multi-document transactions require server 4.2+.
826-
if (compare_versions(get_server_version(test.client), "4.2") < 0) {
826+
if (compare_versions(get_server_version(), "4.2") < 0) {
827827
SKIP("MongoDB server 4.2 or newer required");
828828
}
829829

@@ -857,7 +857,7 @@ TEST_CASE("unacknowledged write in session", "[session]") {
857857

858858
session_test test;
859859

860-
SERVER_HAS_SESSIONS_OR_SKIP(test.client);
860+
SERVER_HAS_SESSIONS_OR_SKIP();
861861

862862
auto s = test.client.start_session();
863863
auto db = test.client["lsid"];

0 commit comments

Comments
 (0)