Skip to content

Commit a50c2f6

Browse files
authored
Use majority concern for all change stream test collections (#1375)
1 parent e74e14f commit a50c2f6

File tree

1 file changed

+51
-44
lines changed

1 file changed

+51
-44
lines changed

src/mongocxx/test/change_streams.cpp

+51-44
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ using bsoncxx::builder::basic::make_document;
4545

4646
using namespace mongocxx;
4747

48+
mongocxx::collection
49+
make_test_coll(mongocxx::client& client, bsoncxx::stdx::string_view db_name, bsoncxx::stdx::string_view coll_name) {
50+
write_concern wc_majority;
51+
wc_majority.acknowledge_level(write_concern::level::k_majority);
52+
53+
read_concern rc_majority;
54+
rc_majority.acknowledge_level(read_concern::level::k_majority);
55+
56+
auto db = client[db_name];
57+
auto coll = db[coll_name];
58+
59+
coll.drop();
60+
coll = db.create_collection(coll_name);
61+
62+
coll.write_concern(wc_majority);
63+
coll.read_concern(rc_majority);
64+
65+
return coll;
66+
}
67+
4868
// Create a single-item document.
4969
// E.g. doc("foo", 123) creates {"foo":123}.
5070
template <typename T>
@@ -91,9 +111,9 @@ auto const watch_interpose = [](mongoc_collection_t const*, bson_t const*, bson_
91111

92112
auto const destroy_interpose = [](mongoc_change_stream_t*) -> void {};
93113

94-
TEST_CASE("Change stream options") {
114+
TEST_CASE("Change stream options", "[change_stream]") {
95115
instance::current();
96-
client mongodb_client{uri{}, test_util::add_test_server_api()};
116+
client client{uri{}, test_util::add_test_server_api()};
97117

98118
if (!test_util::is_replica_set()) {
99119
SKIP("change streams require replica set");
@@ -108,25 +128,20 @@ TEST_CASE("Change stream options") {
108128
cs_opts.resume_after(resume_after.view());
109129
cs_opts.start_after(start_after.view());
110130

111-
auto cs = mongodb_client.watch(cs_opts);
131+
auto cs = client.watch(cs_opts);
112132
REQUIRE_THROWS(cs.begin());
113133
}
114134
}
115135

116-
TEST_CASE("Spec Prose Tests") {
136+
TEST_CASE("Spec Prose Tests", "[change_stream]") {
117137
instance::current();
118138
client client{uri{}, test_util::add_test_server_api()};
119139

120140
if (!test_util::is_replica_set()) {
121141
SKIP("change streams require replica set");
122142
}
123143

124-
auto db = client["db"];
125-
auto coll = db["coll"];
126-
coll.drop();
127-
128-
write_concern wc_majority;
129-
wc_majority.majority(std::chrono::seconds(30));
144+
auto coll = make_test_coll(client, "db", "coll");
130145

131146
// As a sanity check, we implement the first prose test. The behavior tested
132147
// by the prose tests is implemented and tested by the C driver, so we won't
@@ -143,21 +158,18 @@ TEST_CASE("Spec Prose Tests") {
143158
auto doc2 = make_document(kvp("b", 2));
144159
auto doc3 = make_document(kvp("c", 3));
145160

146-
options::insert insert_opts{};
147-
insert_opts.write_concern(wc_majority);
148-
149161
{
150-
auto res = coll.insert_one(doc1.view(), insert_opts);
162+
auto res = coll.insert_one(doc1.view());
151163
REQUIRE(res);
152164
REQUIRE(res->result().inserted_count() == 1);
153165
}
154166
{
155-
auto res = coll.insert_one(doc2.view(), insert_opts);
167+
auto res = coll.insert_one(doc2.view());
156168
REQUIRE(res);
157169
REQUIRE(res->result().inserted_count() == 1);
158170
}
159171
{
160-
auto res = coll.insert_one(doc3.view(), insert_opts);
172+
auto res = coll.insert_one(doc3.view());
161173
REQUIRE(res);
162174
REQUIRE(res->result().inserted_count() == 1);
163175
}
@@ -188,14 +200,13 @@ TEST_CASE("Spec Prose Tests") {
188200
}
189201
}
190202

191-
TEST_CASE("Mock streams and error-handling") {
203+
TEST_CASE("Mock streams and error-handling", "[change_stream]") {
192204
MOCK_CHANGE_STREAM;
193205

194206
instance::current();
195-
client mongodb_client{uri{}, test_util::add_test_server_api()};
207+
client client{uri{}, test_util::add_test_server_api()};
196208
options::change_stream options{};
197-
database db = mongodb_client["streams"];
198-
collection events = db["events"];
209+
collection events = make_test_coll(client, "streams", "events");
199210

200211
// nop watch and destroy
201212
collection_watch->interpose(watch_interpose).forever();
@@ -360,9 +371,9 @@ TEST_CASE("Mock streams and error-handling") {
360371
return nullptr;
361372
});
362373

363-
mongodb_client["db"]["collection"].watch(cs_pipeline, cs_opts);
364-
mongodb_client["db"].watch(cs_pipeline, cs_opts);
365-
mongodb_client.watch(cs_pipeline, cs_opts);
374+
client["db"]["collection"].watch(cs_pipeline, cs_opts);
375+
client["db"].watch(cs_pipeline, cs_opts);
376+
client.watch(cs_pipeline, cs_opts);
366377

367378
// Ensure the interpose was called.
368379
REQUIRE(collection_watch_called);
@@ -372,35 +383,33 @@ TEST_CASE("Mock streams and error-handling") {
372383
}
373384

374385
// Put this before other tests which assume the collections already exists.
375-
TEST_CASE("Create streams.events and assert we can read a single event", "[min36]") {
386+
TEST_CASE("Create streams.events and assert we can read a single event", "[change_stream]") {
376387
instance::current();
377-
client mongodb_client{uri{}, test_util::add_test_server_api()};
388+
client client{uri{}, test_util::add_test_server_api()};
378389
if (!test_util::is_replica_set()) {
379390
SKIP("change streams require replica set");
380391
}
381392

382-
collection events = mongodb_client["streams"]["events"];
383-
events.drop();
393+
collection events = make_test_coll(client, "streams", "events");
384394

385395
events.insert_one(make_document(kvp("dummy", "doc")));
386396
change_stream stream = events.watch();
387397
events.insert_one(make_document(kvp("another", "event")));
388398
REQUIRE(std::distance(stream.begin(), stream.end()) == 1);
389399

390400
// because we watch events2 in a test
391-
auto events2 = mongodb_client["streams"]["events2"];
392-
events2.drop();
401+
auto events2 = make_test_coll(client, "streams", "events2");
393402
}
394403

395-
TEST_CASE("Give an invalid pipeline", "[min36]") {
404+
TEST_CASE("Give an invalid pipeline", "[change_stream]") {
396405
instance::current();
397-
client mongodb_client{uri{}, test_util::add_test_server_api()};
406+
client client{uri{}, test_util::add_test_server_api()};
398407
if (!test_util::is_replica_set()) {
399408
SKIP("change streams require replica set");
400409
}
401410

402411
options::change_stream options{};
403-
collection events = mongodb_client["streams"]["events"];
412+
collection events = make_test_coll(client, "streams", "events");
404413

405414
pipeline p;
406415
p.match(make_document(kvp("$foo", -1)));
@@ -421,16 +430,15 @@ TEST_CASE("Give an invalid pipeline", "[min36]") {
421430
}
422431
}
423432

424-
TEST_CASE("Documentation Examples", "[min36]") {
433+
TEST_CASE("Documentation Examples", "[change_stream]") {
425434
instance::current();
426435
mongocxx::pool pool{uri{}, options::pool(test_util::add_test_server_api())};
427-
auto mongodb_client = pool.acquire();
436+
auto client = pool.acquire();
428437
if (!test_util::is_replica_set()) {
429438
SKIP("change streams require replica set");
430439
}
431440

432-
collection events = (*mongodb_client)["streams"]["events"];
433-
collection inventory = events; // doc examples use this name
441+
collection inventory = make_test_coll(*client, "streams", "events");
434442

435443
std::atomic_bool insert_thread_done;
436444
insert_thread_done.store(false);
@@ -525,20 +533,19 @@ TEST_CASE("Documentation Examples", "[min36]") {
525533

526534
insert_thread_done = true;
527535
insert_thread.join();
528-
inventory.drop();
529536
}
530537

531-
TEST_CASE("Watch 2 collections", "[min36]") {
538+
TEST_CASE("Watch 2 collections", "[change_stream]") {
532539
instance::current();
533-
client mongodb_client{uri{}, test_util::add_test_server_api()};
540+
client client{uri{}, test_util::add_test_server_api()};
534541
if (!test_util::is_replica_set()) {
535542
SKIP("change streams require replica set");
536543
}
537544

538545
options::change_stream options{};
539546

540-
collection events = mongodb_client["streams"]["events"];
541-
collection events2 = mongodb_client["streams"]["events2"];
547+
collection events = make_test_coll(client, "streams", "events");
548+
collection events2 = make_test_coll(client, "streams", "events2");
542549

543550
change_stream x = events.watch();
544551
change_stream x2 = events.watch();
@@ -575,15 +582,15 @@ TEST_CASE("Watch 2 collections", "[min36]") {
575582
}
576583
}
577584

578-
TEST_CASE("Watch a Collection", "[min36]") {
585+
TEST_CASE("Watch a Collection", "[change_stream]") {
579586
instance::current();
580-
client mongodb_client{uri{}, test_util::add_test_server_api()};
587+
client client{uri{}, test_util::add_test_server_api()};
581588
if (!test_util::is_replica_set()) {
582589
SKIP("change streams require replica set");
583590
}
584591

585592
options::change_stream options{};
586-
collection events = mongodb_client["streams"]["events"];
593+
collection events = make_test_coll(client, "streams", "events");
587594

588595
change_stream x = events.watch();
589596

0 commit comments

Comments
 (0)