@@ -377,7 +377,50 @@ def oid_generated_on_process(oid):
377377
378378
379379def delay (sec ):
380- return """function() { sleep(%f * 1000); return true; }""" % sec
380+ """Along with a ``$where`` operator, this triggers an arbitrarily long-running
381+ operation on the server.
382+
383+ This can be useful in time-sensitive tests (e.g., timeouts, signals).
384+ Note that you must have at least one document in the collection or the
385+ server may decide not to sleep at all.
386+
387+ Example
388+ -------
389+
390+ .. code-block:: python
391+
392+ db.coll.insert_one({"x": 1})
393+ db.test.find_one({"x": 1})
394+ # {'x': 1, '_id': ObjectId('54f4e12bfba5220aa4d6dee8')}
395+
396+ # The following will wait 2.5 seconds before returning.
397+ db.test.find_one({"$where": delay(2.5)})
398+ # {'x': 1, '_id': ObjectId('54f4e12bfba5220aa4d6dee8')}
399+
400+ Using ``delay`` to provoke a KeyboardInterrupt
401+ ----------------------------------------------
402+
403+ .. code-block:: python
404+
405+ import signal
406+
407+ # Raise KeyboardInterrupt in 1 second
408+ def sigalarm(num, frame):
409+ raise KeyboardInterrupt
410+
411+
412+ signal.signal(signal.SIGALRM, sigalarm)
413+ signal.alarm(1)
414+
415+ raised = False
416+ try:
417+ clxn.find_one({"$where": delay(1.5)})
418+ except KeyboardInterrupt:
419+ raised = True
420+
421+ assert raised
422+ """
423+ return "function() { sleep(%f * 1000); return true; }" % sec
381424
382425
383426def camel_to_snake (camel ):
0 commit comments