Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1556f42

Browse files
authored
gh-110666: Fix multiprocessing test_terminate() elapsed (#110667)
multiprocessing test_terminate() and test_wait_socket_slow() no longer test the CI performance: no longer check maximum elapsed time. Add CLOCK_RES constant: tolerate a difference of 100 ms.
1 parent f901f56 commit 1556f42

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

Lib/test/_test_multiprocessing.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
raise unittest.SkipTest("libasan has a pthread_create() dead lock related to thread+fork")
8585

8686

87+
# gh-110666: Tolerate a difference of 100 ms when comparing timings
88+
# (clock resolution)
89+
CLOCK_RES = 0.100
90+
91+
8792
def latin(s):
8893
return s.encode('latin')
8994

@@ -1655,8 +1660,7 @@ def _test_waitfor_timeout_f(cls, cond, state, success, sem):
16551660
dt = time.monotonic()
16561661
result = cond.wait_for(lambda : state.value==4, timeout=expected)
16571662
dt = time.monotonic() - dt
1658-
# borrow logic in assertTimeout() from test/lock_tests.py
1659-
if not result and expected * 0.6 <= dt:
1663+
if not result and (expected - CLOCK_RES) <= dt:
16601664
success.value = True
16611665

16621666
@unittest.skipUnless(HAS_SHAREDCTYPES, 'needs sharedctypes')
@@ -2678,14 +2682,11 @@ def test_make_pool(self):
26782682
p.join()
26792683

26802684
def test_terminate(self):
2681-
result = self.pool.map_async(
2682-
time.sleep, [0.1 for i in range(10000)], chunksize=1
2683-
)
2685+
# Simulate slow tasks which take "forever" to complete
2686+
args = [support.LONG_TIMEOUT for i in range(10_000)]
2687+
result = self.pool.map_async(time.sleep, args, chunksize=1)
26842688
self.pool.terminate()
2685-
join = TimingWrapper(self.pool.join)
2686-
join()
2687-
# Sanity check the pool didn't wait for all tasks to finish
2688-
self.assertLess(join.elapsed, 2.0)
2689+
self.pool.join()
26892690

26902691
def test_empty_iterable(self):
26912692
# See Issue 12157
@@ -4908,7 +4909,7 @@ class TestWait(unittest.TestCase):
49084909
def _child_test_wait(cls, w, slow):
49094910
for i in range(10):
49104911
if slow:
4911-
time.sleep(random.random()*0.1)
4912+
time.sleep(random.random() * 0.100)
49124913
w.send((i, os.getpid()))
49134914
w.close()
49144915

@@ -4948,7 +4949,7 @@ def _child_test_wait_socket(cls, address, slow):
49484949
s.connect(address)
49494950
for i in range(10):
49504951
if slow:
4951-
time.sleep(random.random()*0.1)
4952+
time.sleep(random.random() * 0.100)
49524953
s.sendall(('%s\n' % i).encode('ascii'))
49534954
s.close()
49544955

@@ -4997,25 +4998,19 @@ def test_wait_socket_slow(self):
49974998
def test_wait_timeout(self):
49984999
from multiprocessing.connection import wait
49995000

5000-
expected = 5
5001+
timeout = 5.0 # seconds
50015002
a, b = multiprocessing.Pipe()
50025003

50035004
start = time.monotonic()
5004-
res = wait([a, b], expected)
5005+
res = wait([a, b], timeout)
50055006
delta = time.monotonic() - start
50065007

50075008
self.assertEqual(res, [])
5008-
self.assertLess(delta, expected * 2)
5009-
self.assertGreater(delta, expected * 0.5)
5009+
self.assertGreater(delta, timeout - CLOCK_RES)
50105010

50115011
b.send(None)
5012-
5013-
start = time.monotonic()
50145012
res = wait([a, b], 20)
5015-
delta = time.monotonic() - start
5016-
50175013
self.assertEqual(res, [a])
5018-
self.assertLess(delta, 0.4)
50195014

50205015
@classmethod
50215016
def signal_and_sleep(cls, sem, period):

0 commit comments

Comments
 (0)