Skip to content

Commit 73c58fb

Browse files
committed
Refactor test
1 parent 9d5f1ae commit 73c58fb

1 file changed

Lines changed: 25 additions & 20 deletions

File tree

Lib/test/test_thread.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ def func():
347347

348348
class StartNewThreadKwargsRace(unittest.TestCase):
349349

350+
def setUp(self):
351+
key = threading_helper.threading_setup()
352+
self.addCleanup(threading_helper.threading_cleanup, *key)
353+
350354
@unittest.skipUnless(support.Py_GIL_DISABLED, "GIL must be disabled")
351355
def test_dict_growsup_when_thread_start(self):
352356
# See gh-149816 - (62) Concurrent kwargs growth causes heap overwrite
@@ -364,28 +368,29 @@ def mutator(shared, stop, prefix, burst):
364368
results.append(prefix)
365369

366370
def nop(i, **kwargs):
367-
pass
371+
results.append(i)
368372

369-
DELAY = 1.0
370-
stop = thread.lock()
371-
shared = {f"base_{i}": i for i in range(20000)}
372-
n = 4
373-
for i in range(n):
374-
args=(shared, stop, f"dynamic_{i}", 1000)
375-
thread.start_new_thread(mutator, args)
373+
with threading_helper.wait_threads_exit():
374+
stop = thread.lock()
375+
shared = {f"base_{i}": i for i in range(20000)}
376+
n = 4
377+
for i in range(n):
378+
args=(shared, stop, f"dynamic_{i}", 1000)
379+
thread.start_new_thread(mutator, args)
380+
381+
snt = 16
382+
for i in range(snt):
383+
try:
384+
thread.start_new_thread(nop, (i,), shared)
385+
except RuntimeError:
386+
break
376387

377-
snt = 32
378-
for i in range(snt):
379-
try:
380-
thread.start_new_thread(nop, (i,), shared)
381-
except RuntimeError:
382-
break
383-
384-
stop.acquire()
385-
# wait for all mutator threads stop.
386-
wait_t = time.monotonic()
387-
while len(results) < n and time.monotonic() - wait_t < DELAY:
388-
time.sleep(0.01)
388+
stop.acquire()
389+
# wait for all mutator/nop threads stop.
390+
for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
391+
if len(results) == n+snt:
392+
break
393+
self.assertTrue(True, "successful test")
389394

390395

391396
class Barrier:

0 commit comments

Comments
 (0)