forked from kenzuya/unshackle
test(tests): make the body-retry, websocket debug-entry and gzip segment tests deterministic
This commit is contained in:
@@ -10,12 +10,16 @@ from tests.core.test_rolling_merge_hls_ism import INIT, PARTS, hls_track, make_c
|
|||||||
from unshackle.core.manifests import HLS
|
from unshackle.core.manifests import HLS
|
||||||
|
|
||||||
|
|
||||||
|
def cut_gzip(part: bytes) -> bytes:
|
||||||
|
return gzip.compress(part, mtime=0)[:-4]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"body, expected",
|
"body, expected",
|
||||||
[
|
[
|
||||||
pytest.param(gzip.compress, lambda part: part, id="gzip"),
|
pytest.param(gzip.compress, lambda part: part, id="gzip"),
|
||||||
pytest.param(lambda part: b"\x1f\x8b\x08" + part, lambda part: b"\x1f\x8b\x08" + part, id="gzip-magic-only"),
|
pytest.param(lambda part: b"\x1f\x8b\x08" + part, lambda part: b"\x1f\x8b\x08" + part, id="gzip-magic-only"),
|
||||||
pytest.param(lambda part: gzip.compress(part)[:-4], lambda part: gzip.compress(part)[:-4], id="cut-gzip"),
|
pytest.param(cut_gzip, cut_gzip, id="cut-gzip"),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_only_a_whole_gzip_segment_is_inflated(body, expected, server, tmp_path): # noqa: F811
|
def test_only_a_whole_gzip_segment_is_inflated(body, expected, server, tmp_path): # noqa: F811
|
||||||
|
|||||||
@@ -34,8 +34,9 @@ def server():
|
|||||||
return
|
return
|
||||||
with conn:
|
with conn:
|
||||||
conn.recv(65536)
|
conn.recv(65536)
|
||||||
conn.sendall(plan[len(hits)] if len(hits) < len(plan) else CUT)
|
reply = plan[len(hits)] if len(hits) < len(plan) else CUT
|
||||||
hits.append(1)
|
hits.append(1)
|
||||||
|
conn.sendall(reply)
|
||||||
|
|
||||||
threading.Thread(target=serve, daemon=True).start()
|
threading.Thread(target=serve, daemon=True).start()
|
||||||
yield f"http://127.0.0.1:{srv.getsockname()[1]}/x", plan, hits
|
yield f"http://127.0.0.1:{srv.getsockname()[1]}/x", plan, hits
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import re
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@@ -106,9 +107,9 @@ async def test_each_handshake_writes_one_debug_entry(debug_log: Path) -> None:
|
|||||||
with pytest.raises(rnet.ConnectionError):
|
with pytest.raises(rnet.ConnectionError):
|
||||||
await call(session.websocket, closed_port().replace("http://", "ws://", 1), GIVEN)
|
await call(session.websocket, closed_port().replace("http://", "ws://", 1), GIVEN)
|
||||||
|
|
||||||
assert [
|
first, refused = [
|
||||||
(entry["request"]["method"], entry["response"]["status"], entry["failure"]) for entry in records(debug_log)
|
(entry["request"]["method"], entry["response"]["status"], entry["failure"]) for entry in records(debug_log)
|
||||||
] == [
|
|
||||||
("GET", 101, None),
|
|
||||||
("GET", None, "ConnectionError (Connection refused)"),
|
|
||||||
]
|
]
|
||||||
|
assert first == ("GET", 101, None)
|
||||||
|
assert refused[:2] == ("GET", None)
|
||||||
|
assert re.fullmatch(r"ConnectionError \([^/]+\)", refused[2])
|
||||||
|
|||||||
@@ -54,15 +54,15 @@ class _Handler(BaseHTTPRequestHandler):
|
|||||||
server.release.wait(5)
|
server.release.wait(5)
|
||||||
with server.lock:
|
with server.lock:
|
||||||
server.others_done_at_release = len(server.served)
|
server.others_done_at_release = len(server.served)
|
||||||
|
with server.lock:
|
||||||
|
server.served.append(header)
|
||||||
|
if not held and len(server.served) == 7:
|
||||||
|
server.release.set()
|
||||||
self.send_response(206)
|
self.send_response(206)
|
||||||
self.send_header("Content-Range", f"bytes {start}-{end}/{len(BODY)}")
|
self.send_header("Content-Range", f"bytes {start}-{end}/{len(BODY)}")
|
||||||
self.send_header("Content-Length", str(len(body)))
|
self.send_header("Content-Length", str(len(body)))
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(body)
|
self.wfile.write(body)
|
||||||
with server.lock:
|
|
||||||
server.served.append(header)
|
|
||||||
if not held and len(server.served) == 7:
|
|
||||||
server.release.set()
|
|
||||||
|
|
||||||
|
|
||||||
def test_fast_connection_takes_the_parts_a_held_connection_cannot(tmp_path, monkeypatch):
|
def test_fast_connection_takes_the_parts_a_held_connection_cannot(tmp_path, monkeypatch):
|
||||||
|
|||||||
Reference in New Issue
Block a user