Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

Fix bug with Watch and 410 retries #227

Merged
merged 1 commit into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 6 additions & 4 deletions watch/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ def stream(self, func, *args, **kwargs):
if 'resource_version' in kwargs:
self.resource_version = kwargs['resource_version']

timeouts = ('timeout_seconds' in kwargs)
# Do not attempt retries if user specifies a timeout.
# We want to ensure we are returning within that timeout.
disable_retries = ('timeout_seconds' in kwargs)
retry_after_410 = False
while True:
resp = func(*args, **kwargs)
Expand All @@ -164,9 +166,9 @@ def stream(self, func, *args, **kwargs):
if isinstance(event, dict) \
and event['type'] == 'ERROR':
obj = event['raw_object']
# Current request expired, let's retry,
# Current request expired, let's retry, (if enabled)
# but only if we have not already retried.
if not retry_after_410 and \
Copy link
Contributor

Choose a reason for hiding this comment

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

please add a comment about the added condition "not disable_retries and not"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, see lines 154-155 as well

if not disable_retries and not retry_after_410 and \
obj['code'] == HTTP_STATUS_GONE:
retry_after_410 = True
break
Expand All @@ -190,5 +192,5 @@ def stream(self, func, *args, **kwargs):
else:
self._stop = True

if timeouts or self._stop:
if self._stop or disable_retries:
break
54 changes: 52 additions & 2 deletions watch/watch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,65 @@ def test_watch_with_error_event(self):
fake_api = Mock()
fake_api.get_thing = Mock(return_value=fake_resp)

w = Watch()
# No events are generated when no initial resourceVersion is passed
# No retry is attempted either, preventing an ApiException
assert not list(w.stream(fake_api.get_thing))
Comment on lines +291 to +293
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the existing test case, which was not actually correct for the current version of the code with retries. Additionally, I added 2 more test cases to test both code paths that my PR is related to.


fake_api.get_thing.assert_called_once_with(
_preload_content=False, watch=True)
fake_resp.read_chunked.assert_called_once_with(decode_content=False)
fake_resp.close.assert_called_once()
fake_resp.release_conn.assert_called_once()

def test_watch_retries_on_error_event(self):
fake_resp = Mock()
fake_resp.close = Mock()
fake_resp.release_conn = Mock()
fake_resp.read_chunked = Mock(
return_value=[
'{"type": "ERROR", "object": {"code": 410, '
'"reason": "Gone", "message": "error message"}}\n'])

fake_api = Mock()
fake_api.get_thing = Mock(return_value=fake_resp)

w = Watch()
try:
for _ in w.stream(fake_api.get_thing):
for _ in w.stream(fake_api.get_thing, resource_version=0):
self.fail(self, "Should fail with ApiException.")
except client.rest.ApiException:
pass

# Two calls should be expected during a retry
fake_api.get_thing.assert_has_calls(
[call(resource_version=0, _preload_content=False, watch=True)] * 2)
fake_resp.read_chunked.assert_has_calls(
[call(decode_content=False)] * 2)
assert fake_resp.close.call_count == 2
assert fake_resp.release_conn.call_count == 2

def test_watch_with_error_event_and_timeout_param(self):
fake_resp = Mock()
fake_resp.close = Mock()
fake_resp.release_conn = Mock()
fake_resp.read_chunked = Mock(
return_value=[
'{"type": "ERROR", "object": {"code": 410, '
'"reason": "Gone", "message": "error message"}}\n'])

fake_api = Mock()
fake_api.get_thing = Mock(return_value=fake_resp)

w = Watch()
try:
for _ in w.stream(fake_api.get_thing, timeout_seconds=10):
Copy link
Contributor

Choose a reason for hiding this comment

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

no resource_version?

Copy link
Contributor Author

@chrisayoub chrisayoub Feb 25, 2021

Choose a reason for hiding this comment

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

For testing this code path and condition, you do not actually need to supply resource_version here, as an exception is raised and only the code in the watch finally block will execute. resource_version is needed in the other test because the code path goes beyond the finally block

Copy link
Contributor

Choose a reason for hiding this comment

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

ok

self.fail(self, "Should fail with ApiException.")
except client.rest.ApiException:
pass

fake_api.get_thing.assert_called_once_with(
_preload_content=False, watch=True)
_preload_content=False, watch=True, timeout_seconds=10)
fake_resp.read_chunked.assert_called_once_with(decode_content=False)
fake_resp.close.assert_called_once()
fake_resp.release_conn.assert_called_once()
Expand Down