mirror of https://github.com/python/cpython
bpo-30727: Fix a race condition in test_threading. (#2334)
This commit is contained in:
parent
c8fb58bd79
commit
32cb968a2e
|
@ -461,21 +461,28 @@ class ConditionTests(BaseTestCase):
|
||||||
# construct. In particular, it is possible that this can no longer
|
# construct. In particular, it is possible that this can no longer
|
||||||
# be conveniently guaranteed should their implementation ever change.
|
# be conveniently guaranteed should their implementation ever change.
|
||||||
N = 5
|
N = 5
|
||||||
|
ready = []
|
||||||
results1 = []
|
results1 = []
|
||||||
results2 = []
|
results2 = []
|
||||||
phase_num = 0
|
phase_num = 0
|
||||||
def f():
|
def f():
|
||||||
cond.acquire()
|
cond.acquire()
|
||||||
|
ready.append(phase_num)
|
||||||
result = cond.wait()
|
result = cond.wait()
|
||||||
cond.release()
|
cond.release()
|
||||||
results1.append((result, phase_num))
|
results1.append((result, phase_num))
|
||||||
cond.acquire()
|
cond.acquire()
|
||||||
|
ready.append(phase_num)
|
||||||
result = cond.wait()
|
result = cond.wait()
|
||||||
cond.release()
|
cond.release()
|
||||||
results2.append((result, phase_num))
|
results2.append((result, phase_num))
|
||||||
b = Bunch(f, N)
|
b = Bunch(f, N)
|
||||||
b.wait_for_started()
|
b.wait_for_started()
|
||||||
|
# first wait, to ensure all workers settle into cond.wait() before
|
||||||
|
# we continue. See issues #8799 and #30727.
|
||||||
|
while len(ready) < 5:
|
||||||
_wait()
|
_wait()
|
||||||
|
ready.clear()
|
||||||
self.assertEqual(results1, [])
|
self.assertEqual(results1, [])
|
||||||
# Notify 3 threads at first
|
# Notify 3 threads at first
|
||||||
cond.acquire()
|
cond.acquire()
|
||||||
|
@ -487,8 +494,8 @@ class ConditionTests(BaseTestCase):
|
||||||
_wait()
|
_wait()
|
||||||
self.assertEqual(results1, [(True, 1)] * 3)
|
self.assertEqual(results1, [(True, 1)] * 3)
|
||||||
self.assertEqual(results2, [])
|
self.assertEqual(results2, [])
|
||||||
# first wait, to ensure all workers settle into cond.wait() before
|
# make sure all awaken workers settle into cond.wait()
|
||||||
# we continue. See issue #8799
|
while len(ready) < 3:
|
||||||
_wait()
|
_wait()
|
||||||
# Notify 5 threads: they might be in their first or second wait
|
# Notify 5 threads: they might be in their first or second wait
|
||||||
cond.acquire()
|
cond.acquire()
|
||||||
|
@ -500,7 +507,9 @@ class ConditionTests(BaseTestCase):
|
||||||
_wait()
|
_wait()
|
||||||
self.assertEqual(results1, [(True, 1)] * 3 + [(True, 2)] * 2)
|
self.assertEqual(results1, [(True, 1)] * 3 + [(True, 2)] * 2)
|
||||||
self.assertEqual(results2, [(True, 2)] * 3)
|
self.assertEqual(results2, [(True, 2)] * 3)
|
||||||
_wait() # make sure all workers settle into cond.wait()
|
# make sure all workers settle into cond.wait()
|
||||||
|
while len(ready) < 5:
|
||||||
|
_wait()
|
||||||
# Notify all threads: they are all in their second wait
|
# Notify all threads: they are all in their second wait
|
||||||
cond.acquire()
|
cond.acquire()
|
||||||
cond.notify_all()
|
cond.notify_all()
|
||||||
|
|
Loading…
Reference in New Issue