-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is for higher level tests, perhaps relying on 3rd party libraries (dev only dependencies.) This first set uses the `requests` module to test some idiomatic patterns using the `runtime` wait generator.
- Loading branch information
Bob Green
committed
Aug 19, 2021
1 parent
d2f28d1
commit c2f4e18
Showing
2 changed files
with
80 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,78 @@ | ||
"""Integration tests | ||
Higher-level tests integrating with 3rd party modules using iodiomatic | ||
backoff patterns. | ||
""" | ||
|
||
import backoff | ||
|
||
|
||
import requests | ||
from requests import HTTPError | ||
import responses | ||
|
||
|
||
@responses.activate | ||
def test_on_predicate_runtime(monkeypatch): | ||
|
||
log = [] | ||
|
||
def sleep(seconds): | ||
log.append(seconds) | ||
|
||
monkeypatch.setattr("time.sleep", sleep) | ||
|
||
url = "http://example.com" | ||
|
||
responses.add(responses.GET, url, status=429, headers={"Retry-After": "1"}) | ||
responses.add(responses.GET, url, status=429, headers={"Retry-After": "3"}) | ||
responses.add(responses.GET, url, status=429, headers={"Retry-After": "7"}) | ||
responses.add(responses.GET, url, status=200) | ||
|
||
@backoff.on_predicate( | ||
backoff.runtime, | ||
predicate=lambda r: r.status_code == 429, | ||
value=lambda r: int(r.headers.get("Retry-After")), | ||
jitter=None, | ||
) | ||
def get_url(): | ||
return requests.get(url) | ||
|
||
resp = get_url() | ||
assert resp.status_code == 200 | ||
|
||
assert log == [1, 3, 7] | ||
|
||
|
||
@responses.activate | ||
def test_on_exception_runtime(monkeypatch): | ||
|
||
log = [] | ||
|
||
def sleep(seconds): | ||
log.append(seconds) | ||
|
||
monkeypatch.setattr("time.sleep", sleep) | ||
|
||
url = "http://example.com" | ||
|
||
responses.add(responses.GET, url, status=429, headers={"Retry-After": "1"}) | ||
responses.add(responses.GET, url, status=429, headers={"Retry-After": "3"}) | ||
responses.add(responses.GET, url, status=429, headers={"Retry-After": "7"}) | ||
responses.add(responses.GET, url, status=200) | ||
|
||
@backoff.on_exception( | ||
backoff.runtime, | ||
HTTPError, | ||
value=lambda e: int(e.response.headers.get("Retry-After")), | ||
jitter=None, | ||
) | ||
def get_url(): | ||
resp = requests.get(url) | ||
resp.raise_for_status() | ||
return resp | ||
|
||
resp = get_url() | ||
assert resp.status_code == 200 | ||
|
||
assert log == [1, 3, 7] |