Visitar URL original
bpo-29595: Expose max_queue_size in ThreadPoolExecutor by prayerslayer 路 Pull Request #143 路 python/cpython 路 GitHub
Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add highlevel test for threadpool queue size
  • Loading branch information
Nikolaus Piccolotto
Nikolaus Piccolotto committed Oct 30, 2017
commit 23389910bfffcdb83f6e8cfbe6adc044bd8a54c0
29 changes: 19 additions & 10 deletions Lib/test/test_concurrent_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,27 @@ def test_thread_names_default(self):
t.join()

def test_default_max_queue_size(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a higher-level test, i.e. create an executor with a non-zero queue size, submit a number of tasks larger than the queue size, and check that they all get executed in the end.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea :)

executor = futures.ThreadPoolExecutor()
self.assertEqual(executor._work_queue.maxsize, 0)
with futures.ThreadPoolExecutor() as executor:
self.assertEqual(executor._work_queue.maxsize, 0)

def test_custom_max_queue_size(self):
# test custom valid and invalid values
for i in range(-10, 10):
executor = futures.ThreadPoolExecutor(max_queue_size=i)
self.assertEqual(executor._work_queue.maxsize, i)
# provided value is forwarded without checking it
for bad_value in [None, "5", False, True, 3.14]:
executor = futures.ThreadPoolExecutor(max_queue_size=bad_value)
self.assertEqual(executor._work_queue.maxsize, bad_value)
qsize = 1
with futures.ThreadPoolExecutor(max_queue_size=qsize) as executor:
# test custom queue size was passed down
self.assertEqual(executor._work_queue.maxsize, qsize)

# test executor works with custom size
n = 10

def process_item(item):
return item + 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add a small sleep() here to help the queue reach its limit at some point?


fs = [executor.submit(process_item, i) for i in range(n)]
expected_results = [process_item(i) for i in range(n)]

for f in futures.as_completed(fs, timeout=10):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, simply, self.assertEqual(sorted(f.result() for f in futures.as_completed(...)), sorted(expected_results))

result = f.result()
self.assertTrue(result in expected_results, result)


class ProcessPoolShutdownTest(ExecutorShutdownTest):
Expand Down