[3.6] bpo-31234: Join threads in tests (#3589) · python/cpython@2c1c2ca (original) (raw)
`@@ -46,28 +46,27 @@ def run(self):
`
46
46
``
47
47
`class BlockingTestMixin:
`
48
48
``
49
``
`-
def tearDown(self):
`
50
``
`-
self.t = None
`
51
``
-
52
49
`def do_blocking_test(self, block_func, block_args, trigger_func, trigger_args):
`
53
``
`-
self.t = _TriggerThread(trigger_func, trigger_args)
`
54
``
`-
self.t.start()
`
55
``
`-
self.result = block_func(*block_args)
`
56
``
`-
If block_func returned before our thread made the call, we failed!
`
57
``
`-
if not self.t.startedEvent.is_set():
`
58
``
`-
self.fail("blocking function '%r' appeared not to block" %
`
59
``
`-
block_func)
`
60
``
`-
self.t.join(10) # make sure the thread terminates
`
61
``
`-
if self.t.is_alive():
`
62
``
`-
self.fail("trigger function '%r' appeared to not return" %
`
63
``
`-
trigger_func)
`
64
``
`-
return self.result
`
``
50
`+
thread = _TriggerThread(trigger_func, trigger_args)
`
``
51
`+
thread.start()
`
``
52
`+
try:
`
``
53
`+
self.result = block_func(*block_args)
`
``
54
`+
If block_func returned before our thread made the call, we failed!
`
``
55
`+
if not thread.startedEvent.is_set():
`
``
56
`+
self.fail("blocking function '%r' appeared not to block" %
`
``
57
`+
block_func)
`
``
58
`+
return self.result
`
``
59
`+
finally:
`
``
60
`+
thread.join(10) # make sure the thread terminates
`
``
61
`+
if thread.is_alive():
`
``
62
`+
self.fail("trigger function '%r' appeared to not return" %
`
``
63
`+
trigger_func)
`
65
64
``
66
65
`# Call this instead if block_func is supposed to raise an exception.
`
67
66
`def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,
`
68
67
`trigger_args, expected_exception_class):
`
69
``
`-
self.t = _TriggerThread(trigger_func, trigger_args)
`
70
``
`-
self.t.start()
`
``
68
`+
thread = _TriggerThread(trigger_func, trigger_args)
`
``
69
`+
thread.start()
`
71
70
`try:
`
72
71
`try:
`
73
72
`block_func(*block_args)
`
`@@ -77,11 +76,11 @@ def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,
`
77
76
`self.fail("expected exception of kind %r" %
`
78
77
`expected_exception_class)
`
79
78
`finally:
`
80
``
`-
self.t.join(10) # make sure the thread terminates
`
81
``
`-
if self.t.is_alive():
`
``
79
`+
thread.join(10) # make sure the thread terminates
`
``
80
`+
if thread.is_alive():
`
82
81
`self.fail("trigger function '%r' appeared to not return" %
`
83
82
`trigger_func)
`
84
``
`-
if not self.t.startedEvent.is_set():
`
``
83
`+
if not thread.startedEvent.is_set():
`
85
84
`self.fail("trigger thread ended but event never set")
`
86
85
``
87
86
``
`@@ -159,8 +158,11 @@ def worker(self, q):
`
159
158
``
160
159
`def queue_join_test(self, q):
`
161
160
`self.cum = 0
`
``
161
`+
threads = []
`
162
162
`for i in (0,1):
`
163
``
`-
threading.Thread(target=self.worker, args=(q,)).start()
`
``
163
`+
thread = threading.Thread(target=self.worker, args=(q,))
`
``
164
`+
thread.start()
`
``
165
`+
threads.append(thread)
`
164
166
`for i in range(100):
`
165
167
`q.put(i)
`
166
168
`q.join()
`
`@@ -169,6 +171,8 @@ def queue_join_test(self, q):
`
169
171
`for i in (0,1):
`
170
172
`q.put(-1) # instruct the threads to close
`
171
173
`q.join() # verify that you can join twice
`
``
174
`+
for thread in threads:
`
``
175
`+
thread.join()
`
172
176
``
173
177
`def test_queue_task_done(self):
`
174
178
`# Test to make sure a queue task completed successfully.
`