Skip to content

Commit

Permalink
Add integration test module
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pytest = "^6.2.2"
pytest-cov = "^2.11.1"
pytest-asyncio = "^0.14.0"
mypy = "^0.812"
requests = "^2.26.0"
responses = "^0.13.4"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
78 changes: 78 additions & 0 deletions tests/test_integration.py
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]

0 comments on commit c2f4e18

Please sign in to comment.