Skip to content

Commit

Permalink
pythongh-109974: Fix threading lock_tests: adjust timeouts
Browse files Browse the repository at this point in the history
* 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().
  • Loading branch information
vstinner committed Sep 28, 2023
1 parent f580edc commit 3e81f9c
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions Lib/test/lock_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -1068,16 +1060,19 @@ def test_single_thread(self):
b.wait()

def test_repr(self):
timeout = support.LONG_TIMEOUT
b = self.barriertype(3)
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>")

0 comments on commit 3e81f9c

Please sign in to comment.