cpython: 020260ec44f2 (original) (raw)
Mercurial > cpython
changeset 74074:020260ec44f2
Issue #11867: Make test_mailbox.test_lock_conflict deterministic (and fix a race condition). [#11867]
Charles-François Natali neologix@free.fr | |
---|---|
date | Mon, 19 Dec 2011 12:19:52 +0100 |
parents | 6452edbc5f12(current diff)0053b7c68a02(diff) |
children | c706f76c9ea8 |
files | Lib/test/test_mailbox.py |
diffstat | 1 files changed, 21 insertions(+), 9 deletions(-)[+] [-] Lib/test/test_mailbox.py 30 |
line wrap: on
line diff
--- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -17,6 +17,10 @@ try: import fcntl except ImportError: pass +try:
class TestBase(unittest.TestCase): @@ -993,28 +997,36 @@ class _TestMboxMMDF(TestMailbox): self.assertEqual(contents, f.read()) self._box = self._factory(self._path)
- @unittest.skipUnless(hasattr(os, 'fork'), "Test needs fork().")
- @unittest.skipUnless(multiprocessing, "Test needs multiprocessing.") def test_lock_conflict(self):
# Fork off a subprocess that will lock the file for 2 seconds,[](#l1.21)
# unlock it, and then exit.[](#l1.22)
if not hasattr(os, 'fork'):[](#l1.23)
return[](#l1.24)
# Fork off a child process that will lock the mailbox temporarily,[](#l1.25)
# unlock it and exit.[](#l1.26)
ready = multiprocessing.Event()[](#l1.27)
done = multiprocessing.Event()[](#l1.28)
+ pid = os.fork() if pid == 0:
# In the child, lock the mailbox.[](#l1.32)
# child[](#l1.33) try:[](#l1.34)
# lock the mailbox, and signal the parent it can proceed[](#l1.35) self._box.lock()[](#l1.36)
time.sleep(2)[](#l1.37)
ready.set()[](#l1.38)
# wait until the parent is done, and unlock the mailbox[](#l1.40)
done.wait(5)[](#l1.41) self._box.unlock()[](#l1.42) finally:[](#l1.43) os._exit(0)[](#l1.44)
# In the parent, sleep a bit to give the child time to acquire[](#l1.46)
# the lock.[](#l1.47)
time.sleep(0.5)[](#l1.48)
# In the parent, wait until the child signals it locked the mailbox.[](#l1.49)
ready.wait(5)[](#l1.50) try:[](#l1.51) self.assertRaises(mailbox.ExternalClashError,[](#l1.52) self._box.lock)[](#l1.53) finally:[](#l1.54)
# Signal the child it can now release the lock and exit.[](#l1.55)
done.set()[](#l1.56) # Wait for child to exit. Locking should now succeed.[](#l1.57) exited_pid, status = os.waitpid(pid, 0)[](#l1.58)