@@ -887,6 +887,73 @@ def test_timeout(self):
887887 self .assertEqual (res , False )
888888 self .assertTimingAlmostEqual (wait .elapsed , TIMEOUT1 )
889889
890+ @classmethod
891+ def _test_waitfor_f (cls , cond , state ):
892+ with cond :
893+ state .value = 0
894+ cond .notify ()
895+ result = cond .wait_for (lambda : state .value == 4 )
896+ if not result or state .value != 4 :
897+ sys .exit (1 )
898+
899+ @unittest .skipUnless (HAS_SHAREDCTYPES , 'needs sharedctypes' )
900+ def test_waitfor (self ):
901+ # based on test in test/lock_tests.py
902+ cond = self .Condition ()
903+ state = self .Value ('i' , - 1 )
904+
905+ p = self .Process (target = self ._test_waitfor_f , args = (cond , state ))
906+ p .daemon = True
907+ p .start ()
908+
909+ with cond :
910+ result = cond .wait_for (lambda : state .value == 0 )
911+ self .assertTrue (result )
912+ self .assertEqual (state .value , 0 )
913+
914+ for i in range (4 ):
915+ time .sleep (0.01 )
916+ with cond :
917+ state .value += 1
918+ cond .notify ()
919+
920+ p .join (5 )
921+ self .assertFalse (p .is_alive ())
922+ self .assertEqual (p .exitcode , 0 )
923+
924+ @classmethod
925+ def _test_waitfor_timeout_f (cls , cond , state , success ):
926+ with cond :
927+ expected = 0.1
928+ dt = time .time ()
929+ result = cond .wait_for (lambda : state .value == 4 , timeout = expected )
930+ dt = time .time () - dt
931+ # borrow logic in assertTimeout() from test/lock_tests.py
932+ if not result and expected * 0.6 < dt < expected * 10.0 :
933+ success .value = True
934+
935+ @unittest .skipUnless (HAS_SHAREDCTYPES , 'needs sharedctypes' )
936+ def test_waitfor_timeout (self ):
937+ # based on test in test/lock_tests.py
938+ cond = self .Condition ()
939+ state = self .Value ('i' , 0 )
940+ success = self .Value ('i' , False )
941+
942+ p = self .Process (target = self ._test_waitfor_timeout_f ,
943+ args = (cond , state , success ))
944+ p .daemon = True
945+ p .start ()
946+
947+ # Only increment 3 times, so state == 4 is never reached.
948+ for i in range (3 ):
949+ time .sleep (0.01 )
950+ with cond :
951+ state .value += 1
952+ cond .notify ()
953+
954+ p .join (5 )
955+ self .assertTrue (success .value )
956+
890957
891958class _TestEvent (BaseTestCase ):
892959
0 commit comments