Skip to content

Commit eda8715

Browse files
authored
Fix stopping Watch for logs and events with timeout (#330)
1 parent 7222c98 commit eda8715

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

kubernetes_asyncio/watch/watch.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,20 @@ async def next(self):
174174

175175
line = line.decode('utf8')
176176

177+
# Special case for faster log streaming
178+
if self.return_type == 'str':
179+
if line == '':
180+
# end of log
181+
raise StopAsyncIteration
182+
return line
183+
177184
# Stop the iterator if K8s sends an empty response. This happens when
178185
# eg the supplied timeout has expired.
179186
if line == '':
180187
if watch_forever:
181188
self._reconnect()
182189
continue
183-
184-
# Special case for faster log streaming
185-
if self.return_type == 'str':
186-
return line
190+
raise StopAsyncIteration
187191

188192
# retry 410 error only once
189193
try:

kubernetes_asyncio/watch/watch_test.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async def test_watch_for_follow(self):
7474
fake_resp = AsyncMock()
7575
fake_resp.content.readline = AsyncMock()
7676
fake_resp.release = Mock()
77-
side_effects = ['log_line_1', 'log_line_2']
77+
side_effects = ['log_line_1', 'log_line_2', '']
7878
side_effects = [_.encode('utf8') for _ in side_effects]
7979
side_effects.extend([AssertionError('Should not have been called')])
8080
fake_resp.content.readline.side_effect = side_effects
@@ -84,16 +84,12 @@ async def test_watch_for_follow(self):
8484
fake_api.read_namespaced_pod_log.__doc__ = ':param follow:\n:type follow: bool\n:rtype: str'
8585

8686
watch = kubernetes_asyncio.watch.Watch()
87-
count = 1
87+
logs = []
8888
async with watch:
8989
async for e in watch.stream(fake_api.read_namespaced_pod_log):
90-
self.assertEqual("log_line_1", e)
91-
# Stop the watch. This must not return the next event which would
92-
# be an AssertionError exception.
93-
count += 1
94-
if count == len(side_effects) - 1:
95-
watch.stop()
90+
logs.append(e)
9691

92+
self.assertListEqual(logs, ['log_line_1', 'log_line_2'])
9793
fake_api.read_namespaced_pod_log.assert_called_once_with(
9894
_preload_content=False, follow=True)
9995
fake_resp.release.assert_called_once_with()

0 commit comments

Comments
 (0)