Skip to content

Commit 53bc8c1

Browse files
fix(tests): making test_read_only test less flaky
1 parent 33c348b commit 53bc8c1

File tree

7 files changed

+42
-14
lines changed

7 files changed

+42
-14
lines changed

kazoo/protocol/connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,9 @@ def zk_loop(self):
544544
if retry(self._connect_loop, retry) is STOP_CONNECTING:
545545
break
546546
except RetryFailedError:
547-
self.logger.warning(
547+
self.logger.exception(
548548
"Failed connecting to Zookeeper "
549-
"within the connection retry policy."
549+
"within the connection retry policy:"
550550
)
551551
finally:
552552
self.connection_stopped.set()

kazoo/testing/common.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,9 @@ def run(self):
221221
)
222222
self.process = subprocess.Popen(args=args)
223223
log.info(
224-
"Started zookeeper process %s using args %s",
224+
"Started zookeeper process %s on port %s using args %s",
225225
self.process.pid,
226+
self.server_info.client_port,
226227
args,
227228
)
228229
self._running = True
@@ -304,12 +305,12 @@ def destroy(self):
304305

305306
shutil.rmtree(self.working_path, True)
306307

307-
def get_logs(self):
308+
def get_logs(self, num_lines=100):
308309
log_path = pathlib.Path(self.working_path, "zookeeper.log")
309310
if log_path.exists():
310311
log_file = log_path.open("r")
311312
lines = log_file.readlines()
312-
return lines[-100:]
313+
return lines[-num_lines:]
313314
return []
314315

315316

kazoo/tests/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55

66
def pytest_exception_interact(node, call, report):
7-
if hasattr(node._testcase, "cluster"):
7+
try:
88
cluster = node._testcase.cluster
99
log.error("Zookeeper cluster logs:")
1010
for logs in cluster.get_logs():
1111
log.error(logs)
12+
except Exception:
13+
log.exception("Cant get ZK logs:")

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from collections import namedtuple, deque
2+
import logging
3+
24
import os
35
import threading
46
import time
@@ -18,8 +20,10 @@
1820
from kazoo.protocol.states import KazooState
1921
from kazoo.protocol.connection import _CONNECTION_DROP
2022
from kazoo.testing import KazooTestCase
21-
from kazoo.tests.util import wait
22-
from kazoo.tests.util import CI_ZK_VERSION
23+
from kazoo.tests.util import wait, CI_ZK_VERSION, CI
24+
25+
26+
log = logging.getLogger(__name__)
2327

2428

2529
class Delete(namedtuple("Delete", "path version")):
@@ -258,7 +262,7 @@ def back(state):
258262
class TestReadOnlyMode(KazooTestCase):
259263
def setUp(self):
260264
os.environ["ZOOKEEPER_LOCAL_SESSION_RO"] = "true"
261-
self.setup_zookeeper(read_only=True)
265+
self.setup_zookeeper()
262266
skip = False
263267
if CI_ZK_VERSION and CI_ZK_VERSION < (3, 4):
264268
skip = True
@@ -279,7 +283,15 @@ def test_read_only(self):
279283
from kazoo.exceptions import NotReadOnlyCallError
280284
from kazoo.protocol.states import KeeperState
281285

282-
client = self.client
286+
if CI:
287+
# force some wait to make sure the data produced during the
288+
# `setUp()` step are replicaed to all zk members
289+
# if not done the `get_children()` test may fail because the
290+
# node does not exist on the node that we will keep alive
291+
time.sleep(15)
292+
# do not keep the client started in the `setUp` step alive
293+
self.client.stop()
294+
client = self._get_client(read_only=True)
283295
states = []
284296
ev = threading.Event()
285297

@@ -289,6 +301,7 @@ def listen(state):
289301
if client.client_state == KeeperState.CONNECTED_RO:
290302
ev.set()
291303

304+
client.start()
292305
try:
293306
# stopping both nodes at the same time
294307
# else the test seems flaky when on CI hosts
@@ -303,6 +316,11 @@ def listen(state):
303316
thread.start()
304317
for thread in zk_stop_threads:
305318
thread.join()
319+
# stopping the client is *mandatory*, else the client might try to
320+
# reconnect using a xid that the server may endlessly refuse
321+
# stopping the client makes sure the xid is resetted
322+
client.stop()
323+
client.start()
306324
ev.wait(15)
307325
assert ev.is_set()
308326
assert client.client_state == KeeperState.CONNECTED_RO

kazoo/tests/test_lock.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,8 @@ def test_rw_lock(self):
511511

512512
with self.condition:
513513
while not self.active_thread:
514-
self.condition.wait()
514+
success = self.condition.wait(timeout=10)
515+
assert success
515516
assert self.active_thread == contender
516517

517518
assert lock.contenders() == remaining
@@ -521,7 +522,8 @@ def test_rw_lock(self):
521522

522523
with self.condition:
523524
while self.active_thread:
524-
self.condition.wait()
525+
success = self.condition.wait(timeout=10)
526+
assert success
525527

526528
reader_thread.join()
527529
writer_thread.join()

pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ extend-exclude = '''
2121

2222
[tool.pytest.ini_options]
2323
addopts = "-ra -v"
24+
# XXX: uncomment if in need off all the captured logs with timing and stuff
25+
# log_cli = true
26+
# log_cli_date_format = "%Y-%m-%d %H:%M:%S"
27+
# log_cli_format = "%(asctime)s %(levelname)s %(message)s"
28+
# log_cli_level = "INFO"
2429

2530
[tool.mypy]
2631

@@ -114,11 +119,11 @@ module = [
114119
'kazoo.testing.common',
115120
'kazoo.testing.harness',
116121
'kazoo.tests.conftest',
117-
'kazoo.tests.test__connection',
118122
'kazoo.tests.test_barrier',
119123
'kazoo.tests.test_build',
120124
'kazoo.tests.test_cache',
121125
'kazoo.tests.test_client',
126+
'kazoo.tests.test_connection',
122127
'kazoo.tests.test_counter',
123128
'kazoo.tests.test_election',
124129
'kazoo.tests.test_eventlet_handler',

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ allowlist_externals =
3636
commands =
3737
sasl: {toxinidir}/init_krb5.sh {envtmpdir}/kerberos \
3838
{toxinidir}/ensure-zookeeper-env.sh \
39-
pytest {posargs: -ra -v --cov-report=xml --cov=kazoo kazoo/tests}
39+
pytest {posargs: -ra -v --cov-report=xml --cov=kazoo kazoo/tests/test_connection.py}
4040

4141
[testenv:build]
4242

0 commit comments

Comments
 (0)