Skip to content

Commit 6540c93

Browse files
fix(test): avoid racy reader vs writer contender in test_rw_lock
`test_rw_lock` is creating 2 threads (`reader` and `writer`) and, after being started, it is expected that `reader` is a contender before `writer`. In some busy systems (like the CI... it's always the CI!) this may not be true and lead to the test failure because `writer` can be a contender before `reader`. This commit makes sure that `reader` is always a contender before `writer`.
1 parent 8269235 commit 6540c93

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

kazoo/tests/test_lock.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,27 +487,29 @@ def test_rw_lock(self):
487487
lock = self.client.WriteLock(self.lockpath, "test")
488488
lock.acquire()
489489

490+
wait = self.make_wait()
490491
reader_thread.start()
492+
# make sure reader_thread is a contender before writer_thread
493+
wait(lambda: len(lock.contenders()) == 2)
491494
writer_thread.start()
492495

493496
# wait for everyone to line up on the lock
494-
wait = self.make_wait()
495497
wait(lambda: len(lock.contenders()) == 3)
496498
contenders = lock.contenders()
497499

498500
assert contenders[0] == "test"
499501
remaining = contenders[1:]
500502

501-
# release the lock and contenders should claim it in order
502-
lock.release()
503-
504503
contender_bits = {
505504
"reader": (reader_thread, reader_event),
506505
"writer": (writer_thread, writer_event),
507506
}
508507

509-
for contender in ("reader", "writer"):
510-
thread, event = contender_bits[contender]
508+
# release the lock and contenders should claim it in order
509+
lock.release()
510+
511+
for contender, contender_bits in contender_bits.items():
512+
_, event = contender_bits
511513

512514
with self.condition:
513515
while not self.active_thread:

0 commit comments

Comments
 (0)