258 lines
9.5 KiB
Python
258 lines
9.5 KiB
Python
"""Native RRD empty reads are pauses, not peer EOF. No device/network involved."""
|
|
import time
|
|
|
|
from k1link.data_plane import ConsumerFrameContext, DecodedPointCloudView
|
|
from k1link.viewer.node_rerun import NodeRerunBridge, RrdSubscriber
|
|
from k1link.viewer.rerun_bridge import RerunSceneSettings
|
|
|
|
|
|
def points(sequence=1, age=0):
|
|
now = time.monotonic_ns()
|
|
return DecodedPointCloudView(
|
|
context=ConsumerFrameContext(sequence, time.time_ns(), now - int(age * 1e9), now, 1, True),
|
|
frame_id="world", positions_xyz=((1.0, 2.0, 3.0),), intensities=b"\x01",
|
|
)
|
|
|
|
|
|
def drain_until(subscriber, predicate, seconds=3):
|
|
deadline = time.monotonic() + seconds
|
|
payloads = []
|
|
while time.monotonic() < deadline:
|
|
payload = subscriber.read()
|
|
assert payload is not None, "idle native binary read retired the live subscriber"
|
|
if payload:
|
|
assert payload.startswith(b"RRF2")
|
|
payloads.append(payload)
|
|
if predicate():
|
|
return payloads
|
|
raise AssertionError("native subscriber did not make progress")
|
|
|
|
|
|
def test_native_idle_read_keeps_subscriber_alive_and_resumes():
|
|
subscriber = RrdSubscriber(RerunSceneSettings)
|
|
try:
|
|
assert drain_until(subscriber, lambda: subscriber.output.empty())
|
|
# This forces several SDK reads with no data; the previous len(None)
|
|
# retired both RRD and camera before a first point could arrive.
|
|
assert subscriber.read() == b""
|
|
assert not subscriber.closed.is_set()
|
|
subscriber.offer(points())
|
|
assert drain_until(subscriber, lambda: subscriber.snapshot()["sequence"] == 1)
|
|
assert subscriber.read() == b""
|
|
subscriber.offer(points(2))
|
|
assert drain_until(subscriber, lambda: subscriber.snapshot()["sequence"] == 2)
|
|
assert subscriber.snapshot()["points"] == 1
|
|
assert not subscriber.closed.is_set()
|
|
finally:
|
|
subscriber.close()
|
|
subscriber.thread.join(timeout=3)
|
|
assert not subscriber.thread.is_alive()
|
|
|
|
|
|
def test_subscriber_does_not_publish_queued_stale_points():
|
|
subscriber = RrdSubscriber(RerunSceneSettings)
|
|
try:
|
|
drain_until(subscriber, lambda: subscriber.output.empty())
|
|
subscriber.offer(points(age=5))
|
|
assert subscriber.read() == b""
|
|
assert subscriber.snapshot()["sequence"] == 0
|
|
subscriber.offer(points(2))
|
|
drain_until(subscriber, lambda: subscriber.snapshot()["sequence"] == 2)
|
|
finally:
|
|
subscriber.close()
|
|
subscriber.thread.join(timeout=3)
|
|
|
|
|
|
def test_reopened_viewer_does_not_replay_cached_points_after_source_pause(monkeypatch):
|
|
bridge = NodeRerunBridge()
|
|
try:
|
|
bridge.process(points())
|
|
cached_at, frame = next(iter(bridge.latest.values()))
|
|
bridge.latest[type(frame)] = (cached_at - 5, frame)
|
|
sub = bridge.subscribe()
|
|
drain_until(sub, lambda: sub.output.empty())
|
|
assert sub.read() == b""
|
|
assert sub.snapshot()["sequence"] == 0
|
|
bridge.process(points(2))
|
|
drain_until(sub, lambda: sub.snapshot()["sequence"] == 2)
|
|
finally:
|
|
bridge.close()
|
|
sub.thread.join(timeout=3)
|
|
|
|
|
|
def test_slow_consumer_resumes_same_recording_and_replays_unacknowledged_batch():
|
|
"""A >500ms delivery pause used to retire the recording and lose its route."""
|
|
bridge = NodeRerunBridge()
|
|
sub = bridge.subscribe("synthetic-view")
|
|
try:
|
|
batch = sub.next_batch()
|
|
assert batch and batch[1].startswith(b"RRF2")
|
|
sequence = batch[0]
|
|
# Produce several tiny frames without draining the bounded outbox.
|
|
for index in range(1, 8):
|
|
bridge.process(points(index))
|
|
time.sleep(0.12)
|
|
assert not sub.closed.is_set()
|
|
assert sub.output.qsize() <= 2
|
|
old_thread = sub.thread
|
|
sub.release()
|
|
resumed = bridge.subscribe("synthetic-view", sequence - 1)
|
|
assert resumed is sub and resumed.thread is old_thread
|
|
assert resumed.next_batch() == batch # ACK lost: resend the exact RRD.
|
|
resumed.acknowledge(sequence)
|
|
following = resumed.next_batch()
|
|
assert following[0] == sequence + 1
|
|
resumed.release()
|
|
again = bridge.subscribe("synthetic-view", following[0])
|
|
assert again is sub and again.pending is None # Delivered ACK lost at sender.
|
|
again.release()
|
|
finally:
|
|
bridge.close()
|
|
sub.thread.join(timeout=3)
|
|
assert not sub.thread.is_alive()
|
|
|
|
|
|
def test_resumption_cannot_silently_replace_expired_recording():
|
|
import pytest
|
|
bridge = NodeRerunBridge()
|
|
try:
|
|
with pytest.raises(ValueError, match="expired"):
|
|
bridge.subscribe("missing-view", 2)
|
|
assert bridge.subscribers == []
|
|
finally:
|
|
bridge.close()
|
|
|
|
|
|
def test_closing_view_releases_capacity_without_waiting_for_resume_grace():
|
|
bridge = NodeRerunBridge()
|
|
views = []
|
|
try:
|
|
for index in range(4):
|
|
sub = bridge.subscribe(f"view-{index}")
|
|
views.append(sub)
|
|
sub.release()
|
|
bridge.release_view(f"view-{index}")
|
|
assert sub.closed.is_set()
|
|
assert bridge.views == {}
|
|
finally:
|
|
bridge.close()
|
|
for sub in views:
|
|
sub.thread.join(timeout=3)
|
|
|
|
|
|
def test_reloading_page_reuses_active_capacity_without_resetting_live_view():
|
|
"""Unloaded pages cannot send retire_view; their peers only detach later."""
|
|
import pytest
|
|
|
|
from k1link.viewer.node_rerun import PreviewResumeError
|
|
|
|
bridge = NodeRerunBridge()
|
|
views = []
|
|
try:
|
|
live = bridge.subscribe("other-live-window")
|
|
views.append(live)
|
|
live_batch = live.next_batch()
|
|
for index in range(6):
|
|
sub = bridge.subscribe(f"page-{index}")
|
|
views.append(sub)
|
|
batch = sub.next_batch()
|
|
assert batch and batch[1].startswith(b"RRF2")
|
|
sub.release() # Transport lost; no explicit viewport retirement.
|
|
# Two detached cursors plus the active window remain. Re-entering a
|
|
# recent view keeps its exact native recording and unacknowledged RRD.
|
|
resumed = bridge.subscribe("page-5", batch[0] - 1)
|
|
assert resumed is sub and resumed.next_batch() == batch
|
|
assert live.next_batch() == live_batch
|
|
assert len(bridge.subscribers) <= 4
|
|
with pytest.raises(RuntimeError, match="another live viewer"):
|
|
bridge.subscribe("third-active-window")
|
|
resumed.release()
|
|
with pytest.raises(PreviewResumeError, match="expired"):
|
|
bridge.subscribe("page-0", 1)
|
|
assert "page-0" not in bridge.views
|
|
finally:
|
|
bridge.close()
|
|
for sub in views:
|
|
sub.thread.join(timeout=3)
|
|
assert all(not sub.thread.is_alive() for sub in views)
|
|
|
|
|
|
def test_resuming_detached_view_cannot_exceed_active_limit():
|
|
import pytest
|
|
bridge = NodeRerunBridge()
|
|
views = []
|
|
try:
|
|
detached = bridge.subscribe("detached")
|
|
views.append(detached)
|
|
batch = detached.next_batch()
|
|
detached.release()
|
|
for name in ("active-a", "active-b"):
|
|
views.append(bridge.subscribe(name))
|
|
with pytest.raises(RuntimeError, match="another live viewer"):
|
|
bridge.subscribe("detached", batch[0] - 1)
|
|
assert not detached.delivery_lock.locked()
|
|
assert detached.next_batch() == batch
|
|
views[-1].release()
|
|
assert bridge.subscribe("detached", batch[0] - 1) is detached
|
|
detached.release()
|
|
finally:
|
|
bridge.close()
|
|
for sub in views:
|
|
sub.thread.join(timeout=3)
|
|
|
|
|
|
def test_budgeted_preview_coalesces_until_ack_without_losing_native_recording():
|
|
class Budget:
|
|
bps = 64000
|
|
|
|
bridge = NodeRerunBridge()
|
|
sub = bridge.subscribe('synthetic-budgeted-view', budget=Budget())
|
|
try:
|
|
first = sub.next_batch()
|
|
assert first and first[1].startswith(b'RRF2')
|
|
for sequence in range(1, 21):
|
|
bridge.process(points(sequence))
|
|
time.sleep(0.12)
|
|
assert sub.pending is first
|
|
assert sub.output.empty()
|
|
assert sub.snapshot()['sequence'] == 0
|
|
sub.acknowledge(first[0])
|
|
drain_until(sub, lambda: sub.snapshot()['sequence'] == 20)
|
|
assert sub.snapshot()['points'] == 1
|
|
assert not sub.closed.is_set()
|
|
finally:
|
|
bridge.close()
|
|
sub.thread.join(timeout=3)
|
|
|
|
|
|
def test_slow_cloud_keeps_full_spatial_frame_and_reports_source_demand():
|
|
from dataclasses import replace
|
|
|
|
from missioncore_plugin_sdk.media_budget import MediaBudget
|
|
|
|
budget = MediaBudget("cloud")
|
|
budget.update({
|
|
"schema": "missioncore.media-budget/v1", "allocation_bps": 4000, "ttl_ms": 3000,
|
|
})
|
|
bridge = NodeRerunBridge()
|
|
sub = bridge.subscribe('full-detail', budget=budget)
|
|
try:
|
|
initial = sub.next_batch()
|
|
first = points(1, age=0.1)
|
|
positions = tuple((i * .01, i % 10 * .2, i % 30 * .03) for i in range(4000))
|
|
first = replace(first, positions_xyz=positions, intensities=b'\x01' * 4000)
|
|
bridge.process(first)
|
|
latest = replace(first, context=points(2).context)
|
|
bridge.process(latest)
|
|
assert sub.pending == initial and sub.output.empty()
|
|
assert budget.demand_bps > 256000
|
|
sub.acknowledge(initial[0])
|
|
drain_until(sub, lambda: sub.snapshot()['sequence'] == 2)
|
|
assert sub.snapshot()['points'] == latest.point_count == 4000
|
|
assert first.positions_xyz == positions
|
|
assert not sub.closed.is_set()
|
|
finally:
|
|
bridge.close()
|
|
sub.thread.join(timeout=3)
|
|
budget.connection.close()
|