Skip to content

Commit 84a34a4

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

File tree

8 files changed

+50
-15
lines changed

8 files changed

+50
-15
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/testing/harness.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,15 @@ def setup_zookeeper(self, **client_options):
210210
self.hosts = self.servers + namespace
211211
if "timeout" not in client_options:
212212
client_options["timeout"] = self.DEFAULT_CLIENT_TIMEOUT
213+
retry_no_interrupt = client_options.pop("retry_no_interrupt", False)
214+
if retry_no_interrupt:
215+
from kazoo.retry import KazooRetry
216+
217+
conn_retry = KazooRetry(max_tries=-1, delay=0.2, max_delay=180)
213218
self.client = self._get_client(**client_options)
219+
if retry_no_interrupt:
220+
conn_retry.interrupt = None
221+
self.client._connection.retry_sleeper = conn_retry.copy()
214222
self.client.start()
215223
self.client.ensure_path("/")
216224

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: 23 additions & 5 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(retry_no_interrupt=True)
262266
skip = False
263267
if CI_ZK_VERSION and CI_ZK_VERSION < (3, 4):
264268
skip = True
@@ -278,8 +282,15 @@ def tearDown(self):
278282
def test_read_only(self):
279283
from kazoo.exceptions import NotReadOnlyCallError
280284
from kazoo.protocol.states import KeeperState
285+
from kazoo.retry import KazooRetry
281286

282-
client = self.client
287+
time.sleep(15)
288+
self.client.stop()
289+
conn_retry = KazooRetry(max_tries=-1, delay=0.2, max_delay=180)
290+
client = self._get_client(connection_retry=conn_retry, read_only=True)
291+
# XXX: avoid the use of `interrupt`
292+
conn_retry.interrupt = None
293+
client._connection.retry_sleeper = conn_retry.copy()
283294
states = []
284295
ev = threading.Event()
285296

@@ -289,6 +300,7 @@ def listen(state):
289300
if client.client_state == KeeperState.CONNECTED_RO:
290301
ev.set()
291302

303+
client.start()
292304
try:
293305
# stopping both nodes at the same time
294306
# else the test seems flaky when on CI hosts
@@ -303,7 +315,13 @@ def listen(state):
303315
thread.start()
304316
for thread in zk_stop_threads:
305317
thread.join()
306-
ev.wait(15)
318+
client.stop()
319+
client.start()
320+
# the latest ZK node can take time to recover on the CI
321+
time_to_wait = 30 if CI else 10
322+
ev.wait(time_to_wait)
323+
for line in self.cluster[0].get_logs(num_lines=250):
324+
log.warning(line)
307325
assert ev.is_set()
308326
assert client.client_state == KeeperState.CONNECTED_RO
309327

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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ extend-exclude = '''
2121

2222
[tool.pytest.ini_options]
2323
addopts = "-ra -v"
24+
log_cli = true
25+
log_cli_date_format = "%Y-%m-%d %H:%M:%S"
26+
log_cli_format = "%(asctime)s %(levelname)s %(message)s"
27+
log_cli_level = "INFO"
2428

2529
[tool.mypy]
2630

@@ -114,11 +118,11 @@ module = [
114118
'kazoo.testing.common',
115119
'kazoo.testing.harness',
116120
'kazoo.tests.conftest',
117-
'kazoo.tests.test__connection',
118121
'kazoo.tests.test_barrier',
119122
'kazoo.tests.test_build',
120123
'kazoo.tests.test_cache',
121124
'kazoo.tests.test_client',
125+
'kazoo.tests.test_connection',
122126
'kazoo.tests.test_counter',
123127
'kazoo.tests.test_election',
124128
'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)