Use threading.Event for the race condition

This commit is contained in:
Rémi Lapeyre 2020-05-04 17:19:56 +02:00
parent 3221eb94a5
commit 13ed830ad7
1 changed files with 9 additions and 5 deletions

View File

@ -4394,9 +4394,11 @@ class _TestPollEintr(BaseTestCase):
# #
class SlowPicklable: class SlowPicklable:
def __init__(self, event):
self.event = event
def __reduce__(self): def __reduce__(self):
from time import sleep self.event.wait()
sleep(1)
return (SlowPicklable, ()) return (SlowPicklable, ())
@ -4417,9 +4419,9 @@ class TestInvalidHandle(unittest.TestCase):
self.assertRaises((ValueError, OSError), self.assertRaises((ValueError, OSError),
multiprocessing.connection.Connection, -1) multiprocessing.connection.Connection, -1)
def race_condition(self, parent): def race_condition(self, parent, event):
try: try:
parent.send(SlowPicklable()) parent.send(SlowPicklable(event))
except Exception as e: except Exception as e:
# It's not possible to mark a test failed in a thread so we send # It's not possible to mark a test failed in a thread so we send
# the exception back to the main thread. # the exception back to the main thread.
@ -4430,9 +4432,11 @@ class TestInvalidHandle(unittest.TestCase):
def test_closed_handled(self): def test_closed_handled(self):
parent, child = multiprocessing.Pipe() parent, child = multiprocessing.Pipe()
t = threading.Thread(target=self.race_condition, args=(parent,)) e = threading.Event()
t = threading.Thread(target=self.race_condition, args=(parent, e))
t.start() t.start()
parent.close() parent.close()
e.set()
t.join() t.join()
self.assertIsInstance(self.exc, OSError) self.assertIsInstance(self.exc, OSError)
self.assertEqual(str(self.exc), "handle is closed") self.assertEqual(str(self.exc), "handle is closed")