summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-11-18 16:16:06 (GMT)
committerGitHub <noreply@github.com>2021-11-18 16:16:06 (GMT)
commit9450c751cc2053b1c2e03ec92ed822a41223b142 (patch)
treec8441acf90e75f855c8f40c2b712b1a5aa3fd5a7 /Lib
parent71d842b9c1e9d86ede449783e630b7a768b579d6 (diff)
downloadcpython-9450c751cc2053b1c2e03ec92ed822a41223b142.zip
cpython-9450c751cc2053b1c2e03ec92ed822a41223b142.tar.gz
cpython-9450c751cc2053b1c2e03ec92ed822a41223b142.tar.bz2
bpo-45835: Fix race condition in test_queue (GH-29601)
Some of the tests in test_queue had a race condition in which a non-sentinel value could be enqueued after the final sentinel value leading to not all the inputs being processed (and test failures). This changes feed() to enqueue a sentinel once the inputs are exhausted, which guarantees that the final queued object is a sentinel. This requires the number of feeder threads to match the number of consumer threads, but that's already the case in the relevant tests. (cherry picked from commit df3e53d86b2ad67da9ac2b5a3f56257d1f394982) Co-authored-by: Sam Gross <colesbury@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_queue.py22
1 files changed, 11 insertions, 11 deletions
diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py
index dd7f0b1..ee58c62 100644
--- a/Lib/test/test_queue.py
+++ b/Lib/test/test_queue.py
@@ -418,11 +418,12 @@ class BaseSimpleQueueTest:
def setUp(self):
self.q = self.type2test()
- def feed(self, q, seq, rnd):
+ def feed(self, q, seq, rnd, sentinel):
while True:
try:
val = seq.pop()
except IndexError:
+ q.put(sentinel)
return
q.put(val)
if rnd.random() > 0.5:
@@ -461,11 +462,10 @@ class BaseSimpleQueueTest:
return
results.append(val)
- def run_threads(self, n_feeders, n_consumers, q, inputs,
- feed_func, consume_func):
+ def run_threads(self, n_threads, q, inputs, feed_func, consume_func):
results = []
sentinel = None
- seq = inputs + [sentinel] * n_consumers
+ seq = inputs.copy()
seq.reverse()
rnd = random.Random(42)
@@ -479,11 +479,11 @@ class BaseSimpleQueueTest:
return wrapper
feeders = [threading.Thread(target=log_exceptions(feed_func),
- args=(q, seq, rnd))
- for i in range(n_feeders)]
+ args=(q, seq, rnd, sentinel))
+ for i in range(n_threads)]
consumers = [threading.Thread(target=log_exceptions(consume_func),
args=(q, results, sentinel))
- for i in range(n_consumers)]
+ for i in range(n_threads)]
with support.start_threads(feeders + consumers):
pass
@@ -541,7 +541,7 @@ class BaseSimpleQueueTest:
# Test a pair of concurrent put() and get()
q = self.q
inputs = list(range(100))
- results = self.run_threads(1, 1, q, inputs, self.feed, self.consume)
+ results = self.run_threads(1, q, inputs, self.feed, self.consume)
# One producer, one consumer => results appended in well-defined order
self.assertEqual(results, inputs)
@@ -551,7 +551,7 @@ class BaseSimpleQueueTest:
N = 50
q = self.q
inputs = list(range(10000))
- results = self.run_threads(N, N, q, inputs, self.feed, self.consume)
+ results = self.run_threads(N, q, inputs, self.feed, self.consume)
# Multiple consumers without synchronization append the
# results in random order
@@ -562,7 +562,7 @@ class BaseSimpleQueueTest:
N = 50
q = self.q
inputs = list(range(10000))
- results = self.run_threads(N, N, q, inputs,
+ results = self.run_threads(N, q, inputs,
self.feed, self.consume_nonblock)
self.assertEqual(sorted(results), inputs)
@@ -572,7 +572,7 @@ class BaseSimpleQueueTest:
N = 50
q = self.q
inputs = list(range(1000))
- results = self.run_threads(N, N, q, inputs,
+ results = self.run_threads(N, q, inputs,
self.feed, self.consume_timeout)
self.assertEqual(sorted(results), inputs)