Skip to content

Commit 6b40a43

Browse files
fix(tests): test_read_only is not flaky anymore.
Fixed the `test_read_only` test based on what is done in the official Java client, we were missing some things and that led the test to fail from time to time (see comments in the code for full story). Also renamed `test__connection.py` to `test_connection.py`, where it belongs.
1 parent ffa0ae9 commit 6b40a43

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

kazoo/tests/test__connection.py renamed to kazoo/tests/test_connection.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
from kazoo.protocol.states import KazooState
1919
from kazoo.protocol.connection import _CONNECTION_DROP
2020
from kazoo.testing import KazooTestCase
21-
from kazoo.tests.util import wait
22-
from kazoo.tests.util import CI_ZK_VERSION
21+
from kazoo.tests.util import wait, CI_ZK_VERSION, CI
2322

2423

2524
class Delete(namedtuple("Delete", "path version")):
@@ -258,7 +257,7 @@ def back(state):
258257
class TestReadOnlyMode(KazooTestCase):
259258
def setUp(self):
260259
os.environ["ZOOKEEPER_LOCAL_SESSION_RO"] = "true"
261-
self.setup_zookeeper(read_only=True)
260+
self.setup_zookeeper()
262261
skip = False
263262
if CI_ZK_VERSION and CI_ZK_VERSION < (3, 4):
264263
skip = True
@@ -279,7 +278,15 @@ def test_read_only(self):
279278
from kazoo.exceptions import NotReadOnlyCallError
280279
from kazoo.protocol.states import KeeperState
281280

282-
client = self.client
281+
if CI:
282+
# force some wait to make sure the data produced during the
283+
# `setUp()` step are replicaed to all zk members
284+
# if not done the `get_children()` test may fail because the
285+
# node does not exist on the node that we will keep alive
286+
time.sleep(15)
287+
# do not keep the client started in the `setUp` step alive
288+
self.client.stop()
289+
client = self._get_client(connection_retry=None, read_only=True)
283290
states = []
284291
ev = threading.Event()
285292

@@ -289,6 +296,7 @@ def listen(state):
289296
if client.client_state == KeeperState.CONNECTED_RO:
290297
ev.set()
291298

299+
client.start()
292300
try:
293301
# stopping both nodes at the same time
294302
# else the test seems flaky when on CI hosts
@@ -303,6 +311,11 @@ def listen(state):
303311
thread.start()
304312
for thread in zk_stop_threads:
305313
thread.join()
314+
# stopping the client is *mandatory*, else the client might try to
315+
# reconnect using a xid that the server may endlessly refuse
316+
# restarting the client makes sure the xid gets reset
317+
client.stop()
318+
client.start()
306319
ev.wait(15)
307320
assert ev.is_set()
308321
assert client.client_state == KeeperState.CONNECTED_RO

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ module = [
114114
'kazoo.testing.common',
115115
'kazoo.testing.harness',
116116
'kazoo.tests.conftest',
117-
'kazoo.tests.test__connection',
118117
'kazoo.tests.test_barrier',
119118
'kazoo.tests.test_build',
120119
'kazoo.tests.test_cache',
121120
'kazoo.tests.test_client',
121+
'kazoo.tests.test_connection',
122122
'kazoo.tests.test_counter',
123123
'kazoo.tests.test_election',
124124
'kazoo.tests.test_eventlet_handler',

0 commit comments

Comments
 (0)