Skip to content

Commit

Permalink
Make mirror tests bandwidth agnostic.
Browse files Browse the repository at this point in the history
  • Loading branch information
bwoodsend committed Sep 28, 2023
1 parent c9963c5 commit 6d7f450
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion polycotylus/_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 43 additions & 11 deletions tests/test_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 6d7f450

Please sign in to comment.