From f8fb3fa4be6537dc581581033e6a99f24dd62ed7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 28 Sep 2023 23:18:54 +0200 Subject: [PATCH] gh-109974: Fix threading lock_tests: adjust timeouts * test_set_and_clear(): increase event wait() timeout. Remove sleep after Bunch.wait_for_started(). * BarrierTests.test_repr(): remove sleep after wait_for_started(). Use way longer timeout for Barrier.wait() timeout. * test_thread_leak() no longer needs to count len(threading.enumerate()): Bunch uses threading_helper.wait_threads_exit() internally which does it in wait_for_finished(). --- Lib/test/lock_tests.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py index e53e24b18f27609..06eba1555a2e2a1 100644 --- a/Lib/test/lock_tests.py +++ b/Lib/test/lock_tests.py @@ -174,17 +174,10 @@ def test_thread_leak(self): def f(): lock.acquire() lock.release() - n = len(threading.enumerate()) + # We run many threads in the hope that existing threads ids won't # be recycled. Bunch(f, 15).wait_for_finished() - if len(threading.enumerate()) != n: - # There is a small window during which a Thread instance's - # target function has finished running, but the Thread is still - # alive and registered. Avoid spurious failures by waiting a - # bit more (seen on a buildbot). - time.sleep(0.4) - self.assertEqual(n, len(threading.enumerate())) def test_timeout(self): lock = self.locktype() @@ -464,17 +457,15 @@ def f(): self.assertTrue(r) def test_set_and_clear(self): - # Issue #13502: check that wait() returns true even when the event is + # gh-57711: check that wait() returns true even when the event is # cleared before the waiting thread is woken up. evt = self.eventtype() results = [] - timeout = 0.250 N = 5 def f(): - results.append(evt.wait(timeout * 4)) + results.append(evt.wait(support.LONG_TIMEOUT)) b = Bunch(f, N) b.wait_for_started() - time.sleep(timeout) evt.set() evt.clear() b.wait_for_finished() @@ -636,7 +627,7 @@ def test_waitfor(self): state = 0 def f(): with cond: - result = cond.wait_for(lambda : state==4) + result = cond.wait_for(lambda: state == 4) self.assertTrue(result) self.assertEqual(state, 4) b = Bunch(f, 1) @@ -660,6 +651,7 @@ def f(): self.assertFalse(result) self.assertTimeout(dt, 0.1) success.append(None) + b = Bunch(f, 1) b.wait_for_started() # Only increment 3 times, so state == 4 is never reached. @@ -1069,15 +1061,18 @@ def test_single_thread(self): def test_repr(self): b = self.barriertype(3) + timeout = support.LONG_TIMEOUT self.assertRegex(repr(b), r"<\w+\.Barrier at .*: waiters=0/3>") def f(): - b.wait(3) + b.wait(timeout) bunch = Bunch(f, 2) + bunch.wait_for_started() - time.sleep(0.2) self.assertRegex(repr(b), r"<\w+\.Barrier at .*: waiters=2/3>") - b.wait(3) + + b.wait(timeout) bunch.wait_for_finished() self.assertRegex(repr(b), r"<\w+\.Barrier at .*: waiters=0/3>") + b.abort() self.assertRegex(repr(b), r"<\w+\.Barrier at .*: broken>")