1616
1717import datetime
1818import os
19+ import signal
1920import sys
2021import time
2122import unittest
@@ -481,38 +482,35 @@ def test_interrupt_signal(self):
481482 Test fix for PYTHON-294 -- make sure Connection closes its socket if it
482483 gets an interrupt while waiting to recv() from it.
483484 """
484- import pymongo
485- import signal
486-
487485 c = get_connection ()
488486 db = c .pymongo_test
489487
490- # A $where clause which takes 1 sec to execute
488+ # A $where clause which takes 1.5 sec to execute
491489 where = '''function() {
492- var d = new Date((new Date()).getTime() + 1* 1000);
490+ var d = new Date((new Date()).getTime() + 1.5 * 1000);
493491 while (d > (new Date())) { }; return true;
494492 }'''
495493
496494 # Need exactly 1 document so find() will execute its $where clause once
497495 db .drop_collection ('foo' )
498496 db .foo .insert ({'_id' : 1 }, safe = True )
499497
500- # Convert SIGALRM to SIGINT -- it's hard to schedule a SIGINT for 1/2 a
498+ # Convert SIGALRM to SIGINT -- it's hard to schedule a SIGINT for one
501499 # second in the future, but easy to schedule SIGALRM.
502500 def sigalarm (num , frame ):
503- import sys
504501 raise KeyboardInterrupt
505502
506503 old_signal_handler = signal .signal (signal .SIGALRM , sigalarm )
507- signal .setitimer ( signal . ITIMER_REAL , 0.5 , 0 )
504+ signal .alarm ( 1 )
508505 raised = False
509506 try :
510- # Need list() to actually iterate the cursor and create the
511- # cursor on the server; find is lazily evaluated. The find() will
512- # be interrupted by sigalarm() raising a KeyboardInterrupt.
513- list (db .foo .find ({'$where' : where }))
514- except KeyboardInterrupt :
515- raised = True
507+ try :
508+ # Need list() to actually iterate the cursor and create the
509+ # cursor on the server; find is lazily evaluated. The find() will
510+ # be interrupted by sigalarm() raising a KeyboardInterrupt.
511+ list (db .foo .find ({'$where' : where }))
512+ except KeyboardInterrupt :
513+ raised = True
516514 finally :
517515 signal .signal (signal .SIGALRM , old_signal_handler )
518516
@@ -528,5 +526,7 @@ def sigalarm(num, frame):
528526 list (db .foo .find ())
529527 )
530528
529+ # TODO: test raising an error in pool.discard_socket()
530+
531531if __name__ == "__main__" :
532532 unittest .main ()
0 commit comments