@@ -377,44 +377,50 @@ def oid_generated_on_process(oid):
377377
378378
379379def 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.
380+ """Along with a ``$where`` operator, this triggers an arbitrarily long-running
381+ operation on the server.
382382
383- This can be useful in many time-sensitive situations, such as testing
384- client-side timeouts and signal handling.
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.
385386
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.
387+ Example
388+ -------
388389
389- Examples
390- --------
391- Insert a document and verify a normal find:
390+ .. code-block:: python
392391
393- >>> db.coll.insert_one({'x' : 1})
394- >>> db.coll .find_one({'x' : 1})['x']
395- 1
392+ db.coll.insert_one({"x" : 1})
393+ db.test .find_one({"x" : 1})
394+ # {'x': 1, '_id': ObjectId('54f4e12bfba5220aa4d6dee8')}
396395
397- The following will wait 2.5 seconds before returning:
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')}
398399
399- >>> db.coll.find_one({'$where': delay(2.5)})['x']
400- 1
400+ Using `` delay`` to provoke a KeyboardInterrupt
401+ ----------------------------------------------
401402
402- Use ``delay`` to trigger a KeyboardInterrupt while the server is working:
403+ .. code-block:: python
403404
404- >>> import signal
405- >>> def sigalarm(num, frame):
406- ... raise KeyboardInterrupt
407- >>> signal.signal(signal.SIGALRM, sigalarm)
408- >>> signal.alarm(1)
405+ import signal
409406
410- >>> raised = False
411- >>> try:
412- ... db.coll.find_one({"$where": delay(1.5)})
413- ... except KeyboardInterrupt:
414- ... raised = True
415- >>> assert raised
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
416422 """
417- return """ function() { sleep(%f * 1000); return true; }"" " % sec
423+ return "function() { sleep(%f * 1000); return true; }" % sec
418424
419425
420426def camel_to_snake (camel ):
0 commit comments