Permalink
Browse files

Add a Queue factory method to the client.

  • Loading branch information...
1 parent 0e4f37b commit f32b5354c3e0d48ba4a1d037a027e1834ba5cf78 @davidmiller committed May 3, 2012
Showing with 43 additions and 3 deletions.
  1. +12 −1 test/test_client.py
  2. +28 −2 zoop/client.py
  3. +3 −0 zoop/exceptions.py
View
@@ -10,7 +10,7 @@
import zookeeper
import zoop
-from zoop import client, exceptions, logutils
+from zoop import client, exceptions, logutils, queue
logutils.set_loglevel('ERROR')
@@ -129,6 +129,17 @@ def test_watch(self):
self.zk.watch('/foo/bar', zoop.Event.Child, cb)
Pspy.assert_called_once_with('/foo/bar', zoop.Event.Child, cb)
+ def test_queue_no_connection(self):
+ """ Should raise an error """
+ with self.assertRaises(exceptions.NotConnectedError):
+ self.zk.Queue('/myq')
+
+ def test_queue(self):
+ self.zk.connected = True
+ with patch.object(queue, 'Queue') as Pq:
+ q = self.zk.Queue('/myq')
+ Pq.assert_called_once_with(self.zk, '/myq')
+
View
@@ -34,6 +34,9 @@ class ZooKeeper(object):
Arguments:
- `connection`: string of host:port
+
+ Return: None
+ Exceptions: None
"""
def __init__(self, connection):
@@ -180,10 +183,11 @@ def set(self, path, value):
- `path`: string
- `value`: string
- Return: Tuple of (Value, Statsdict)
+ Return: None
Exceptions: NoNodeError
"""
- return zookeeper.set(self._zk, path, value)
+ zookeeper.set(self._zk, path, value)
+ return
def watch(self, path, callback, event):
"""
@@ -202,6 +206,28 @@ def watch(self, path, callback, event):
self.watcher.spyon(path, callback, event)
return
+ def Queue(self, path):
+ """
+ Returns an instantiated Queue with the root `path`
+ connected to this ZooKeeper instance.
+
+ Arguments:
+ - `path`: string - the root of your Queue. should be an
+ absolute path.
+
+ Return: Queue
+ Exceptions: NotConnectedError - the ZooKeeper instance was not connected
+ """
+ if not self.connected:
+ err = "You aren't connected to a ZooKeeper instance - no way to create a Queue"
+ raise exceptions.NotConnectedError(err)
+ # Avoid circular imports from the top-level package namespace
+ from zoop import queue
+ return queue.Queue(self, path)
+
+
+
+
class AsyncZooKeeper(object):
"""
A ZooKeeper client that uses the Asynchronous
View
@@ -10,6 +10,9 @@ class NoEventError(Error):
class LostConnectionError(Error):
"Lost connection to the ZooKeeper instance"
+class NotConnectedError(Error):
+ "Not Connected to a ZooKeeper instance."
+
class NodeExistsError(Error):
"The Node Exists!"

0 comments on commit f32b535

Please sign in to comment.