Browse files

More test cleanups; stop littering ZK cluster.

  • Loading branch information...
1 parent a61e949 commit ea87eed9e12306bbf9c1dd6a9fc01fdf182c1d83 @labisso labisso committed May 7, 2012
Showing with 37 additions and 47 deletions.
  1. +14 −2 kazoo/test/__init__.py
  2. +3 −14 kazoo/test/test_client.py
  3. +20 −31 kazoo/test/test_zkclient.py
View
16 kazoo/test/__init__.py
@@ -1,9 +1,9 @@
import os
import unittest
import time
+import uuid
-from kazoo.zkclient import ZooKeeperClient
-from kazoo.client import KazooClient
+from kazoo.client import KazooClient, KazooState
# if this env variable is set, ZK client integration tests are run
# against the specified host list
@@ -35,3 +35,15 @@ def until_timeout(timeout, value=None):
+class KazooTestCase(unittest.TestCase):
+ def setUp(self):
+ self.hosts = get_hosts_or_skip()
+
+ self.namespace = "/kazootests" + uuid.uuid4().hex
+ self.client = KazooClient(self.hosts, namespace=self.namespace)
+
+ def tearDown(self):
+ if self.client.state == KazooState.LOST:
+ self.client.connect()
+ self.client.recursive_delete(self.namespace)
+ self.client.close()
View
17 kazoo/test/test_client.py
@@ -1,22 +1,11 @@
-import unittest
-import uuid
import threading
-from kazoo.client import KazooClient, KazooState
+from kazoo.client import KazooState
from kazoo.zkclient import EventType
-from kazoo.test import get_hosts_or_skip
+from kazoo.test import KazooTestCase
from kazoo.exceptions import NoNodeException
-class ZooKeeperClientTests(unittest.TestCase):
- def setUp(self):
- self.hosts = get_hosts_or_skip()
-
- self.namespace = "/kazootests" + uuid.uuid4().hex
- self.client = KazooClient(self.hosts, namespace=self.namespace)
-
- def tearDown(self):
- self.client.recursive_delete(self.namespace)
- self.client.close()
+class ZooKeeperClientTests(KazooTestCase):
def test_namespace(self):
namespace = self.namespace
View
51 kazoo/test/test_zkclient.py
@@ -1,35 +1,20 @@
-import unittest
import uuid
import threading
from kazoo.zkclient import ZooKeeperClient
-from kazoo.test import get_hosts_or_skip
+from kazoo.test import KazooTestCase
-class ZooKeeperClientTests(unittest.TestCase):
- def setUp(self):
- self.hosts = get_hosts_or_skip()
+class ZooKeeperClientTests(KazooTestCase):
- self.zk = None
- self.created = []
-
- def tearDown(self):
- if self.zk:
- #TODO remove any created nodes
- self.zk.close()
-
- def test_connect_close(self):
- self.zk = ZooKeeperClient(self.hosts)
-
- self.zk.connect()
- self.assertTrue(self.zk.connected)
- self.zk.close()
- self.assertFalse(self.zk.connected)
+ @property
+ def zk(self):
+ return self.client.zk
def test_create_get_set(self):
- self.zk = ZooKeeperClient(self.hosts)
- self.zk.connect()
+ self.client.connect()
+ self.client.ensure_path("/")
- nodepath = "/" + uuid.uuid4().hex
+ nodepath = self.namespace + "/" + uuid.uuid4().hex
self.zk.create(nodepath, "sandwich", ephemeral=True)
@@ -41,8 +26,8 @@ def test_create_get_set(self):
self.assertGreater(newstat['version'], stat['version'])
def test_create_get_sequential(self):
- self.zk = ZooKeeperClient(self.hosts)
- self.zk.connect()
+ self.client.connect()
+ self.client.ensure_path("/")
basepath = "/" + uuid.uuid4().hex
realpath = self.zk.create(basepath, "sandwich", sequence=True,
@@ -54,8 +39,8 @@ def test_create_get_sequential(self):
self.assertEqual(data, "sandwich")
def test_exists(self):
- self.zk = ZooKeeperClient(self.hosts)
- self.zk.connect()
+ self.client.connect()
+ self.client.ensure_path("/")
nodepath = "/" + uuid.uuid4().hex
@@ -67,9 +52,13 @@ def test_exists(self):
self.assertTrue(exists)
self.assertIn("version", exists)
+ multi_node_nonexistent = "/" + uuid.uuid4().hex + "/hats"
+ exists = self.zk.exists(multi_node_nonexistent)
+ self.assertIsNone(exists)
+
def test_exists_watch(self):
- self.zk = ZooKeeperClient(self.hosts)
- self.zk.connect()
+ self.client.connect()
+ self.client.ensure_path("/")
nodepath = "/" + uuid.uuid4().hex
@@ -88,8 +77,8 @@ def w(watch_event):
self.assertTrue(event.is_set())
def test_create_delete(self):
- self.zk = ZooKeeperClient(self.hosts)
- self.zk.connect()
+ self.client.connect()
+ self.client.ensure_path("/")
nodepath = "/" + uuid.uuid4().hex

0 comments on commit ea87eed

Please sign in to comment.