From d9a75096f1042eab3367458856e9061c7bd5127c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 14 Sep 2017 17:09:52 +0200 Subject: [PATCH 1/2] bpo-31234: Join threads in test_hashlib Use thread.join() to wait until the parallel hash tasks complete rather than using events. Calling thread.join() prevent "dangling thread" warnings. --- Lib/test/test_hashlib.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index 5c8f090116fabd5..ded1b6069404b1c 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -749,25 +749,25 @@ def test_threaded_hashing(self): 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 range(num_threads): chunk_size = len(data) // (10**threadnum) self.assertGreater(chunk_size, 0) self.assertEqual(chunk_size % len(smallest_data), 0) - event = threading.Event() - events.append(event) - threading.Thread(target=hash_in_chunks, - args=(chunk_size, event)).start() - - for event in events: - event.wait() + thread = threading.Thread(target=hash_in_chunks, + args=(chunk_size,)) + threads.append(thread) + + for thread in threads: + thread.start() + for thread in threads: + thread.join() self.assertEqual(expected_hash, hasher.hexdigest()) From 47833265666f52c3d23f6eddb6b69a3599b3d750 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 14 Sep 2017 17:11:15 +0200 Subject: [PATCH 2/2] test_hashlib: minor PEP 8 coding style fixes --- Lib/test/test_hashlib.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index ded1b6069404b1c..90e6a563a72c72e 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -746,18 +746,18 @@ def test_threaded_hashing(self): hasher = hashlib.sha1() num_threads = 5 smallest_data = b'swineflu' - data = smallest_data*200000 + data = smallest_data * 200000 expected_hash = hashlib.sha1(data*num_threads).hexdigest() def hash_in_chunks(chunk_size): index = 0 while index < len(data): - hasher.update(data[index:index+chunk_size]) + hasher.update(data[index:index + chunk_size]) index += chunk_size threads = [] for threadnum in range(num_threads): - chunk_size = len(data) // (10**threadnum) + chunk_size = len(data) // (10 ** threadnum) self.assertGreater(chunk_size, 0) self.assertEqual(chunk_size % len(smallest_data), 0) thread = threading.Thread(target=hash_in_chunks,