Skip to content

Commit

Permalink
Raise RuntimeError if rtypes.Watch instance is used after client.close()
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-fg committed Jan 14, 2022
1 parent ad133db commit be1ff05
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions aetcd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,16 @@ async def response_callback(response):
canceled = asyncio.Event()

async def cancel():
if not self._watcher:
raise RuntimeError('Calling watcher.cancel() on a closed client')
canceled.set()
await events.put(None)
await self._watcher.cancel(watcher_callback.watch_id)

@_handle_errors
async def iterator():
if not self._watcher:
raise RuntimeError('Using watcher as iterator on a closed client')
while not canceled.is_set():
event = await events.get()
if event is None:
Expand Down
11 changes: 8 additions & 3 deletions aetcd/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,15 @@ def __init__(self, kind, kv, prev_kv=None):


class Watch:
"""Reperesents the result of a watch operation.
"""Represents the result of a watch operation.
To get emitted events use as an asynchronous iterator, emitted events are
instances of an :class:`~aetcd.rtypes.Event`.
Use as an asynchronous iterator to get emitted events.
Events are instances of an :class:`~aetcd.rtypes.Event`.
Such iterator is exhausted either when undelying :class:`~aetcd.client.Client`
is closed or cancel method is called on this instance.
Instance must not be used after underlying :class:`~aetcd.client.Client`
is closed, and doing so will raise RuntimeError.
Usage example:
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/test_watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,16 @@ async def test_watch_key_ignores_global_timeout(client, etcdctl_put):
break

await w.cancel()


@pytest.mark.asyncio
async def test_watch_for_closed_client(etcd, etcdctl_put):
w = await etcd.watch(b'key')
etcdctl_put('key', '1')
await etcd.close()
etcdctl_put('key', '2')
with pytest.raises(RuntimeError):
async for kv in w:
raise AssertionError('non-empty watcher iterator with closed client')
with pytest.raises(RuntimeError):
await w.cancel()

0 comments on commit be1ff05

Please sign in to comment.