8000 gh-110666: Fix multiprocessing test_terminate() elapsed by vstinner · Pull Request #110667 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-110666: Fix multiprocessing test_terminate() elapsed #110667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
raise unittest.SkipTest("libasan has a pthread_create() dead lock related to thread+fork")


# gh-110666: Tolerate a difference of 100 ms when comparing timings
# (clock resolution)
CLOCK_RES = 0.100


def latin(s):
return s.encode('latin')

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

@unittest.skipUnless(HAS_SHAREDCTYPES, 'needs sharedctypes')
Expand Down Expand Up @@ -2678,14 +2682,11 @@ def test_make_pool(self):
p.join()

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

def test_empty_iterable(self):
# See Issue 12157
Expand Down Expand Up @@ -4908,7 +4909,7 @@ class TestWait(unittest.TestCase):
def _child_test_wait(cls, w, slow):
for i in range(10):
if slow:
time.sleep(random.random()*0.1)
time.sleep(random.random() * 0.100)
w.send((i, os.getpid()))
w.close()

Expand Down Expand Up @@ -4948,7 +4949,7 @@ def _child_test_wait_socket(cls, address, slow):
s.connect(address)
for i in range(10):
if slow:
time.sleep(random.random()*0.1)
time.sleep(random.random() * 0.100)
s.sendall(('%s\n' % i).encode('ascii'))
s.close()

Expand Down Expand Up @@ -4997,25 +4998,19 @@ def test_wait_socket_slow(self):
def test_wait_timeout(self):
from multiprocessing.connection import wait

expected = 5
timeout = 5.0 # seconds
a, b = multiprocessing.Pipe()

start = time.monotonic()
res = wait([a, b], expected)
res = wait([a, b], timeout)
delta = time.monotonic() - start

self.assertEqual(res, [])
self.assertLess(delta, expected * 2)
self.assertGreater(delta, expected * 0.5)
self.assertGreater(delta, timeout - CLOCK_RES)

b.send(None)

start = time.monotonic()
res = wait([a, b], 20)
delta = time.monotonic() - start

self.assertEqual(res, [a])
self.assertLess(delta, 0.4)

@classmethod
def signal_and_sleep(cls, sem, period):
Expand Down
0