-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test for aiohttp failure when backoff is used
- Loading branch information
Stefan Tjarks
committed
Aug 15, 2018
1 parent
c5eb00d
commit 51150c4
Showing
1 changed file
with
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import pytest | ||
|
||
import backoff | ||
|
||
aiohttp = pytest.importorskip("aiohttp") | ||
aiohttp.web = pytest.importorskip("aiohttp.web") | ||
aiohttp.web_exceptions = pytest.importorskip("aiohttp.web_exceptions") | ||
|
||
|
||
async def test_backoff_on_all_session_requests(aiohttp_client): | ||
call_count = 0 | ||
|
||
async def handler(request): | ||
nonlocal call_count | ||
call_count += 1 | ||
raise aiohttp.web_exceptions.HTTPUnauthorized | ||
|
||
app = aiohttp.web.Application() | ||
app.router.add_get('/', handler) | ||
client = await aiohttp_client( | ||
app, | ||
connector=aiohttp.TCPConnector(limit=1), | ||
raise_for_status=True, | ||
) | ||
|
||
# retry every session request | ||
client._session._request = backoff.on_exception( | ||
wait_gen=backoff.expo, | ||
exception=aiohttp.client_exceptions.ClientResponseError, | ||
max_tries=2, | ||
)(client._session._request) | ||
|
||
with pytest.raises(aiohttp.client_exceptions.ClientResponseError) as exc: | ||
await client.get('/') | ||
|
||
assert exc.value.code == 401 | ||
assert call_count == 2 |