Skip to content

Commit

Permalink
Close the ChildrenWatch if the node doesn't exist.
Browse files Browse the repository at this point in the history
This implementation resolves the "NoNodeError" while watching children.

Fix python-zk#149.
  • Loading branch information
tonyseek committed Sep 27, 2016
1 parent 08157d9 commit e66dccb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
9 changes: 7 additions & 2 deletions kazoo/recipe/watchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,13 @@ def _get_children(self, event=None):
if self._stopped:
return

children = self._client.retry(self._client.get_children,
self._path, self._watcher)
try:
children = self._client.retry(self._client.get_children,
self._path, self._watcher)
except NoNodeError:
self._stopped = True
return

if not self._watch_established:
self._watch_established = True

Expand Down
40 changes: 40 additions & 0 deletions kazoo/tests/test_watchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,46 @@ def changed(d, stat):

eq_(args, [None, None])

def test_no_such_node_for_children_watch(self):
args = []
path = self.path + '/test_no_such_node_for_children_watch'
update = threading.Event()

def changed(children):
args.append(children)
update.set()

# watch a node which does not exist
children_watch = self.client.ChildrenWatch(path, changed)
eq_(update.is_set(), False)
eq_(children_watch._stopped, True)
eq_(args, [])

# watch a node which exists
self.client.create(path, b'')
children_watch = self.client.ChildrenWatch(path, changed)
update.wait(3)
eq_(args, [[]])
update.clear()

# watch changes
self.client.create(path + '/fred', b'')
update.wait(3)
eq_(args, [[], ['fred']])
update.clear()

# delete children
self.client.delete(path + '/fred')
update.wait(3)
eq_(args, [[], ['fred'], []])
update.clear()

# delete watching
self.client.delete(path)
time.sleep(1)
eq_(update.is_set(), False)
eq_(children_watch._stopped, True)

def test_bad_watch_func2(self):
counter = 0

Expand Down

0 comments on commit e66dccb

Please sign in to comment.