Skip to content

Commit

Permalink
fix(test): avoid racy reader vs writer contender in test_rw_lock
Browse files Browse the repository at this point in the history
`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`.
  • Loading branch information
StephenSorriaux committed Mar 8, 2024
1 parent 8269235 commit 6540c93
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions kazoo/tests/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,27 +487,29 @@ def test_rw_lock(self):
lock = self.client.WriteLock(self.lockpath, "test")
lock.acquire()

wait = self.make_wait()
reader_thread.start()
# make sure reader_thread is a contender before writer_thread
wait(lambda: len(lock.contenders()) == 2)
writer_thread.start()

# wait for everyone to line up on the lock
wait = self.make_wait()
wait(lambda: len(lock.contenders()) == 3)
contenders = lock.contenders()

assert contenders[0] == "test"
remaining = contenders[1:]

# release the lock and contenders should claim it in order
lock.release()

contender_bits = {
"reader": (reader_thread, reader_event),
"writer": (writer_thread, writer_event),
}

for contender in ("reader", "writer"):
thread, event = contender_bits[contender]
# release the lock and contenders should claim it in order
lock.release()

for contender, contender_bits in contender_bits.items():
_, event = contender_bits

with self.condition:
while not self.active_thread:
Expand Down

0 comments on commit 6540c93

Please sign in to comment.