Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close the ChildrenWatch if the node doesn't exist. #393

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice if there was a non-timing dependent way to wait for this. But I understand there may not be. Maybe if the test tries to grab the watchers _run_lock which should be released as soon as _stopped is set and returned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a good idea. I will have a try.

eq_(update.is_set(), False)
eq_(children_watch._stopped, True)

def test_bad_watch_func2(self):
counter = 0

Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ commands = flake8 {posargs}
[testenv]
usedevelop = True
install_command = pip install {opts} {packages}
setenv = VIRTUAL_ENV={envdir}
setenv =
VIRTUAL_ENV={envdir}
ZOOKEEPER_VERSION={env:ZOOKEEPER_VERSION:}
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/requirements_sphinx.txt
commands = {toxinidir}/ensure-zookeeper-env.sh nosetests {posargs: -d -v --with-coverage kazoo.tests}
Expand Down