From 02c01b453c3ead381f17d48cf3030269bbed442b Mon Sep 17 00:00:00 2001 From: Stephen Sorriaux Date: Sun, 3 Mar 2024 12:10:01 -0500 Subject: [PATCH] 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`. --- kazoo/tests/test_lock.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/kazoo/tests/test_lock.py b/kazoo/tests/test_lock.py index 397e971f..7e5ecb77 100644 --- a/kazoo/tests/test_lock.py +++ b/kazoo/tests/test_lock.py @@ -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: