Skip to content

Commit ddc749d

Browse files
committed
Added long docstring to test.utils_shared.delay
1 parent 222a55f commit ddc749d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

test/utils_shared.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,43 @@ def oid_generated_on_process(oid):
377377

378378

379379
def delay(sec):
380+
"""Along with a ``$where`` operator, this returns a JavaScript function that
381+
triggers an arbitrarily long-running operation on the MongoDB server.
382+
383+
This can be useful in many time-sensitive situations, such as testing
384+
client-side timeouts and signal handling.
385+
386+
Note that you must have at least one matching document in the collection,
387+
otherwise the server may choose not to invoke the ``$where`` function.
388+
389+
Examples
390+
--------
391+
Insert a document and verify a normal find:
392+
393+
>>> db.coll.insert_one({'x': 1})
394+
>>> db.coll.find_one({'x': 1})['x']
395+
1
396+
397+
The following will wait 2.5 seconds before returning:
398+
399+
>>> db.coll.find_one({'$where': delay(2.5)})['x']
400+
1
401+
402+
Use ``delay`` to trigger a KeyboardInterrupt while the server is working:
403+
404+
>>> import signal
405+
>>> def sigalarm(num, frame):
406+
... raise KeyboardInterrupt
407+
>>> signal.signal(signal.SIGALRM, sigalarm)
408+
>>> signal.alarm(1)
409+
410+
>>> raised = False
411+
>>> try:
412+
... db.coll.find_one({"$where": delay(1.5)})
413+
... except KeyboardInterrupt:
414+
... raised = True
415+
>>> assert raised
416+
"""
380417
return """function() { sleep(%f * 1000); return true; }""" % sec
381418

382419

0 commit comments

Comments
 (0)