Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Retry on timeouts during getting remote amschema
Browse files Browse the repository at this point in the history
  • Loading branch information
roel-devries committed Jan 25, 2024
1 parent e904c31 commit 572b177
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
18 changes: 14 additions & 4 deletions gobcore/model/amschema/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import os

import requests
from pydash import snake_case
from requests import Session
from requests.adapters import HTTPAdapter
from urllib3.util import Retry

from gobcore.model import Schema
from gobcore.model.amschema.model import Dataset, Table
Expand All @@ -25,8 +26,17 @@ def _get_file(self, location: str):
return self._load_file(location)

def _download_file(self, location: str):
r = requests.get(location, timeout=5)
r.raise_for_status()
retries = Retry(
total=6,
backoff_factor=1, # 1 x 2^0 until 1 x 2^6
status_forcelist=[502, 503, 504],
allowed_methods={"GET"},
)

with Session() as session:
session.mount("https://", HTTPAdapter(max_retries=retries))
r = session.get(location, timeout=10)
r.raise_for_status()

return r.json()

Expand Down
21 changes: 17 additions & 4 deletions tests/gobcore/model/amschema/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ def test_repo_base(self):
instance.get_schema(schema)
instance._download_dataset.assert_called_with("https://test.repo/datasets/nap/dataset.json")

@patch("gobcore.model.amschema.repo.requests")
def test_download_dataset(self, mock_requests):
@patch("gobcore.model.amschema.repo.HTTPAdapter")
@patch("gobcore.model.amschema.repo.Retry")
@patch("gobcore.model.amschema.repo.Session")
def test_download_dataset(self, mock_session, mock_retry, mock_http):
"""Test remote (HTTP) AMS schema dataset."""
mock_session_cm = mock_session.return_value.__enter__.return_value

with open(Path(__file__).parent.parent.parent.joinpath('amschema_fixtures/dataset.json')) as f:
filecontents = f.read()
mock_requests.get.return_value.json = lambda: json.loads(filecontents)
mock_session_cm.get.return_value.json = lambda: json.loads(filecontents)

instance = AMSchemaRepository()
dataset = get_dataset()
Expand All @@ -71,7 +75,16 @@ def test_download_dataset(self, mock_requests):
result = instance._download_dataset(download_url)
self.assertEqual(result, dataset)

mock_requests.get.assert_called_with(download_url, timeout=5)
mock_retry.assert_called_with(
total=6,
backoff_factor=1,
status_forcelist=[502, 503, 504],
allowed_methods={"GET"}
)
mock_http.assert_called_with(max_retries=mock_retry.return_value)
mock_session_cm.get.assert_called_with(download_url, timeout=10)
mock_session_cm.get.return_value.raise_for_status.assert_called_once()
mock_session_cm.mount.assert_called_with("https://", mock_http.return_value)

@patch("gobcore.model.amschema.repo.json_to_cached_dict")
def test_local_table(self, mock_cached_dict):
Expand Down

0 comments on commit 572b177

Please sign in to comment.