Skip to content
Browse files

Slightly better ZooLock test, and fixes

  • Loading branch information...
1 parent 3e5aff1 commit a28b01aee4d629574c681864a8ca6d83fdd63a31 @labisso labisso committed
Showing with 16 additions and 9 deletions.
  1. +8 −5 kazoo/recipe/lock.py
  2. +8 −4 kazoo/recipe/test/test_lock.py
View
13 kazoo/recipe/lock.py
@@ -39,6 +39,7 @@ def acquire(self):
except Exception:
# if we did ultimately fail, attempt to clean up
self._best_effort_cleanup()
+ raise
def _inner_acquire(self):
node = None
@@ -50,23 +51,25 @@ def _inner_acquire(self):
if not node:
node = self.client.create(self.create_path, "",
ephemeral=True, sequence=True)
+ # strip off path to node
+ node = node[len(self.path)+1:]
self.node = node
while True:
children = self._get_sorted_children()
- our_index = children.index(node)
-
- if our_index == -1:
- # somehow we aren't in the election -- probably we are
+ try:
+ our_index = children.index(node)
+ except ValueError:
+ # somehow we aren't in the children -- probably we are
# recovering from a session failure and our ephemeral
# node was removed
raise ForceRetryError()
#noinspection PySimplifyBooleanCheck
if our_index == 0:
- # we are leader
+ # we have the lock
return True
# otherwise we are in the mix. watch predecessor and bide our time
View
12 kazoo/recipe/test/test_lock.py
@@ -1,6 +1,7 @@
import unittest
import uuid
import threading
+import time
from kazoo.recipe.lock import ZooLock
from kazoo.test import get_client_or_skip
@@ -13,7 +14,7 @@ def setUp(self):
self.lockpath = "/" + uuid.uuid4().hex
self._c.create(self.lockpath, "")
- self.semaphore = threading.Semaphore()
+ self.active = 0
def tearDown(self):
if self.lockpath:
@@ -35,6 +36,7 @@ def test_lock(self):
clients.append(c)
locks.append(l)
+ # these will be greenlets in a monkey patched test env.
threads = [threading.Thread(target=self._thread_lock_acquire,
args=(lock,)) for lock in locks]
@@ -44,12 +46,14 @@ def test_lock(self):
for t in threads:
t.join()
- #TODO this test needs work
+ self.assertEqual(0, self.active)
def _thread_lock_acquire(self, lock):
with lock:
- self.semaphore.release()
+ self.active += 1
+ self.assertEqual(self.active, 1)
print "got lock"
- self.semaphore.acquire()
+ time.sleep(0)
+ self.active -= 1

0 comments on commit a28b01a

Please sign in to comment.
Something went wrong with that request. Please try again.