Skip to content

Commit 13cf110

Browse files
authored
PYTHON-4633 Speed up TestCollectionChangeStream.test_uuid_representations (mongodb#1775)
1 parent dcaa42b commit 13cf110

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

test/test_change_stream.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,21 +1020,32 @@ def test_raw(self):
10201020
self.assertEqual(change["ns"]["coll"], self.watched_collection().name)
10211021
self.assertEqual(change["fullDocument"], raw_doc)
10221022

1023+
@client_context.require_version_min(4, 0) # Needed for start_at_operation_time.
10231024
def test_uuid_representations(self):
10241025
"""Test with uuid document _ids and different uuid_representation."""
1026+
optime = self.db.command("ping")["operationTime"]
1027+
self.watched_collection().insert_many(
1028+
[
1029+
{"_id": Binary(uuid.uuid4().bytes, id_subtype)}
1030+
for id_subtype in (STANDARD, PYTHON_LEGACY)
1031+
]
1032+
)
10251033
for uuid_representation in ALL_UUID_REPRESENTATIONS:
1026-
for id_subtype in (STANDARD, PYTHON_LEGACY):
1027-
options = self.watched_collection().codec_options.with_options(
1028-
uuid_representation=uuid_representation
1029-
)
1030-
coll = self.watched_collection(codec_options=options)
1031-
with coll.watch() as change_stream:
1032-
coll.insert_one({"_id": Binary(uuid.uuid4().bytes, id_subtype)})
1033-
_ = change_stream.next()
1034-
resume_token = change_stream.resume_token
1034+
options = self.watched_collection().codec_options.with_options(
1035+
uuid_representation=uuid_representation
1036+
)
1037+
coll = self.watched_collection(codec_options=options)
1038+
with coll.watch(start_at_operation_time=optime, max_await_time_ms=1) as change_stream:
1039+
_ = change_stream.next()
1040+
resume_token_1 = change_stream.resume_token
1041+
_ = change_stream.next()
1042+
resume_token_2 = change_stream.resume_token
10351043

1036-
# Should not error.
1037-
coll.watch(resume_after=resume_token)
1044+
# Should not error.
1045+
with coll.watch(resume_after=resume_token_1):
1046+
pass
1047+
with coll.watch(resume_after=resume_token_2):
1048+
pass
10381049

10391050
def test_document_id_order(self):
10401051
"""Test with document _ids that need their order preserved."""
@@ -1053,7 +1064,8 @@ def test_document_id_order(self):
10531064
# The resume token is always a document.
10541065
self.assertIsInstance(resume_token, document_class)
10551066
# Should not error.
1056-
coll.watch(resume_after=resume_token)
1067+
with coll.watch(resume_after=resume_token):
1068+
pass
10571069
coll.delete_many({})
10581070

10591071
def test_read_concern(self):

test/test_custom_types.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,7 @@ def test_grid_out_custom_opts(self):
764764
db.fs,
765765
_id=5,
766766
filename="my_file",
767-
contentType="text/html",
768767
chunkSize=1000,
769-
aliases=["foo"],
770768
metadata={"foo": "red", "bar": "blue"},
771769
bar=3,
772770
baz="hello",
@@ -780,13 +778,10 @@ def test_grid_out_custom_opts(self):
780778
self.assertEqual("my_file", two.filename)
781779
self.assertEqual(5, two._id)
782780
self.assertEqual(11, two.length)
783-
self.assertEqual("text/html", two.content_type)
784781
self.assertEqual(1000, two.chunk_size)
785782
self.assertTrue(isinstance(two.upload_date, datetime.datetime))
786-
self.assertEqual(["foo"], two.aliases)
787783
self.assertEqual({"foo": "red", "bar": "blue"}, two.metadata)
788784
self.assertEqual(3, two.bar)
789-
self.assertEqual(None, two.md5)
790785

791786
for attr in [
792787
"_id",
@@ -805,7 +800,9 @@ def test_grid_out_custom_opts(self):
805800
class ChangeStreamsWCustomTypesTestMixin:
806801
@no_type_check
807802
def change_stream(self, *args, **kwargs):
808-
return self.watched_target.watch(*args, **kwargs)
803+
stream = self.watched_target.watch(*args, max_await_time_ms=1, **kwargs)
804+
self.addCleanup(stream.close)
805+
return stream
809806

810807
@no_type_check
811808
def insert_and_check(self, change_stream, insert_doc, expected_doc):

test/test_examples.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ def test_change_streams(self):
747747
done = False
748748

749749
def insert_docs():
750+
nonlocal done
750751
while not done:
751752
db.inventory.insert_one({"username": "alice"})
752753
db.inventory.delete_one({"username": "alice"})
@@ -760,17 +761,20 @@ def insert_docs():
760761
cursor = db.inventory.watch()
761762
next(cursor)
762763
# End Changestream Example 1
764+
cursor.close()
763765

764766
# Start Changestream Example 2
765767
cursor = db.inventory.watch(full_document="updateLookup")
766768
next(cursor)
767769
# End Changestream Example 2
770+
cursor.close()
768771

769772
# Start Changestream Example 3
770773
resume_token = cursor.resume_token
771774
cursor = db.inventory.watch(resume_after=resume_token)
772775
next(cursor)
773776
# End Changestream Example 3
777+
cursor.close()
774778

775779
# Start Changestream Example 4
776780
pipeline = [
@@ -780,6 +784,7 @@ def insert_docs():
780784
cursor = db.inventory.watch(pipeline=pipeline)
781785
next(cursor)
782786
# End Changestream Example 4
787+
cursor.close()
783788
finally:
784789
done = True
785790
t.join()

test/test_sdam_monitoring_spec.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def setUp(self):
179179

180180
def create_test(scenario_def):
181181
def run_scenario(self):
182-
with client_knobs(events_queue_frequency=0.1):
182+
with client_knobs(events_queue_frequency=0.05, min_heartbeat_interval=0.05):
183183
_run_scenario(self)
184184

185185
def _run_scenario(self):
@@ -216,7 +216,7 @@ def _run(self):
216216
)
217217

218218
# Wait some time to catch possible lagging extra events.
219-
time.sleep(0.5)
219+
wait_until(lambda: topology._events.empty(), "publish lagging events")
220220

221221
i = 0
222222
while i < expected_len:
@@ -273,7 +273,9 @@ class TestSdamMonitoring(IntegrationTest):
273273
def setUpClass(cls):
274274
super().setUpClass()
275275
# Speed up the tests by decreasing the event publish frequency.
276-
cls.knobs = client_knobs(events_queue_frequency=0.1)
276+
cls.knobs = client_knobs(
277+
events_queue_frequency=0.1, heartbeat_frequency=0.1, min_heartbeat_interval=0.1
278+
)
277279
cls.knobs.enable()
278280
cls.listener = ServerAndTopologyEventListener()
279281
retry_writes = client_context.supports_transactions()

0 commit comments

Comments
 (0)