Skip to content

Commit

Permalink
retry on timeout (#54)
Browse files Browse the repository at this point in the history
* retry on timeout

Signed-off-by: zliu3 <[email protected]>

* add unit test and throw exception on timeout

Signed-off-by: zliu3 <[email protected]>

* update README.md

Signed-off-by: zliu3 <[email protected]>

* add test to make sure retry on exception, add a good case

Signed-off-by: zliu3 <[email protected]>

---------

Signed-off-by: zliu3 <[email protected]>
Co-authored-by: zliu3 <[email protected]>
  • Loading branch information
zejunintuit and zliu3 authored May 23, 2024
1 parent a468a55 commit 2e6be18
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ The simplest way to hit the ground running if you want to contribute with code i

Launch a python container
```shell
localhost$ docker run --rm -it -v $(pwd):$(pwd) -w $(pwd) python:3.7-stretch bash
localhost$ docker run --rm -it -v $(pwd):$(pwd) -w $(pwd) python:3.11-slim-bullseye bash
```

Install the project and test dependencies in developer mode
Expand Down
7 changes: 5 additions & 2 deletions gordian/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _set_target_branch(self, target_branch, source_branch=None):
self.branch_name = f"refs/heads/{datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S.%f')}"
self.source_branch = self.target_ref

@retry(GithubException, tries=3, delay=1, backoff=2)
@retry((GithubException, TimeoutError), tries=3, delay=1, backoff=2)
def _get_repo_contents(self, path):
try:
logger.debug(f'Fetching repo contents {path}...')
Expand All @@ -135,6 +135,9 @@ def _get_repo_contents(self, path):
if e.status == 404:
raise e
logger.info(f'Error fetching repo contents: {e}')
except TimeoutError as e:
logger.info(f'Error fetching repo contents: {e}')
raise e

@retry(GithubException, tries=3, delay=1, backoff=2)
def _make_branch(self):
Expand Down Expand Up @@ -248,6 +251,6 @@ def _get_new_version(self):
elif self.semver_label == 'patch':
patch = str(int(patch) + 1)
self.new_version = '.'.join([major, minor, patch])

def get_github_client(self):
return self._github
20 changes: 20 additions & 0 deletions tests/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,23 @@ def test__get_github_client(self):

self.assertIsNotNone(repo.get_github_client())
self.assertEqual(repo.get_github_client(), self.mock_git)

def test_get_repo_contents_timeout_error(self):
self.repo._set_target_branch('target')
self.repo.files = []
self.repo._source_repo = MagicMock()
self.repo._source_repo.get_contents.side_effect = TimeoutError('Read Timeout')
with pytest.raises(Exception) as context:
self.repo._get_repo_contents(path='test/afile.txt')
assert "Read Timeout" in str(context.value)
self.repo._source_repo.get_contents.assert_has_calls([call('test/afile.txt', 'target'), call('test/afile.txt', 'target'), call('test/afile.txt', 'target')])


def test_get_repo_contents(self):
self.repo._set_target_branch('target')
self.repo.files = []
self.repo._source_repo = MagicMock()
repository_file = MagicMock(path='test/afile.txt', type='not_dir')
self.repo._source_repo.get_contents.side_effect = [repository_file]
self.repo._get_repo_contents(path='test/afile.txt')
self.repo._source_repo.get_contents.assert_has_calls([call('test/afile.txt', 'target')])

0 comments on commit 2e6be18

Please sign in to comment.