bpo-31234: Join threads in test_queue (#3586) · python/cpython@167cbde
@@ -47,28 +47,27 @@ def run(self):
47474848class BlockingTestMixin:
494950-def tearDown(self):
51-self.t = None
52-5350def do_blocking_test(self, block_func, block_args, trigger_func, trigger_args):
54-self.t = _TriggerThread(trigger_func, trigger_args)
55-self.t.start()
56-self.result = block_func(*block_args)
57-# If block_func returned before our thread made the call, we failed!
58-if not self.t.startedEvent.is_set():
59-self.fail("blocking function '%r' appeared not to block" %
60-block_func)
61-self.t.join(10) # make sure the thread terminates
62-if self.t.is_alive():
63-self.fail("trigger function '%r' appeared to not return" %
64-trigger_func)
65-return self.result
51+thread = _TriggerThread(trigger_func, trigger_args)
52+thread.start()
53+try:
54+self.result = block_func(*block_args)
55+# If block_func returned before our thread made the call, we failed!
56+if not thread.startedEvent.is_set():
57+self.fail("blocking function '%r' appeared not to block" %
58+block_func)
59+return self.result
60+finally:
61+thread.join(10) # make sure the thread terminates
62+if thread.is_alive():
63+self.fail("trigger function '%r' appeared to not return" %
64+trigger_func)
66656766# Call this instead if block_func is supposed to raise an exception.
6867def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,
6968trigger_args, expected_exception_class):
70-self.t = _TriggerThread(trigger_func, trigger_args)
71-self.t.start()
69+thread = _TriggerThread(trigger_func, trigger_args)
70+thread.start()
7271try:
7372try:
7473block_func(*block_args)
@@ -78,11 +77,11 @@ def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,
7877self.fail("expected exception of kind %r" %
7978expected_exception_class)
8079finally:
81-self.t.join(10) # make sure the thread terminates
82-if self.t.is_alive():
80+thread.join(10) # make sure the thread terminates
81+if thread.is_alive():
8382self.fail("trigger function '%r' appeared to not return" %
8483trigger_func)
85-if not self.t.startedEvent.is_set():
84+if not thread.startedEvent.is_set():
8685self.fail("trigger thread ended but event never set")
87868887@@ -160,8 +159,11 @@ def worker(self, q):
160159161160def queue_join_test(self, q):
162161self.cum = 0
162+threads = []
163163for i in (0,1):
164-threading.Thread(target=self.worker, args=(q,)).start()
164+thread = threading.Thread(target=self.worker, args=(q,))
165+thread.start()
166+threads.append(thread)
165167for i in range(100):
166168q.put(i)
167169q.join()
@@ -170,6 +172,8 @@ def queue_join_test(self, q):
170172for i in (0,1):
171173q.put(-1) # instruct the threads to close
172174q.join() # verify that you can join twice
175+for thread in threads:
176+thread.join()
173177174178def test_queue_task_done(self):
175179# Test to make sure a queue task completed successfully.