aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/_test_multiprocessing.py
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2024-06-13 21:03:01 +0200
committerGitHub <noreply@github.com>2024-06-13 19:03:01 +0000
commita3711afefa7a520b3de01be3b2367cb830d1fc84 (patch)
treea8105b15d3d57bd1cc9e30f312c07c820fc31ef3 /Lib/test/_test_multiprocessing.py
parent6674c63dc7bb175acc997ddcb799e8dbbafd2968 (diff)
downloadcpython-a3711afefa7a520b3de01be3b2367cb830d1fc84.tar.gz
cpython-a3711afefa7a520b3de01be3b2367cb830d1fc84.zip
gh-120012: clarify the behaviour of `multiprocessing.Queue.empty` on closed queues. (GH-120102)
* improve doc for `multiprocessing.Queue.empty` * add tests for checking emptiness of queues Co-authored-by: Gregory P. Smith <greg@krypto.org>
Diffstat (limited to 'Lib/test/_test_multiprocessing.py')
-rw-r--r--Lib/test/_test_multiprocessing.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index 301541a666e..4b3a0645cfc 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -1332,6 +1332,23 @@ class _TestQueue(BaseTestCase):
self.assertTrue(not_serializable_obj.reduce_was_called)
self.assertTrue(not_serializable_obj.on_queue_feeder_error_was_called)
+ def test_closed_queue_empty_exceptions(self):
+ # Assert that checking the emptiness of an unused closed queue
+ # does not raise an OSError. The rationale is that q.close() is
+ # a no-op upon construction and becomes effective once the queue
+ # has been used (e.g., by calling q.put()).
+ for q in multiprocessing.Queue(), multiprocessing.JoinableQueue():
+ q.close() # this is a no-op since the feeder thread is None
+ q.join_thread() # this is also a no-op
+ self.assertTrue(q.empty())
+
+ for q in multiprocessing.Queue(), multiprocessing.JoinableQueue():
+ q.put('foo') # make sure that the queue is 'used'
+ q.close() # close the feeder thread
+ q.join_thread() # make sure to join the feeder thread
+ with self.assertRaisesRegex(OSError, 'is closed'):
+ q.empty()
+
def test_closed_queue_put_get_exceptions(self):
for q in multiprocessing.Queue(), multiprocessing.JoinableQueue():
q.close()
@@ -5815,6 +5832,15 @@ class TestSimpleQueue(unittest.TestCase):
finally:
parent_can_continue.set()
+ def test_empty_exceptions(self):
+ # Assert that checking emptiness of a closed queue raises
+ # an OSError, independently of whether the queue was used
+ # or not. This differs from Queue and JoinableQueue.
+ q = multiprocessing.SimpleQueue()
+ q.close() # close the pipe
+ with self.assertRaisesRegex(OSError, 'is closed'):
+ q.empty()
+
def test_empty(self):
queue = multiprocessing.SimpleQueue()
child_can_start = multiprocessing.Event()