mirror of https://github.com/python/cpython
bpo-31234: Join threads in test_hashlib (#3573)
* 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. * test_hashlib: minor PEP 8 coding style fixes
This commit is contained in:
parent
18e95b4176
commit
8dcf22f442
|
@ -746,28 +746,28 @@ class HashLibTestCase(unittest.TestCase):
|
||||||
hasher = hashlib.sha1()
|
hasher = hashlib.sha1()
|
||||||
num_threads = 5
|
num_threads = 5
|
||||||
smallest_data = b'swineflu'
|
smallest_data = b'swineflu'
|
||||||
data = smallest_data*200000
|
data = smallest_data * 200000
|
||||||
expected_hash = hashlib.sha1(data*num_threads).hexdigest()
|
expected_hash = hashlib.sha1(data*num_threads).hexdigest()
|
||||||
|
|
||||||
def hash_in_chunks(chunk_size, event):
|
def hash_in_chunks(chunk_size):
|
||||||
index = 0
|
index = 0
|
||||||
while index < len(data):
|
while index < len(data):
|
||||||
hasher.update(data[index:index+chunk_size])
|
hasher.update(data[index:index + chunk_size])
|
||||||
index += chunk_size
|
index += chunk_size
|
||||||
event.set()
|
|
||||||
|
|
||||||
events = []
|
threads = []
|
||||||
for threadnum in range(num_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.assertGreater(chunk_size, 0)
|
||||||
self.assertEqual(chunk_size % len(smallest_data), 0)
|
self.assertEqual(chunk_size % len(smallest_data), 0)
|
||||||
event = threading.Event()
|
thread = threading.Thread(target=hash_in_chunks,
|
||||||
events.append(event)
|
args=(chunk_size,))
|
||||||
threading.Thread(target=hash_in_chunks,
|
threads.append(thread)
|
||||||
args=(chunk_size, event)).start()
|
|
||||||
|
|
||||||
for event in events:
|
for thread in threads:
|
||||||
event.wait()
|
thread.start()
|
||||||
|
for thread in threads:
|
||||||
|
thread.join()
|
||||||
|
|
||||||
self.assertEqual(expected_hash, hasher.hexdigest())
|
self.assertEqual(expected_hash, hasher.hexdigest())
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue