Skip to content

Commit ede07f4

Browse files
authored
PYTHON-3250 Speed up majority writes in test suite (#936)
1 parent 6e4e90a commit ede07f4

File tree

5 files changed

+60
-43
lines changed

5 files changed

+60
-43
lines changed

test/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,10 +1029,15 @@ def tearDown(self):
10291029
super(MockClientTest, self).tearDown()
10301030

10311031

1032+
# Global knobs to speed up the test suite.
1033+
global_knobs = client_knobs(events_queue_frequency=0.05)
1034+
1035+
10321036
def setup():
10331037
client_context.init()
10341038
warnings.resetwarnings()
10351039
warnings.simplefilter("always")
1040+
global_knobs.enable()
10361041

10371042

10381043
def _get_executors(topology):
@@ -1086,6 +1091,7 @@ def print_running_clients():
10861091

10871092

10881093
def teardown():
1094+
global_knobs.disable()
10891095
garbage = []
10901096
for g in gc.garbage:
10911097
garbage.append("GARBAGE: %r" % (g,))

test/test_encryption.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import textwrap
2525
import traceback
2626
import uuid
27-
from typing import Any
27+
from typing import Any, Dict
2828

2929
from pymongo.collection import Collection
3030

@@ -621,31 +621,30 @@ def maybe_skip_scenario(self, test):
621621
def setup_scenario(self, scenario_def):
622622
"""Override a test's setup."""
623623
key_vault_data = scenario_def["key_vault_data"]
624+
json_schema = scenario_def["json_schema"]
625+
data = scenario_def["data"]
624626
if key_vault_data:
625-
coll = client_context.client.get_database(
626-
"keyvault", write_concern=WriteConcern(w="majority"), codec_options=OPTS
627-
)["datakeys"]
628-
coll.drop()
627+
coll = client_context.client.get_database("keyvault", codec_options=OPTS)["datakeys"]
628+
coll.delete_many({})
629629
coll.insert_many(key_vault_data)
630630

631631
db_name = self.get_scenario_db_name(scenario_def)
632632
coll_name = self.get_scenario_coll_name(scenario_def)
633-
db = client_context.client.get_database(
634-
db_name, write_concern=WriteConcern(w="majority"), codec_options=OPTS
635-
)
633+
db = client_context.client.get_database(db_name, codec_options=OPTS)
636634
coll = db[coll_name]
637635
coll.drop()
638-
json_schema = scenario_def["json_schema"]
636+
wc = WriteConcern(w="majority")
637+
kwargs: Dict[str, Any] = {}
639638
if json_schema:
640-
db.create_collection(
641-
coll_name, validator={"$jsonSchema": json_schema}, codec_options=OPTS
642-
)
643-
else:
644-
db.create_collection(coll_name)
639+
kwargs["validator"] = {"$jsonSchema": json_schema}
640+
kwargs["codec_options"] = OPTS
641+
if not data:
642+
kwargs["write_concern"] = wc
643+
db.create_collection(coll_name, **kwargs)
645644

646-
if scenario_def["data"]:
645+
if data:
647646
# Load data.
648-
coll.insert_many(scenario_def["data"])
647+
coll.with_options(write_concern=wc).insert_many(scenario_def["data"])
649648

650649
def allowable_errors(self, op):
651650
"""Override expected error classes."""

test/test_retryable_reads.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,20 @@ def get_scenario_coll_name(self, scenario_def):
112112
def setup_scenario(self, scenario_def):
113113
"""Override a test's setup to support GridFS tests."""
114114
if "bucket_name" in scenario_def:
115+
data = scenario_def["data"]
115116
db_name = self.get_scenario_db_name(scenario_def)
116-
db = client_context.client.get_database(
117-
db_name, write_concern=WriteConcern(w="majority")
118-
)
119-
# Create a bucket for the retryable reads GridFS tests.
120-
client_context.client.drop_database(db_name)
121-
if scenario_def["data"]:
122-
data = scenario_def["data"]
123-
# Load data.
117+
db = client_context.client[db_name]
118+
# Create a bucket for the retryable reads GridFS tests with as few
119+
# majority writes as possible.
120+
wc = WriteConcern(w="majority")
121+
if data:
122+
db["fs.chunks"].drop()
123+
db["fs.files"].drop()
124124
db["fs.chunks"].insert_many(data["fs.chunks"])
125-
db["fs.files"].insert_many(data["fs.files"])
125+
db.get_collection("fs.files", write_concern=wc).insert_many(data["fs.files"])
126+
else:
127+
db.get_collection("fs.chunks").drop()
128+
db.get_collection("fs.files", write_concern=wc).drop()
126129
else:
127130
super(TestSpec, self).setup_scenario(scenario_def)
128131

test/unified_format.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -721,22 +721,24 @@ def should_run_on(run_on_spec):
721721
return False
722722

723723
def insert_initial_data(self, initial_data):
724-
for collection_data in initial_data:
724+
for i, collection_data in enumerate(initial_data):
725725
coll_name = collection_data["collectionName"]
726726
db_name = collection_data["databaseName"]
727727
documents = collection_data["documents"]
728728

729-
coll = self.client.get_database(db_name).get_collection(
730-
coll_name, write_concern=WriteConcern(w="majority")
731-
)
732-
coll.drop()
733-
734-
if len(documents) > 0:
735-
coll.insert_many(documents)
729+
# Setup the collection with as few majority writes as possible.
730+
db = self.client[db_name]
731+
db.drop_collection(coll_name)
732+
# Only use majority wc only on the final write.
733+
if i == len(initial_data) - 1:
734+
wc = WriteConcern(w="majority")
735+
else:
736+
wc = WriteConcern(w=1)
737+
if documents:
738+
db.get_collection(coll_name, write_concern=wc).insert_many(documents)
736739
else:
737-
# ensure collection exists
738-
result = coll.insert_one({})
739-
coll.delete_one({"_id": result.inserted_id})
740+
# Ensure collection exists
741+
db.create_collection(coll_name, write_concern=wc)
740742

741743
@classmethod
742744
def setUpClass(cls):

test/utils_spec_runner.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,20 @@ def setup_scenario(self, scenario_def):
453453
"""Allow specs to override a test's setup."""
454454
db_name = self.get_scenario_db_name(scenario_def)
455455
coll_name = self.get_scenario_coll_name(scenario_def)
456-
db = client_context.client.get_database(db_name, write_concern=WriteConcern(w="majority"))
457-
coll = db[coll_name]
458-
coll.drop()
459-
db.create_collection(coll_name)
460-
if scenario_def["data"]:
461-
# Load data.
462-
coll.insert_many(scenario_def["data"])
456+
documents = scenario_def["data"]
457+
458+
# Setup the collection with as few majority writes as possible.
459+
db = client_context.client.get_database(db_name)
460+
coll_exists = bool(db.list_collection_names(filter={"name": coll_name}))
461+
if coll_exists:
462+
db[coll_name].delete_many({})
463+
# Only use majority wc only on the final write.
464+
wc = WriteConcern(w="majority")
465+
if documents:
466+
db.get_collection(coll_name, write_concern=wc).insert_many(documents)
467+
elif not coll_exists:
468+
# Ensure collection exists.
469+
db.create_collection(coll_name, write_concern=wc)
463470

464471
def run_scenario(self, scenario_def, test):
465472
self.maybe_skip_scenario(test)

0 commit comments

Comments
 (0)