Browse files

Update async lock to also use lock root. Add some basic async lock te…

…sts.
  • Loading branch information...
1 parent a60bf5b commit 31b6538d8431cfbef3dc949bb4dcb1b5e0be0d52 @bbangert bbangert committed May 9, 2012
Showing with 31 additions and 5 deletions.
  1. +5 −5 zktools/locking.py
  2. +26 −0 zktools/tests/test_locking.py
View
10 zktools/locking.py
@@ -98,17 +98,19 @@ class ZkAsyncLock(object):
do_more_stuff()
"""
- def __init__(self, connection, lock_path):
+ def __init__(self, connection, lock_name, lock_root='/ZktoolsLocks'):
"""Create an Asynchronous Zookeeper Lock
:param connection: Zookeeper connection object
:type connection: zc.zk Zookeeper instance
- :param lock_path: Path to the lock node that should be used
+ :param lock_name: Path to the lock node that should be used
+ :param lock_root: Path to the root lock node to create the locks
+ under
:type lock_root: string
"""
self._zk = connection
- self._lock_path = lock_path
+ self._lock_path = '/'.join([lock_root, lock_name])
self._lock_event = threading.Event()
self._acquired = False
self._candidate_path = None
@@ -287,8 +289,6 @@ def __init__(self, connection, lock_name, lock_root='/ZktoolsLocks'):
:param lock_root: Path to the root lock node to create the locks
under
:type lock_root: string
- :param logfile: Path to a file to log the Zookeeper stream to
- :type logfile: string
"""
self._zk = connection
View
26 zktools/tests/test_locking.py
@@ -1,10 +1,36 @@
import threading
from nose.tools import eq_
+import zookeeper
from zktools.tests import TestBase
+class TestAsyncLocking(TestBase):
+ def makeOne(self, *args, **kwargs):
+ from zktools.locking import ZkAsyncLock
+ return ZkAsyncLock(self.conn, *args, **kwargs)
+
+ def test_retryable(self):
+ from zktools.locking import retryable
+ eq_(True, retryable(zookeeper.CONNECTIONLOSS))
+
+ def testBasicLock(self):
+ lock = self.makeOne('zkALockTest')
+ lock.acquire()
+ lock.wait_for_acquire()
+ eq_(True, lock.acquired)
+ lock.release()
+ lock.wait_for_release()
+ eq_(False, lock.acquired)
+
+ def test_with_blocking(self):
+ lock = self.makeOne('zkALockTest')
+ with lock:
+ eq_(True, lock.acquired)
+ eq_(False, lock.acquired)
+
+
class TestLocking(TestBase):
def makeOne(self, *args, **kwargs):
from zktools.locking import ZkLock

0 comments on commit 31b6538

Please sign in to comment.