changeset: 89634:99fd1c21ed25 tag: tip user: Victor Stinner date: Thu Mar 13 16:49:08 2014 +0100 files: Lib/test/fork_wait.py Lib/test/lock_tests.py Lib/test/support/__init__.py Lib/test/test_docxmlrpc.py Lib/test/test_faulthandler.py Lib/test/test_fork1.py Lib/test/test_socket.py Lib/test/test_threadsignals.py Lib/test/test_time.py description: Test short timings diff -r 5153cda5092e -r 99fd1c21ed25 Lib/test/fork_wait.py --- a/Lib/test/fork_wait.py Thu Mar 13 16:09:43 2014 +0100 +++ b/Lib/test/fork_wait.py Thu Mar 13 16:49:08 2014 +0100 @@ -48,7 +48,12 @@ class ForkWait(unittest.TestCase): for i in range(NUM_THREADS): _thread.start_new(self.f, (i,)) - time.sleep(LONGSLEEP) + # busy-loop to wait for threads + deadline = time.monotonic() + LONGSLEEP + while len(self.alive) < NUM_THREADS: + time.sleep(support.TEST_SHORT_SLEEP) + if time.monotonic() <= deadline: + break a = sorted(self.alive.keys()) self.assertEqual(a, list(range(NUM_THREADS))) diff -r 5153cda5092e -r 99fd1c21ed25 Lib/test/lock_tests.py --- a/Lib/test/lock_tests.py Thu Mar 13 16:09:43 2014 +0100 +++ b/Lib/test/lock_tests.py Thu Mar 13 16:49:08 2014 +0100 @@ -881,12 +881,13 @@ class BarrierTests(BaseTestCase): Test the barrier's default timeout """ # create a barrier with a low default timeout - barrier = self.barriertype(self.N, timeout=support.TEST_SLEEP) + timeout = max(support.TEST_SLEEP, 0.010) + barrier = self.barriertype(self.N, timeout=timeout) def f(): i = barrier.wait() if i == self.N // 2: # One thread is later than the default timeout. - time.sleep(support.TEST_SLEEP * 2) + time.sleep(timeout * 2) self.assertRaises(threading.BrokenBarrierError, barrier.wait) self.run_threads(f) diff -r 5153cda5092e -r 99fd1c21ed25 Lib/test/test_docxmlrpc.py --- a/Lib/test/test_docxmlrpc.py Thu Mar 13 16:09:43 2014 +0100 +++ b/Lib/test/test_docxmlrpc.py Thu Mar 13 16:49:08 2014 +0100 @@ -87,10 +87,11 @@ class DocXMLRPCHTTPGETServer(unittest.Te threading.Thread(target=server, args=(self.evt, 1)).start() # wait for port to be assigned - n = 1000 - while n > 0 and PORT is None: + deadline = time.monotonic() + 5.0 + while PORT is None: time.sleep(support.TEST_SHORT_SLEEP) - n -= 1 + if time.monotonic() > deadline: + break self.client = http.client.HTTPConnection("localhost:%d" % PORT) diff -r 5153cda5092e -r 99fd1c21ed25 Lib/test/test_faulthandler.py --- a/Lib/test/test_faulthandler.py Thu Mar 13 16:09:43 2014 +0100 +++ b/Lib/test/test_faulthandler.py Thu Mar 13 16:49:08 2014 +0100 @@ -17,7 +17,7 @@ try: except ImportError: HAVE_THREADS = False -TIMEOUT = support.TEST_SLEEP +TIMEOUT = max(support.TEST_SLEEP, 0.005) def expected_traceback(lineno1, lineno2, header, min_count=1): regex = header diff -r 5153cda5092e -r 99fd1c21ed25 Lib/test/test_fork1.py --- a/Lib/test/test_fork1.py Thu Mar 13 16:09:43 2014 +0100 +++ b/Lib/test/test_fork1.py Thu Mar 13 16:49:08 2014 +0100 @@ -19,7 +19,8 @@ get_attribute(os, 'fork') class ForkTest(ForkWait): def wait_impl(self, cpid): - for i in range(10): + deadline = time.monotonic() + 3.0 + while time.monotonic() <= deadline: # waitpid() shouldn't hang, but some of the buildbots seem to hang # in the forking tests. This is an attempt to fix the problem. spid, status = os.waitpid(cpid, os.WNOHANG) diff -r 5153cda5092e -r 99fd1c21ed25 Lib/test/test_socket.py --- a/Lib/test/test_socket.py Thu Mar 13 16:09:43 2014 +0100 +++ b/Lib/test/test_socket.py Thu Mar 13 16:49:08 2014 +0100 @@ -3679,6 +3679,10 @@ class InterruptedSendTimeoutTest(Interru @unittest.skipUnless(thread, 'Threading required for this test.') class TCPCloserTest(ThreadedTCPSocketTest): + def setUp(self): + super().setUp() + self.close_evt = threading.Event() + def testClose(self): conn, addr = self.serv.accept() conn.close() @@ -3691,10 +3695,12 @@ class TCPCloserTest(ThreadedTCPSocketTes # Calling close() many times should be safe. conn.close() conn.close() + self.close_evt.set() def _testClose(self): self.cli.connect((HOST, self.port)) time.sleep(support.TEST_SLEEP) + self.close_evt.wait() @unittest.skipUnless(hasattr(socket, 'socketpair'), 'test needs socket.socketpair()') @@ -3825,7 +3831,8 @@ class NonBlockingTCPTests(ThreadedTCPSoc self.fail("Error trying to do accept after select.") def _testAccept(self): - time.sleep(support.TEST_SLEEP) + # wait until the server is accepting sockets + time.sleep(max(support.TEST_SLEEP, 0.010)) self.cli.connect((HOST, self.port)) def testConnect(self): diff -r 5153cda5092e -r 99fd1c21ed25 Lib/test/test_threadsignals.py --- a/Lib/test/test_threadsignals.py Thu Mar 13 16:09:43 2014 +0100 +++ b/Lib/test/test_threadsignals.py Thu Mar 13 16:49:08 2014 +0100 @@ -8,6 +8,7 @@ from test import support from test.support import run_unittest, import_module thread = import_module('_thread') import time +import threading if (sys.platform[:3] == 'win'): raise unittest.SkipTest("Can't test signal on %s" % sys.platform) @@ -136,23 +137,31 @@ class ThreadSignals(unittest.TestCase): self.sig_recvd = True old_handler = signal.signal(signal.SIGUSR1, my_handler) try: + event1 = threading.Event() + event2 = threading.Event() + def other_thread(): # Acquire the lock in a non-main thread, so this test works for # RLocks. lock.acquire() # Wait until the main thread is blocked in the lock acquire, and # then wake it up with this. - time.sleep(support.TEST_SLEEP) + event1.set() + event2.wait() + time.sleep(max(support.TEST_SLEEP, 0.001)) os.kill(process_pid, signal.SIGUSR1) # Let the main thread take the interrupt, handle it, and retry # the lock acquisition. Then we'll let it run. time.sleep(support.TEST_SHORT_SLEEP) lock.release() thread.start_new_thread(other_thread, ()) - # Wait until we can't acquire it without blocking... - while lock.acquire(blocking=False): - lock.release() - time.sleep(support.TEST_SHORT_SLEEP) + + # wait until the thread acquired the lock + event1.wait() + + # ask the thread to release the lock + event2.set() + self.assertFalse(self.sig_recvd) result = lock.acquire() # Block while we receive a signal. self.assertTrue(self.sig_recvd) self.assertTrue(result) diff -r 5153cda5092e -r 99fd1c21ed25 Lib/test/test_time.py --- a/Lib/test/test_time.py Thu Mar 13 16:09:43 2014 +0100 +++ b/Lib/test/test_time.py Thu Mar 13 16:49:08 2014 +0100 @@ -389,12 +389,12 @@ class TimeTestCase(unittest.TestCase): # monotonic() includes time elapsed during a sleep t1 = time.monotonic() - time.sleep(0.5) + time.sleep(0.500) t2 = time.monotonic() dt = t2 - t1 self.assertGreater(t2, t1) # Issue #20101: On some Windows machines, dt may be slightly low - support.check_time_delta(0.5, dt, 0.5, clock='monotonic') + support.check_time_delta(0.500, dt, 0.501, clock='monotonic') # monotonic() is a monotonic but non adjustable clock info = time.get_clock_info('monotonic')