bpo-31234: Join threads in test_queue (#3586) · python/cpython@167cbde

@@ -47,28 +47,27 @@ def run(self):

47474848

class BlockingTestMixin:

494950-

def tearDown(self):

51-

self.t = None

52-5350

def 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.

6867

def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,

6968

trigger_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()

7271

try:

7372

try:

7473

block_func(*block_args)

@@ -78,11 +77,11 @@ def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,

7877

self.fail("expected exception of kind %r" %

7978

expected_exception_class)

8079

finally:

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():

8382

self.fail("trigger function '%r' appeared to not return" %

8483

trigger_func)

85-

if not self.t.startedEvent.is_set():

84+

if not thread.startedEvent.is_set():

8685

self.fail("trigger thread ended but event never set")

87868887

@@ -160,8 +159,11 @@ def worker(self, q):

160159161160

def queue_join_test(self, q):

162161

self.cum = 0

162+

threads = []

163163

for 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)

165167

for i in range(100):

166168

q.put(i)

167169

q.join()

@@ -170,6 +172,8 @@ def queue_join_test(self, q):

170172

for i in (0,1):

171173

q.put(-1) # instruct the threads to close

172174

q.join() # verify that you can join twice

175+

for thread in threads:

176+

thread.join()

173177174178

def test_queue_task_done(self):

175179

# Test to make sure a queue task completed successfully.