Skip to content

Commit d218dc9

Browse files
fix(testing): cleanup ZK cluster between test cases
Now performing a ZK cluster nuking at the end of each test case since keeping the same ZK state from test cases to test cases actually led to some tests randomly failing. Added `client_cluster_health` ZK client in the `setup_zookeeper()` step that is there to "guarantee" the cluster is up and running, without even considering the configuration that will be used by the test itself.
1 parent 6b40a43 commit d218dc9

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

kazoo/testing/harness.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"ZOOKEEPER_OBSERVER_START_ID": -1,
2222
"ZOOKEEPER_LOCAL_SESSION_RO": "false",
2323
}
24+
MAX_INIT_TRIES = 5
2425

2526

2627
def get_global_cluster():
@@ -208,8 +209,29 @@ def setup_zookeeper(self, **client_options):
208209
self.cluster.start()
209210
namespace = "/kazootests" + uuid.uuid4().hex
210211
self.hosts = self.servers + namespace
211-
if "timeout" not in client_options:
212-
client_options["timeout"] = self.DEFAULT_CLIENT_TIMEOUT
212+
213+
tries = 0
214+
while True:
215+
try:
216+
client_cluster_health = self._get_client()
217+
client_cluster_health.start()
218+
client_cluster_health.ensure_path("/")
219+
client_cluster_health.stop()
220+
self.log(logging.INFO, "cluster looks ready to go")
221+
break
222+
except Exception:
223+
tries += 1
224+
if tries >= MAX_INIT_TRIES:
225+
raise
226+
if tries > 0 and tries % 2 == 0:
227+
self.log(
228+
logging.WARNING,
229+
"nuking current cluster and making another one",
230+
)
231+
self.cluster.terminate()
232+
self.cluster.start()
233+
continue
234+
213235
self.client = self._get_client(**client_options)
214236
self.client.start()
215237
self.client.ensure_path("/")
@@ -259,3 +281,9 @@ def setUp(self):
259281

260282
def tearDown(self):
261283
self.teardown_zookeeper()
284+
285+
@classmethod
286+
def tearDownClass(cls):
287+
cluster = get_global_cluster()
288+
if cluster is not None:
289+
cluster.terminate()

0 commit comments

Comments
 (0)