python-zk / kazoo

Kazoo is a high-level Python library that makes it easier to use Apache Zookeeper.
https://kazoo.readthedocs.io
Apache License 2.0
1.3k stars 386 forks source link

fix(test): avoid racy reader vs writer contender in `test_rw_lock` #747

Closed StephenSorriaux closed 6 months ago

StephenSorriaux commented 6 months ago

See test failure https://github.com/python-zk/kazoo/actions/runs/8116444130/job/22206401448?pr=744

        for contender in ("reader", "writer"):
            thread, event = contender_bits[contender]

            with self.condition:
                while not self.active_thread:
                    self.condition.wait()
>               assert self.active_thread == contender
E               AssertionError: assert 'writer' == 'reader'
E                 - reader
E                 + writer

kazoo/tests/test_lock.py:515: AssertionError

Why is this needed?

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.

Proposed Changes

Does this PR introduce any breaking change?

No!