From 6d7f45078a7f4fedbd5b370c38b4e8cc62ae9002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=A9nainn=20Woodsend?= Date: Fri, 29 Sep 2023 00:15:35 +0100 Subject: [PATCH] Make mirror tests bandwidth agnostic. --- polycotylus/_mirror.py | 2 +- tests/test_mirror.py | 54 +++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/polycotylus/_mirror.py b/polycotylus/_mirror.py index c1360f9..1fc98bb 100644 --- a/polycotylus/_mirror.py +++ b/polycotylus/_mirror.py @@ -292,7 +292,7 @@ def _download(self): with self.upstream: with open(cache, "wb") as f: shutil.copyfileobj(self.upstream, f) - except: # pragma: no cover + except: with contextlib.suppress(FileNotFoundError): cache.unlink() raise diff --git a/tests/test_mirror.py b/tests/test_mirror.py index 4f6832a..7d026f3 100644 --- a/tests/test_mirror.py +++ b/tests/test_mirror.py @@ -9,6 +9,9 @@ from pathlib import Path import sys import subprocess +import contextlib +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from http import HTTPStatus import pytest @@ -29,6 +32,23 @@ def _alpine_mirror(tmp_path): ) +@contextlib.contextmanager +def fake_upstream(do_GET): + + class RequestHandler(BaseHTTPRequestHandler): + pass + + RequestHandler.do_GET = do_GET + + with ThreadingHTTPServer(("", 8899), RequestHandler) as httpd: + try: + thread = threading.Thread(target=httpd.serve_forever) + thread.start() + yield + finally: + httpd.shutdown() + + def test_basic(tmp_path): self = _alpine_mirror(tmp_path) url = "http://localhost:9989/MIRRORS.txt" @@ -160,17 +180,29 @@ def test_index_page_handling(tmp_path): def test_concurrent(tmp_path): - self = _alpine_mirror(tmp_path) - url = "http://localhost:9989/edge/main/x86_64/APKINDEX.tar.gz" - - with self: - for i in range(3): - with urlopen(url) as a: - part = a.read(100) - with urlopen(url) as b: - raw = b.read() - gzip.decompress(raw) - gzip.decompress(part + a.read()) + url = "http://localhost:9989/foo.bar" + self = CachedMirror( + "http://0.0.0.0:8899", tmp_path, ["*.bar"], [], 9989, "", (_alpine_sync_time,)) + payload = os.urandom(300_000) + + def upstream_get(self): + self.send_response(HTTPStatus.OK) + self.send_header("Content-Length", 300_000) + self.end_headers() + for i in range(0, 300_000, 100): + # Mimic a slow download rate. + self.wfile.write(payload[i: i + 100]) + time.sleep(0.001) + + with fake_upstream(upstream_get): + with self: + for i in range(3): + with urlopen(url) as a: + part = a.read(100) + with urlopen(url) as b: + raw = b.read() + assert raw == payload + assert part + a.read() == payload def test_kill_resume(tmp_path):