mirror of
https://github.com/db9979/uconsole_asw_game.git
synced 2026-12-05 20:34:16 +01:00
- Server: a reconnecting client supersedes its own idle audio or sonar stream instead of getting 409 stream_exists while the old worker has not yet noticed the dead socket (root cause of the flaky test_audio_websocket_resumes_behind_the_browser_cursor). Regression tests fail on the old code. - Web client: a push-woken poll clears the pending poll timer, so a healthy push no longer adds a /state request per pushed state. - Browser tests for live audio and the state push run in real time (RealTimeHost: wall-clock sim steps via Game._frame_dt, DevTools reads off-thread) instead of fixed steps or --virtual-time-budget. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017XUBGQzKG1jnToU9vYPfwo
325 lines
15 KiB
Python
325 lines
15 KiB
Python
"""Bounded binary Sonar stream over the authenticated protocol-v2 session."""
|
|
|
|
import base64
|
|
import json
|
|
import os
|
|
import socket
|
|
import struct
|
|
|
|
import pytest
|
|
|
|
from src.commander import server as transport
|
|
from test_commander_sessions_v2 import pair_v2, projection_states, server
|
|
|
|
|
|
def _publish_sonar(server):
|
|
states = projection_states("stream-world")
|
|
state = states["sonar"]
|
|
state["clock"] = {"sim": 12.5}
|
|
state["sonar"] = {"visualization": {
|
|
"broadband": {"history": [{"age_s": 1.5, "bins": [0.0, .5, 1.0]}]},
|
|
"lofar": {"history": [{"age_s": .25, "bearing": 72.0,
|
|
"bins": [.1, .2]}],
|
|
"spectrum": [.25, .75]},
|
|
"demon": {"history": [{"age_s": .5, "bins": [.4]}],
|
|
"spectrum": [.6]},
|
|
"receiver": {"listen_bearing": 71.0},
|
|
}}
|
|
chart = dict(protocol=2, revision="stream-world", size_nm=500,
|
|
landmasses=[], disclaimer="")
|
|
server.publish_v2(states, {role: chart for role in states})
|
|
|
|
|
|
def test_binary_sonar_payload_is_versioned_bounded_and_quantized():
|
|
state = projection_states("stream-world")["sonar"]
|
|
state["clock"] = {"sim": 12.5}
|
|
state["sonar"] = {"visualization": {
|
|
"broadband": {"history": [{"age_s": 1.0, "bins": [0, .5, 1]}]},
|
|
"lofar": {"history": [{"age_s": .25, "bearing": 15,
|
|
"bins": [.25]}], "spectrum": [.75]},
|
|
"demon": {"history": [], "spectrum": []},
|
|
"receiver": {"listen_bearing": 15},
|
|
}}
|
|
context, payload = transport._sonar_stream_payload(state, 7)
|
|
assert context == ("stream-world", 0)
|
|
assert payload[:4] == b"UJS2" and payload[4] == 1
|
|
assert len(payload) <= transport.SONAR_STREAM_MAX_BYTES
|
|
header = struct.unpack("<4sBBHQQdfHHHHHHfff", payload[:60])
|
|
assert header[3:6] == (60, 7, 0)
|
|
assert header[8:13] == (3, 1, 0, 1, 0)
|
|
assert payload[60:] == bytes((0, 128, 255, 64, 191))
|
|
|
|
|
|
def test_compact_poll_projection_omits_only_streamed_sonar_arrays(server):
|
|
_publish_sonar(server)
|
|
compact = json.loads(server._v2_sonar_compact_state)
|
|
visual = compact["sonar"]["visualization"]
|
|
assert visual["broadband"]["history"] == []
|
|
assert visual["lofar"]["history"] == []
|
|
assert visual["lofar"]["spectrum"] == []
|
|
assert visual["demon"]["history"] == []
|
|
assert visual["demon"]["spectrum"] == []
|
|
assert visual["receiver"] == {"listen_bearing": 71.0}
|
|
|
|
|
|
def test_websocket_requires_session_and_active_sonar_lease(server):
|
|
_publish_sonar(server)
|
|
_, cookie, _, paired = pair_v2(server, "Sonar stream")
|
|
assert server.grant_station(paired["client_id"], "sonar")
|
|
host, port = server.address
|
|
key = base64.b64encode(os.urandom(16)).decode("ascii")
|
|
request = (
|
|
f"GET {transport.SONAR_STREAM_ROUTE} HTTP/1.1\r\n"
|
|
f"Host: {host}:{port}\r\nOrigin: http://{host}:{port}\r\n"
|
|
"Upgrade: websocket\r\nConnection: Upgrade\r\n"
|
|
f"Sec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n"
|
|
"Sec-WebSocket-Protocol: u-jagd-sonar-v2\r\n"
|
|
f"Cookie: {cookie}\r\n\r\n").encode("ascii")
|
|
with socket.create_connection(server.address, timeout=3) as connection:
|
|
connection.sendall(request)
|
|
received = bytearray()
|
|
while b"\r\n\r\n" not in received:
|
|
received.extend(connection.recv(4096))
|
|
headers, frame = bytes(received).split(b"\r\n\r\n", 1)
|
|
assert headers.startswith(b"HTTP/1.1 101")
|
|
assert b"Sec-WebSocket-Protocol: u-jagd-sonar-v2" in headers
|
|
while len(frame) < 2:
|
|
frame += connection.recv(4096)
|
|
assert frame[0] == 0x82
|
|
length = frame[1] & 0x7f
|
|
if length == 126:
|
|
while len(frame) < 4:
|
|
frame += connection.recv(4096)
|
|
length = struct.unpack("!H", frame[2:4])[0]
|
|
offset = 4
|
|
else:
|
|
offset = 2
|
|
while len(frame) < offset + length:
|
|
frame += connection.recv(4096)
|
|
assert frame[offset:offset + 4] == b"UJS2"
|
|
assert length <= transport.SONAR_STREAM_MAX_BYTES
|
|
|
|
|
|
@pytest.mark.parametrize("role", ("sonar", "helicopter"))
|
|
def test_audio_websocket_is_leased_sequenced_and_revoked(server, role):
|
|
_, cookie, _, paired = pair_v2(server, "Audio stream")
|
|
client_id = paired["client_id"]
|
|
assert server.grant_station(client_id, role)
|
|
assert server.set_client_grant(client_id, role, "sonar_audio", True)
|
|
prepare = (server.prepare_sonar_audio if role == "sonar"
|
|
else server.prepare_helicopter_audio)
|
|
publish = (server.publish_sonar_audio if role == "sonar"
|
|
else server.publish_helicopter_audio)
|
|
generation = prepare(world_session="audio-world", world_epoch=3)
|
|
assert generation is not None
|
|
pcm = bytes(transport.SONAR_AUDIO_BYTES)
|
|
# Ten pending blocks: a fresh socket gets the newest eight (the browser's
|
|
# priming lead), the first two are skipped.
|
|
published = transport.SONAR_AUDIO_RESUME_BLOCKS + 2
|
|
for _ in range(published):
|
|
assert publish(pcm, world_session="audio-world",
|
|
world_epoch=3, station_generation=generation)
|
|
host, port = server.address
|
|
key = base64.b64encode(os.urandom(16)).decode("ascii")
|
|
|
|
def handshake(cookie_value):
|
|
cookie_header = f"Cookie: {cookie_value}\r\n" if cookie_value else ""
|
|
return (f"GET /ws/v2/{role}/audio HTTP/1.1\r\nHost: {host}:{port}\r\n"
|
|
f"Origin: http://{host}:{port}\r\nUpgrade: websocket\r\n"
|
|
f"Connection: Upgrade\r\nSec-WebSocket-Key: {key}\r\n"
|
|
"Sec-WebSocket-Version: 13\r\nSec-WebSocket-Protocol: u-jagd-audio-v2\r\n"
|
|
f"{cookie_header}"
|
|
"\r\n").encode("ascii")
|
|
|
|
with socket.create_connection(server.address, timeout=3) as unauthorized:
|
|
unauthorized.sendall(handshake(""))
|
|
assert unauthorized.recv(1024).startswith(b"HTTP/1.1 401")
|
|
with socket.create_connection(server.address, timeout=3) as connection:
|
|
connection.settimeout(3)
|
|
connection.sendall(handshake(cookie))
|
|
received = bytearray()
|
|
while b"\r\n\r\n" not in received:
|
|
received.extend(connection.recv(4096))
|
|
headers, frame = bytes(received).split(b"\r\n\r\n", 1)
|
|
assert headers.startswith(b"HTTP/1.1 101")
|
|
while len(frame) < 4:
|
|
frame += connection.recv(4096)
|
|
assert frame[0] == 0x82 and frame[1] == 126
|
|
size = struct.unpack("!H", frame[2:4])[0]
|
|
while len(frame) < size + 4:
|
|
frame += connection.recv(4096)
|
|
payload = frame[4:4 + size]
|
|
assert len(payload) == 2060 and payload[:4] == b"UJA2"
|
|
assert struct.unpack("<Q", payload[4:12])[0] == 3
|
|
assert server.audio_stream_stats()[role]["skipped_blocks"] == 2
|
|
assert payload[12:] == pcm
|
|
assert server.set_client_grant(client_id, role, "sonar_audio", False)
|
|
# Already queued frames may arrive first. The worker must close after
|
|
# its next bounded lease check rather than keep streaming indefinitely.
|
|
while connection.recv(4096):
|
|
pass
|
|
assert server.set_client_grant(client_id, role, "sonar_audio", True)
|
|
generation = prepare(world_session="audio-world", world_epoch=3)
|
|
assert publish(pcm, world_session="audio-world", world_epoch=3,
|
|
station_generation=generation)
|
|
with socket.create_connection(server.address, timeout=3) as reconnected:
|
|
reconnected.settimeout(3)
|
|
reconnected.sendall(handshake(cookie))
|
|
response = bytearray()
|
|
while b"\r\n\r\n" not in response:
|
|
response.extend(reconnected.recv(4096))
|
|
assert response.startswith(b"HTTP/1.1 101")
|
|
_, frame = bytes(response).split(b"\r\n\r\n", 1)
|
|
while len(frame) < 16:
|
|
frame += reconnected.recv(4096)
|
|
# The regranted stream continues above every sequence sent before.
|
|
assert struct.unpack("<Q", frame[8:16])[0] > published
|
|
|
|
|
|
def test_audio_websocket_resumes_behind_the_browser_cursor(server):
|
|
"""``?after=N`` resumes a reconnect without re-sending held blocks."""
|
|
_, cookie, _, paired = pair_v2(server, "Resume")
|
|
client_id = paired["client_id"]
|
|
assert server.grant_station(client_id, "sonar")
|
|
assert server.set_client_grant(client_id, "sonar", "sonar_audio", True)
|
|
generation = server.prepare_sonar_audio(world_session="audio-world", world_epoch=1)
|
|
blocks = [bytes([value]) * transport.SONAR_AUDIO_BYTES for value in range(1, 13)]
|
|
for pcm in blocks:
|
|
assert server.publish_sonar_audio(pcm, world_session="audio-world",
|
|
world_epoch=1, station_generation=generation)
|
|
host, port = server.address
|
|
|
|
def handshake(query):
|
|
key = base64.b64encode(os.urandom(16)).decode("ascii")
|
|
return (f"GET /ws/v2/sonar/audio{query} HTTP/1.1\r\nHost: {host}:{port}\r\n"
|
|
f"Origin: http://{host}:{port}\r\nUpgrade: websocket\r\n"
|
|
f"Connection: Upgrade\r\nSec-WebSocket-Key: {key}\r\n"
|
|
"Sec-WebSocket-Version: 13\r\nSec-WebSocket-Protocol: u-jagd-audio-v2\r\n"
|
|
f"Cookie: {cookie}\r\n\r\n").encode("ascii")
|
|
|
|
def first_frame(query):
|
|
with socket.create_connection(server.address, timeout=3) as connection:
|
|
connection.settimeout(3)
|
|
connection.sendall(handshake(query))
|
|
received = bytearray()
|
|
while b"\r\n\r\n" not in received:
|
|
received.extend(connection.recv(4096))
|
|
headers, frame = bytes(received).split(b"\r\n\r\n", 1)
|
|
if not headers.startswith(b"HTTP/1.1 101"):
|
|
return headers.split(b"\r\n", 1)[0], None
|
|
while len(frame) < 16:
|
|
frame += connection.recv(4096)
|
|
return headers.split(b"\r\n", 1)[0], struct.unpack("<Q", frame[8:16])[0]
|
|
|
|
# Without a cursor: the newest priming lead only, older blocks skipped.
|
|
assert first_frame("") == (b"HTTP/1.1 101 Switching Protocols",
|
|
len(blocks) - transport.SONAR_AUDIO_RESUME_BLOCKS + 1)
|
|
assert server.audio_stream_stats()["sonar"]["skipped_blocks"] == (
|
|
len(blocks) - transport.SONAR_AUDIO_RESUME_BLOCKS)
|
|
# Behind a cursor inside the lead: exactly the next block, nothing repeated.
|
|
assert first_frame(f"?after={len(blocks) - 2}") == (b"HTTP/1.1 101 Switching Protocols", len(blocks) - 1)
|
|
# Malformed cursors are refused before the upgrade.
|
|
for query in ("?after=x", "?after=-1", "?after=01", "?after=1&x=2", "?since=3",
|
|
f"?after={2**53}"):
|
|
status, sequence = first_frame(query)
|
|
assert status == b"HTTP/1.1 400 Bad Request" and sequence is None, query
|
|
assert server.audio_stream_stats()["sonar"]["connections"] == 2
|
|
|
|
|
|
def _upgrade(address, path, protocol, cookie):
|
|
host, port = address
|
|
key = base64.b64encode(os.urandom(16)).decode("ascii")
|
|
connection = socket.create_connection(address, timeout=3)
|
|
connection.settimeout(3)
|
|
connection.sendall((
|
|
f"GET {path} HTTP/1.1\r\nHost: {host}:{port}\r\n"
|
|
f"Origin: http://{host}:{port}\r\nUpgrade: websocket\r\n"
|
|
f"Connection: Upgrade\r\nSec-WebSocket-Key: {key}\r\n"
|
|
f"Sec-WebSocket-Version: 13\r\nSec-WebSocket-Protocol: {protocol}\r\n"
|
|
f"Cookie: {cookie}\r\n\r\n").encode("ascii"))
|
|
received = bytearray()
|
|
while b"\r\n\r\n" not in received:
|
|
chunk = connection.recv(4096)
|
|
assert chunk, bytes(received)
|
|
received.extend(chunk)
|
|
headers, rest = bytes(received).split(b"\r\n\r\n", 1)
|
|
return connection, headers.split(b"\r\n", 1)[0], bytearray(rest)
|
|
|
|
|
|
def _frames(connection, buffered, count):
|
|
"""Read ``count`` server frames as (opcode, payload)."""
|
|
frames = []
|
|
while len(frames) < count:
|
|
while True:
|
|
if len(buffered) >= 2:
|
|
length, offset = buffered[1] & 0x7f, 2
|
|
if length == 126 and len(buffered) >= 4:
|
|
length, offset = struct.unpack("!H", buffered[2:4])[0], 4
|
|
if length != 126 and len(buffered) >= offset + length:
|
|
break
|
|
chunk = connection.recv(65536)
|
|
assert chunk, "socket closed early"
|
|
buffered.extend(chunk)
|
|
frames.append((buffered[0] & 0x0f, bytes(buffered[offset:offset + length])))
|
|
del buffered[:offset + length]
|
|
return frames
|
|
|
|
|
|
def _closed_by_server(connection):
|
|
while connection.recv(4096):
|
|
pass
|
|
return True
|
|
|
|
|
|
def test_audio_reconnect_supersedes_the_clients_idle_stream(server):
|
|
"""A browser reconnecting before the host noticed its old socket died
|
|
takes the stream over instead of being refused (409) while the old
|
|
worker idles between blocks."""
|
|
_, cookie, _, paired = pair_v2(server, "Supersede")
|
|
client_id = paired["client_id"]
|
|
assert server.grant_station(client_id, "sonar")
|
|
assert server.set_client_grant(client_id, "sonar", "sonar_audio", True)
|
|
generation = server.prepare_sonar_audio(world_session="audio-world", world_epoch=1)
|
|
for value in range(1, transport.SONAR_AUDIO_RESUME_BLOCKS + 1):
|
|
assert server.publish_sonar_audio(
|
|
bytes([value]) * transport.SONAR_AUDIO_BYTES, world_session="audio-world",
|
|
world_epoch=1, station_generation=generation)
|
|
old, status, buffered = _upgrade(server.address, "/ws/v2/sonar/audio",
|
|
"u-jagd-audio-v2", cookie)
|
|
with old:
|
|
assert status == b"HTTP/1.1 101 Switching Protocols"
|
|
# Every pending block delivered: the old worker now idles.
|
|
sequences = [struct.unpack("<Q", payload[4:12])[0] for _, payload in
|
|
_frames(old, buffered, transport.SONAR_AUDIO_RESUME_BLOCKS)]
|
|
assert sequences == list(range(1, transport.SONAR_AUDIO_RESUME_BLOCKS + 1))
|
|
new, status, buffered = _upgrade(
|
|
server.address, f"/ws/v2/sonar/audio?after={sequences[-1]}",
|
|
"u-jagd-audio-v2", cookie)
|
|
with new:
|
|
assert status == b"HTTP/1.1 101 Switching Protocols"
|
|
# The superseded worker closes; the new one carries the stream on.
|
|
assert _closed_by_server(old)
|
|
assert server.publish_sonar_audio(
|
|
bytes([99]) * transport.SONAR_AUDIO_BYTES, world_session="audio-world",
|
|
world_epoch=1, station_generation=generation)
|
|
((opcode, payload),) = _frames(new, buffered, 1)
|
|
assert opcode == 2 and struct.unpack("<Q", payload[4:12])[0] == sequences[-1] + 1
|
|
assert server.audio_stream_stats()["sonar"]["connections"] == 2
|
|
|
|
|
|
def test_sonar_stream_reconnect_supersedes_the_clients_idle_stream(server):
|
|
_publish_sonar(server)
|
|
_, cookie, _, paired = pair_v2(server, "Sonar supersede")
|
|
assert server.grant_station(paired["client_id"], "sonar")
|
|
old, status, buffered = _upgrade(server.address, transport.SONAR_STREAM_ROUTE,
|
|
"u-jagd-sonar-v2", cookie)
|
|
with old:
|
|
assert status == b"HTTP/1.1 101 Switching Protocols"
|
|
assert _frames(old, buffered, 1)[0][1][:4] == b"UJS2"
|
|
new, status, buffered = _upgrade(server.address, transport.SONAR_STREAM_ROUTE,
|
|
"u-jagd-sonar-v2", cookie)
|
|
with new:
|
|
assert status == b"HTTP/1.1 101 Switching Protocols"
|
|
assert _frames(new, buffered, 1)[0][1][:4] == b"UJS2"
|
|
assert _closed_by_server(old)
|