Skip to content

Commit

Permalink
fix(testing): cleanup ZK cluster between test cases
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
StephenSorriaux committed Apr 24, 2023
1 parent 6b40a43 commit d218dc9
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions kazoo/testing/harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"ZOOKEEPER_OBSERVER_START_ID": -1,
"ZOOKEEPER_LOCAL_SESSION_RO": "false",
}
MAX_INIT_TRIES = 5


def get_global_cluster():
Expand Down Expand Up @@ -208,8 +209,29 @@ def setup_zookeeper(self, **client_options):
self.cluster.start()
namespace = "/kazootests" + uuid.uuid4().hex
self.hosts = self.servers + namespace
if "timeout" not in client_options:
client_options["timeout"] = self.DEFAULT_CLIENT_TIMEOUT

tries = 0
while True:
try:
client_cluster_health = self._get_client()
client_cluster_health.start()
client_cluster_health.ensure_path("/")
client_cluster_health.stop()
self.log(logging.INFO, "cluster looks ready to go")
break
except Exception:
tries += 1
if tries >= MAX_INIT_TRIES:
raise
if tries > 0 and tries % 2 == 0:
self.log(
logging.WARNING,
"nuking current cluster and making another one",
)
self.cluster.terminate()
self.cluster.start()
continue

self.client = self._get_client(**client_options)
self.client.start()
self.client.ensure_path("/")
Expand Down Expand Up @@ -259,3 +281,9 @@ def setUp(self):

def tearDown(self):
self.teardown_zookeeper()

@classmethod
def tearDownClass(cls):
cluster = get_global_cluster()
if cluster is not None:
cluster.terminate()

0 comments on commit d218dc9

Please sign in to comment.