Kaydet (Commit) 167cbde5 authored tarafından Victor Stinner's avatar Victor Stinner Kaydeden (comit) GitHub

bpo-31234: Join threads in test_queue (#3586)

Call thread.join() to prevent the "dangling thread" warning.
üst ff40ecda
...@@ -47,28 +47,27 @@ class _TriggerThread(threading.Thread): ...@@ -47,28 +47,27 @@ class _TriggerThread(threading.Thread):
class BlockingTestMixin: class BlockingTestMixin:
def tearDown(self):
self.t = None
def do_blocking_test(self, block_func, block_args, trigger_func, trigger_args): def do_blocking_test(self, block_func, block_args, trigger_func, trigger_args):
self.t = _TriggerThread(trigger_func, trigger_args) thread = _TriggerThread(trigger_func, trigger_args)
self.t.start() thread.start()
self.result = block_func(*block_args) try:
# If block_func returned before our thread made the call, we failed! self.result = block_func(*block_args)
if not self.t.startedEvent.is_set(): # If block_func returned before our thread made the call, we failed!
self.fail("blocking function '%r' appeared not to block" % if not thread.startedEvent.is_set():
block_func) self.fail("blocking function '%r' appeared not to block" %
self.t.join(10) # make sure the thread terminates block_func)
if self.t.is_alive(): return self.result
self.fail("trigger function '%r' appeared to not return" % finally:
trigger_func) thread.join(10) # make sure the thread terminates
return self.result if thread.is_alive():
self.fail("trigger function '%r' appeared to not return" %
trigger_func)
# Call this instead if block_func is supposed to raise an exception. # Call this instead if block_func is supposed to raise an exception.
def do_exceptional_blocking_test(self,block_func, block_args, trigger_func, def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,
trigger_args, expected_exception_class): trigger_args, expected_exception_class):
self.t = _TriggerThread(trigger_func, trigger_args) thread = _TriggerThread(trigger_func, trigger_args)
self.t.start() thread.start()
try: try:
try: try:
block_func(*block_args) block_func(*block_args)
...@@ -78,11 +77,11 @@ class BlockingTestMixin: ...@@ -78,11 +77,11 @@ class BlockingTestMixin:
self.fail("expected exception of kind %r" % self.fail("expected exception of kind %r" %
expected_exception_class) expected_exception_class)
finally: finally:
self.t.join(10) # make sure the thread terminates thread.join(10) # make sure the thread terminates
if self.t.is_alive(): if thread.is_alive():
self.fail("trigger function '%r' appeared to not return" % self.fail("trigger function '%r' appeared to not return" %
trigger_func) trigger_func)
if not self.t.startedEvent.is_set(): if not thread.startedEvent.is_set():
self.fail("trigger thread ended but event never set") self.fail("trigger thread ended but event never set")
...@@ -160,8 +159,11 @@ class BaseQueueTestMixin(BlockingTestMixin): ...@@ -160,8 +159,11 @@ class BaseQueueTestMixin(BlockingTestMixin):
def queue_join_test(self, q): def queue_join_test(self, q):
self.cum = 0 self.cum = 0
threads = []
for i in (0,1): for i in (0,1):
threading.Thread(target=self.worker, args=(q,)).start() thread = threading.Thread(target=self.worker, args=(q,))
thread.start()
threads.append(thread)
for i in range(100): for i in range(100):
q.put(i) q.put(i)
q.join() q.join()
...@@ -170,6 +172,8 @@ class BaseQueueTestMixin(BlockingTestMixin): ...@@ -170,6 +172,8 @@ class BaseQueueTestMixin(BlockingTestMixin):
for i in (0,1): for i in (0,1):
q.put(-1) # instruct the threads to close q.put(-1) # instruct the threads to close
q.join() # verify that you can join twice q.join() # verify that you can join twice
for thread in threads:
thread.join()
def test_queue_task_done(self): def test_queue_task_done(self):
# Test to make sure a queue task completed successfully. # Test to make sure a queue task completed successfully.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment