| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import io |
| 4 |
import tarfile |
| 5 |
import threading |
| 6 |
import time |
| 7 |
from typing import Any |
| 8 |
from urllib.error import URLError |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from harlequin.exception import HarlequinTzDataError |
| 13 |
from harlequin.windows_timezone import ( |
| 14 |
DOWNLOAD_DEADLINE_SECONDS, |
| 15 |
MAX_DOWNLOAD_BYTES, |
| 16 |
SOCKET_TIMEOUT_SECONDS, |
| 17 |
download_tzdata, |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
def _empty_tar_gz() -> bytes: |
| 22 |
"""A real, empty .tar.gz, so a fake first response gets past extraction.""" |
| 23 |
buffer = io.BytesIO() |
| 24 |
with tarfile.open(fileobj=buffer, mode="w:gz"): |
| 25 |
pass |
| 26 |
return buffer.getvalue() |
| 27 |
|
| 28 |
|
| 29 |
class _Trickle(io.RawIOBase): |
| 30 |
"""A peer that keeps the socket busy without ever finishing, which is what |
| 31 |
a per-operation timeout cannot catch.""" |
| 32 |
|
| 33 |
def read(self, size: int = -1) -> bytes: |
| 34 |
time.sleep(0.005) |
| 35 |
return b"x" |
| 36 |
|
| 37 |
|
| 38 |
def test_every_request_has_a_timeout(monkeypatch: pytest.MonkeyPatch) -> None: |
| 39 |
"""Both requests, not just the first: the one after a successful tarball is |
| 40 |
where a user is most likely to be sitting when the network goes bad.""" |
| 41 |
timeouts: list[Any] = [] |
| 42 |
|
| 43 |
def fake_urlopen(url: str, *args: Any, **kwargs: Any) -> Any: |
| 44 |
timeouts.append(kwargs.get("timeout")) |
| 45 |
if len(timeouts) == 1: |
| 46 |
# a real, empty gzip member, so the tarball step gets past extraction |
| 47 |
return io.BytesIO(_empty_tar_gz()) |
| 48 |
raise URLError("no network") |
| 49 |
|
| 50 |
monkeypatch.setattr("harlequin.windows_timezone.urlopen", fake_urlopen) |
| 51 |
with pytest.raises(HarlequinTzDataError): |
| 52 |
download_tzdata() |
| 53 |
assert timeouts == [SOCKET_TIMEOUT_SECONDS, SOCKET_TIMEOUT_SECONDS] |
| 54 |
|
| 55 |
|
| 56 |
def test_a_trickling_peer_hits_the_deadline(monkeypatch: pytest.MonkeyPatch) -> None: |
| 57 |
"""The socket timeout is per-operation, so only the overall deadline ends |
| 58 |
a transfer that never stalls but never finishes either.""" |
| 59 |
monkeypatch.setattr( |
| 60 |
"harlequin.windows_timezone.DOWNLOAD_DEADLINE_SECONDS", 0.25, raising=True |
| 61 |
) |
| 62 |
monkeypatch.setattr( |
| 63 |
"harlequin.windows_timezone.urlopen", |
| 64 |
lambda *args, **kwargs: _Trickle(), |
| 65 |
) |
| 66 |
started = time.monotonic() |
| 67 |
with pytest.raises(HarlequinTzDataError) as excinfo: |
| 68 |
download_tzdata() |
| 69 |
assert time.monotonic() - started < DOWNLOAD_DEADLINE_SECONDS |
| 70 |
assert "did not finish" in str(excinfo.value) |
| 71 |
|
| 72 |
|
| 73 |
def test_an_oversized_response_is_refused(monkeypatch: pytest.MonkeyPatch) -> None: |
| 74 |
class _Firehose(io.RawIOBase): |
| 75 |
def read(self, size: int = -1) -> bytes: |
| 76 |
return b"x" * (1024 * 1024) |
| 77 |
|
| 78 |
monkeypatch.setattr( |
| 79 |
"harlequin.windows_timezone.urlopen", lambda *args, **kwargs: _Firehose() |
| 80 |
) |
| 81 |
with pytest.raises(HarlequinTzDataError) as excinfo: |
| 82 |
download_tzdata() |
| 83 |
assert str(MAX_DOWNLOAD_BYTES) in str(excinfo.value) |
| 84 |
|
| 85 |
|
| 86 |
def test_stop_abandons_the_download_without_an_error( |
| 87 |
monkeypatch: pytest.MonkeyPatch, |
| 88 |
) -> None: |
| 89 |
"""Quitting is not a failure to report, and it must not wait for the peer.""" |
| 90 |
monkeypatch.setattr( |
| 91 |
"harlequin.windows_timezone.urlopen", lambda *args, **kwargs: _Trickle() |
| 92 |
) |
| 93 |
stop = threading.Event() |
| 94 |
stop.set() |
| 95 |
download_tzdata(stop=stop) |
| 96 |
|
| 97 |
|
| 98 |
def test_a_failed_download_names_the_exception( |
| 99 |
monkeypatch: pytest.MonkeyPatch, |
| 100 |
) -> None: |
| 101 |
def fake_urlopen(url: str, *args: Any, **kwargs: Any) -> Any: |
| 102 |
raise URLError("no network") |
| 103 |
|
| 104 |
monkeypatch.setattr("harlequin.windows_timezone.urlopen", fake_urlopen) |
| 105 |
with pytest.raises(HarlequinTzDataError) as excinfo: |
| 106 |
download_tzdata() |
| 107 |
assert "no network" in str(excinfo.value) |
| 108 |
assert "--no-download-tzdata" in str(excinfo.value) |