Skip to content

Commit

Permalink
Add unit test for get_token retry behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mbthornton-lbl committed Dec 17, 2024
1 parent abc2b0e commit c5dec12
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions nmdc_automation/api/nmdcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ def _get_token(self, *args, **kwargs):
return _get_token

@retry(
wait=wait_exponential(multiplier=1, min=30, max=240),
stop=stop_after_attempt(4),
wait=wait_exponential(multiplier=4, min=8, max=120),
stop=stop_after_attempt(6),
reraise=True,
)
def get_token(self):
"""
Get a token using a client id/secret.
Retries up to 4 times with exponential backoff.
Retries up to 6 times with exponential backoff.
"""
h = {
"accept": "application/json",
Expand Down
24 changes: 24 additions & 0 deletions tests/test_nmdcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,27 @@ def test_jobs(requests_mock, site_config_file, mock_api):
requests_mock.post(url, json={}, status_code=409)
resp = n.claim_job("abc")
assert resp["claimed"] is True


def test_nmdcapi_get_token_with_retry(requests_mock, site_config_file):
n = nmdcapi(site_config_file)
token_url = "http://localhost:8000/token"

requests_mock.post(
token_url, [{"status_code": 401, "json": {"error": "Unauthorized"}},
{"status_code": 200, "json": {
"access_token": "mocked_access_token",
"expires": {"days": 1},
}}]
)
# sanity check
assert n.token is None
assert n.expires_at == 0

# Call method under test - should retry and succeed
n.get_token()

# Check that the token was set
assert n.token == "mocked_access_token"
assert n.expires_at > 0

0 comments on commit c5dec12

Please sign in to comment.