Skip to content

Commit 1a26622

Browse files
committed
refactor test_multithreading
1 parent b557966 commit 1a26622

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

tests/test_bcrypt.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,8 @@ def test_2a_wraparound_bug():
504504
# this test spawns threads and is slow, so don't run it in many threads
505505
@pytest.mark.parallel_threads(1)
506506
def test_multithreading():
507-
def get_id():
508-
return uuid.uuid4().bytes
509-
510507
class User:
511-
def __init__(self, id_, pw):
512-
self.id_ = id_
508+
def __init__(self, pw):
513509
self.salt = bcrypt.gensalt(4)
514510
self.hash_ = bcrypt.hashpw(pw, self.salt)
515511
self.key = bcrypt.kdf(pw, self.salt, 32, 50)
@@ -520,21 +516,14 @@ def check(self, pw):
520516

521517
# use UUIDs as both ID and passwords
522518
num_users = 50
523-
ids = [get_id() for _ in range(num_users)]
524-
pws = {id_: get_id() for id_, _ in zip(ids, range(num_users))}
525-
526519
user_creator = ThreadPoolExecutor(max_workers=4)
520+
pws = [uuid.uuid4().bytes for _ in range(num_users)]
527521

528-
def create_user(id_, pw):
529-
return id_, User(id_, pw)
530-
531-
creator_futures = [
532-
user_creator.submit(create_user, id_, pw) for id_, pw in pws.items()
533-
]
522+
futures = [user_creator.submit(User, pw) for pw in pws]
534523

535-
users = [future.result() for future in creator_futures]
524+
users = [future.result() for future in futures]
536525

537-
for id_, user in users:
538-
assert bcrypt.hashpw(pws[id_], user.salt) == user.hash_
539-
assert user.check(pws[id_])
540-
assert bcrypt.kdf(pws[id_], user.salt, 32, 50) == user.key
526+
for pw, user in zip(pws, users):
527+
assert bcrypt.hashpw(pw, user.salt) == user.hash_
528+
assert user.check(pw)
529+
assert bcrypt.kdf(pw, user.salt, 32, 50) == user.key

0 commit comments

Comments
 (0)