File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -377,6 +377,43 @@ 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.
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
You can’t perform that action at this time.
0 commit comments