Skip to content

Commit

Permalink
Test Retry-After support with multiple local timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
hodbn committed Aug 26, 2020
1 parent f639520 commit 258cffc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dev-requirements.txt
Expand Up @@ -15,3 +15,6 @@ gcp-devrel-py-tools==0.0.15

# https://github.com/GoogleCloudPlatform/python-repo-tools/issues/23
pylint<2.0;python_version<="2.7"

pytz==2020.1
tzlocal==2.1
11 changes: 11 additions & 0 deletions test/conftest.py
Expand Up @@ -8,6 +8,8 @@
import trustme
from tornado import web, ioloop

from .tz_fake import fake_timezone_ctx

from dummyserver.handlers import TestingApp
from dummyserver.server import run_tornado_app
from dummyserver.server import HAS_IPV6
Expand Down Expand Up @@ -96,3 +98,12 @@ def ipv6_san_server(tmp_path_factory):

with run_server_in_thread("https", "::1", tmpdir, ca, server_cert) as cfg:
yield cfg


@pytest.yield_fixture
def fake_timezone(request):
"""
A pytest fixture that runs the test with a fake timezone.
"""
with fake_timezone_ctx(request.param):
yield
10 changes: 10 additions & 0 deletions test/test_retry.py
Expand Up @@ -332,6 +332,16 @@ def test_respect_retry_after_header_propagated(self, respect_retry_after_header)
("Mon Jun 3 11:30:12 2019", True, 1812),
],
)
@pytest.mark.parametrize(
"fake_timezone",
[
"UTC",
"Asia/Jerusalem",
None,
],
indirect=True,
)
@pytest.mark.usefixtures("fake_timezone")
def test_respect_retry_after_header_sleep(
self, retry_after_header, respect_retry_after_header, sleep_duration
):
Expand Down
37 changes: 37 additions & 0 deletions test/tz_fake.py
@@ -0,0 +1,37 @@
import pytz
from contextlib import contextmanager
import time
import os
import pytest
import tzlocal


@contextmanager
def fake_timezone_ctx(tz):
"""
Switch to a locally-known timezone specified by `tz`.
On exit, restore the previous timezone.
If `tz` is `None`, do nothing.
"""
if tz is None:
yield
return

if not hasattr(time, "tzset"):
pytest.skip("Timezone patching is not supported")

# Make sure the new timezone exists, at least in pytz
if tz not in pytz.all_timezones:
raise ValueError("Invalid timezone specified: %r" % (tz,))

# Get the current timezone
try:
old_tz = tzlocal.get_localzone().zone
except ValueError:
pytest.skip("Cannot determine current timezone")

os.environ["TZ"] = tz
time.tzset()
yield
os.environ["TZ"] = old_tz
time.tzset()

0 comments on commit 258cffc

Please sign in to comment.