Skip to content

Commit aef9edc

Browse files
authored
Merge pull request #89 from tomplus/feat/stream_res_version
fix: watch.stream stores resource_version for the next call
2 parents 33a12bd + 4ae6b9d commit aef9edc

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

kubernetes_asyncio/watch/watch.py

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ def stream(self, func, *args, **kwargs):
181181
self.return_type = self.get_return_type(func)
182182
kwargs['watch'] = True
183183
kwargs['_preload_content'] = False
184+
if 'resource_version' in kwargs:
185+
self.resource_version = kwargs['resource_version']
184186

185187
self.func = partial(func, *args, **kwargs)
186188

kubernetes_asyncio/watch/watch_test.py

+29
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ async def test_watch_with_decode(self):
6767
_preload_content=False, watch=True, resource_version='123')
6868
fake_resp.release.assert_called_once_with()
6969

70+
# last resource_version has to be stored in the object
71+
self.assertEqual(watch.resource_version, '2')
72+
7073
async def test_watch_k8s_empty_response(self):
7174
"""Stop the iterator when the response is empty.
7275
@@ -190,6 +193,32 @@ async def test_watch_timeout(self):
190193
call(_preload_content=False, watch=True, resource_version='1555')])
191194
fake_resp.release.assert_called_once_with()
192195

196+
async def test_watch_timeout_with_resource_version(self):
197+
fake_resp = CoroutineMock()
198+
fake_resp.content.readline = CoroutineMock()
199+
fake_resp.release = Mock()
200+
201+
fake_resp.content.readline.side_effect = [asyncio.TimeoutError(),
202+
b""]
203+
204+
fake_api = Mock()
205+
fake_api.get_namespaces = CoroutineMock(return_value=fake_resp)
206+
fake_api.get_namespaces.__doc__ = ':return: V1NamespaceList'
207+
208+
watch = kubernetes_asyncio.watch.Watch()
209+
async with watch.stream(fake_api.get_namespaces, resource_version='10') as stream:
210+
async for e in stream: # noqa
211+
pass
212+
213+
# all calls use the passed resource version
214+
fake_api.get_namespaces.assert_has_calls(
215+
[call(_preload_content=False, watch=True, resource_version='10'),
216+
call(_preload_content=False, watch=True, resource_version='10')])
217+
218+
fake_resp.release.assert_called_once_with()
219+
self.assertEqual(watch.resource_version, '10')
220+
221+
193222
if __name__ == '__main__':
194223
import asynctest
195224
asynctest.main()

0 commit comments

Comments
 (0)