-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test to ensure 429 Too Many Requests is retried (#48)
Which, it turns out it is, and I too misread the code and thought it wasn't. So now there's test documentation for that behavior.
- Loading branch information
1 parent
dbd1756
commit 50d3745
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest | ||
import requests | ||
import time | ||
import turbopuffer as tpuf | ||
from turbopuffer import backend as tpuf_backend | ||
from unittest import mock | ||
|
||
|
||
def mock_response_returning(status_code, reason): | ||
response = mock.Mock(spec=requests.Response) | ||
response.status_code = status_code | ||
# None of these values really matter, they're just accessed in Backend, so | ||
# they need to exist. | ||
response.url = tpuf.api_base_url | ||
response.reason = reason | ||
response.headers = {} | ||
response.request = mock.Mock(spec=requests.PreparedRequest) | ||
response.request.url = response.url | ||
response.raise_for_status = lambda: requests.Response.raise_for_status(response) | ||
return response | ||
|
||
|
||
def test_500_retried(): | ||
backend = tpuf_backend.Backend("fake_api_key") | ||
backend.session.send = mock.MagicMock() | ||
backend.session.send.return_value = mock_response_returning(500, 'Internal Error') | ||
|
||
with mock.patch.object(time, 'sleep', return_value=None) as sleep: | ||
with pytest.raises(tpuf.error.APIError): | ||
backend.make_api_request('vectors', payload={}) | ||
assert sleep.call_count == tpuf.max_retries - 1 | ||
|
||
|
||
def test_429_retried(): | ||
backend = tpuf_backend.Backend("fake_api_key") | ||
backend.session.send = mock.MagicMock() | ||
backend.session.send.return_value = mock_response_returning(429, 'Too Many Requests') | ||
|
||
with mock.patch.object(time, 'sleep', return_value=None) as sleep: | ||
with pytest.raises(tpuf.error.APIError): | ||
backend.make_api_request('vectors', payload={}) | ||
assert sleep.call_count == tpuf.max_retries - 1 |