[2.7] bpo-31234: Join threads explicitly in tests (#7406)
* Add support.wait_threads_exit(): context manager looping at exit
until the number of threads decreases to its original number.
* Add some missing thread.join()
* test_asyncore.test_send(): call explicitly t.join() because the cleanup
function is only called outside the test method, whereas the method
has a @test_support.reap_threads decorator
* test_hashlib: replace threading.Event with thread.join()
* test_thread:
* Use wait_threads_exit() context manager
* Replace test_support with support
* test_forkinthread(): check child process exit status in the
main thread to better handle error.
diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
index 471ebb4..b8d6388 100644
--- a/Lib/test/test_hashlib.py
+++ b/Lib/test/test_hashlib.py
@@ -371,25 +371,25 @@
data = smallest_data*200000
expected_hash = hashlib.sha1(data*num_threads).hexdigest()
- def hash_in_chunks(chunk_size, event):
+ def hash_in_chunks(chunk_size):
index = 0
while index < len(data):
hasher.update(data[index:index+chunk_size])
index += chunk_size
- event.set()
- events = []
+ threads = []
for threadnum in xrange(num_threads):
chunk_size = len(data) // (10**threadnum)
assert chunk_size > 0
assert chunk_size % len(smallest_data) == 0
- event = threading.Event()
- events.append(event)
- threading.Thread(target=hash_in_chunks,
- args=(chunk_size, event)).start()
+ thread = threading.Thread(target=hash_in_chunks,
+ args=(chunk_size,))
+ threads.append(thread)
- for event in events:
- event.wait()
+ for thread in threads:
+ thread.start()
+ for thread in threads:
+ thread.join()
self.assertEqual(expected_hash, hasher.hexdigest())