Skip to content

Commit cf26301

Browse files
authored
PYTHON-4677 Specify how maxTimeMS can be set for explain helpers (#2439)
1 parent 55d399b commit cf26301

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

pymongo/asynchronous/cursor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,8 @@ async def explain(self) -> _DocumentType:
767767
:meth:`~pymongo.asynchronous.database.AsyncDatabase.command` to run the explain
768768
command directly.
769769
770+
.. note:: The timeout of this method can be set using :func:`pymongo.timeout`.
771+
770772
.. seealso:: The MongoDB documentation on `explain <https://dochub.mongodb.org/core/explain>`_.
771773
"""
772774
c = self.clone()

pymongo/synchronous/cursor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,8 @@ def explain(self) -> _DocumentType:
765765
:meth:`~pymongo.database.Database.command` to run the explain
766766
command directly.
767767
768+
.. note:: The timeout of this method can be set using :func:`pymongo.timeout`.
769+
768770
.. seealso:: The MongoDB documentation on `explain <https://dochub.mongodb.org/core/explain>`_.
769771
"""
770772
c = self.clone()

test/asynchronous/test_cursor.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,24 @@ async def test_explain_with_read_concern(self):
362362
self.assertEqual(len(started), 1)
363363
self.assertNotIn("readConcern", started[0].command)
364364

365+
# https://github.com/mongodb/specifications/blob/master/source/crud/tests/README.md#14-explain-helpers-allow-users-to-specify-maxtimems
366+
async def test_explain_csot(self):
367+
# Create a MongoClient with command monitoring enabled (referred to as client).
368+
listener = AllowListEventListener("explain")
369+
client = await self.async_rs_or_single_client(event_listeners=[listener])
370+
371+
# Create a collection, referred to as collection, with the namespace explain-test.collection.
372+
collection = client["explain-test"]["collection"]
373+
374+
# Run an explained find on collection. The find will have the query predicate { name: 'john doe' }. Specify a maxTimeMS value of 2000ms for the explain.
375+
with pymongo.timeout(2.0):
376+
self.assertTrue(await collection.find({"name": "john doe"}).explain())
377+
378+
# Obtain the command started event for the explain. Confirm that the top-level explain command should has a maxTimeMS value of 2000.
379+
started = listener.started_events
380+
self.assertEqual(len(started), 1)
381+
assert 1500 < started[0].command["maxTimeMS"] <= 2000
382+
365383
async def test_hint(self):
366384
db = self.db
367385
with self.assertRaises(TypeError):

test/test_cursor.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,24 @@ def test_explain_with_read_concern(self):
354354
self.assertEqual(len(started), 1)
355355
self.assertNotIn("readConcern", started[0].command)
356356

357+
# https://github.com/mongodb/specifications/blob/master/source/crud/tests/README.md#14-explain-helpers-allow-users-to-specify-maxtimems
358+
def test_explain_csot(self):
359+
# Create a MongoClient with command monitoring enabled (referred to as client).
360+
listener = AllowListEventListener("explain")
361+
client = self.rs_or_single_client(event_listeners=[listener])
362+
363+
# Create a collection, referred to as collection, with the namespace explain-test.collection.
364+
collection = client["explain-test"]["collection"]
365+
366+
# Run an explained find on collection. The find will have the query predicate { name: 'john doe' }. Specify a maxTimeMS value of 2000ms for the explain.
367+
with pymongo.timeout(2.0):
368+
self.assertTrue(collection.find({"name": "john doe"}).explain())
369+
370+
# Obtain the command started event for the explain. Confirm that the top-level explain command should has a maxTimeMS value of 2000.
371+
started = listener.started_events
372+
self.assertEqual(len(started), 1)
373+
assert 1500 < started[0].command["maxTimeMS"] <= 2000
374+
357375
def test_hint(self):
358376
db = self.db
359377
with self.assertRaises(TypeError):

0 commit comments

Comments
 (0)