Skip to content

Commit b41d958

Browse files
committed
Close the ChildrenWatch if the node doesn't exist.
This implementation resolves the "NoNodeError" while watching children. Fix #149.
1 parent 1b4bca7 commit b41d958

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

kazoo/recipe/watchers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,13 @@ def _get_children(self, event=None):
315315
if self._stopped:
316316
return
317317

318-
children = self._client.retry(self._client.get_children,
319-
self._path, self._watcher)
318+
try:
319+
children = self._client.retry(self._client.get_children,
320+
self._path, self._watcher)
321+
except NoNodeError:
322+
self._stopped = True
323+
return
324+
320325
if not self._watch_established:
321326
self._watch_established = True
322327

kazoo/tests/test_watchers.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,46 @@ def changed(d, stat):
171171

172172
eq_(args, [None, None])
173173

174+
def test_no_such_node_for_children_watch(self):
175+
args = []
176+
path = self.path + '/test_no_such_node_for_children_watch'
177+
update = threading.Event()
178+
179+
def changed(children):
180+
args.append(children)
181+
update.set()
182+
183+
# watch a node which does not exist
184+
children_watch = self.client.ChildrenWatch(path, changed)
185+
eq_(update.is_set(), False)
186+
eq_(children_watch._stopped, True)
187+
eq_(args, [])
188+
189+
# watch a node which exists
190+
self.client.create(path, b'')
191+
children_watch = self.client.ChildrenWatch(path, changed)
192+
update.wait(3)
193+
eq_(args, [[]])
194+
update.clear()
195+
196+
# watch changes
197+
self.client.create(path + '/fred', b'')
198+
update.wait(3)
199+
eq_(args, [[], ['fred']])
200+
update.clear()
201+
202+
# delete children
203+
self.client.delete(path + '/fred')
204+
update.wait(3)
205+
eq_(args, [[], ['fred'], []])
206+
update.clear()
207+
208+
# delete watching
209+
self.client.delete(path)
210+
time.sleep(1)
211+
eq_(update.is_set(), False)
212+
eq_(children_watch._stopped, True)
213+
174214
def test_bad_watch_func2(self):
175215
counter = 0
176216

0 commit comments

Comments
 (0)