8000 Retry all methods on 429 by adamjmcgrath · Pull Request #518 · auth0/auth0-python · GitHub
[go: up one dir, main page]

Skip to content

Retry all methods on 429 #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
8000 Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
adamjmcgrath committed Aug 25, 2023
commit 68d71c0843397d102b9c421bbb58c12aee6ac32d
20 changes: 18 additions & 2 deletions auth0/test/management/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import unittest
from unittest import mock

import requests

from auth0.rest import RestClient, RestClientOptions

from ...exceptions import Auth0Error, RateLimitError
Expand Down Expand Up @@ -475,6 +473,24 @@ def test_get_rate_limit_retries_use_exponential_backoff(self, mock_request):
# Ensure total delay sum is never more than 10s.
self.assertLessEqual(finalBackoff, 10000)

@mock.patch("requests.request")
def test_post_rate_limit_retries(self, mock_request):
options = RestClientOptions(telemetry=False, retries=10)
rc = RestClient(jwt="a-token", options=options)
rc._skip_sleep = True

mock_request.return_value.text = (
'{"statusCode": 429, "errorCode": "code", "message": "message"}'
)
mock_request.return_value.status_code = 429

with self.assertRaises(Auth0Error) as context:
rc.post("the/url")

self.assertEqual(context.exception.status_code, 429)

self.assertEqual(len(rc._metrics["backoff"]), 10)

@mock.patch("requests.request")
def test_post(self, mock_request):
rc = RestClient(jwt="a-token", telemetry=False)
Expand Down
14 changes: 14 additions & 0 deletions auth0/test_async/test_asyncify.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,20 @@ async def test_rate_limit(self, mocked):
(a, b, c) = rest_client._metrics["backoff"]
self.assertTrue(100 <= a < b < c <= 1000)

@pytest.mark.asyncio
@aioresponses()
async def test_rate_limit_post(self, mocked):
callback, mock = get_callback(status=429)
await mocked.post(clients, callback=callback)
await mocked.post(clients, callback=callback)
await mocked.post(clients, callback=callback)
await mocked.post(clients, payload=payload)
c = asyncify(Clients)(domain="example.com", token="jwt")
rest_client = c._async_client.client
rest_client._skip_sleep = True
self.assertEqual(await c.all_async(), payload)
self.assertEqual(3, mock.call_count)

@pytest.mark.asyncio
@aioresponses()
async def test_timeout(self, mocked):
Expand Down
0