Bounded deque(maxlen) drop-oldest so a disconnect-without-cancel can't grow the per-turn offline replay buffer unbounded (per-abandoned-turn OOM risk, #4633 family). Dropped frames recovered via run-journal last_event_id replay path. Deep-review gate: Codex SAFE (journal-recovery verified end-to-end), 20 own + 1106 regression tests green. Crown-jewel streaming path. Co-authored-by: t <a@b> Co-authored-by: ai-ag2026 <ai-ag2026@users.noreply.github.com>
53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
from api.config import STREAMS, STREAMS_LOCK, create_stream_channel
|
|
from api.routes import _stream_runtime_diagnostics
|
|
|
|
|
|
def test_stream_channel_exposes_buffer_and_subscriber_counts():
|
|
channel = create_stream_channel()
|
|
channel.put_nowait(("token", {"text": "offline"}))
|
|
|
|
assert channel.diagnostic_snapshot() == {
|
|
"subscriber_count": 0,
|
|
"offline_buffered_events": 1,
|
|
"offline_dropped_events": 0,
|
|
}
|
|
|
|
subscriber = channel.subscribe()
|
|
try:
|
|
snapshot = channel.diagnostic_snapshot()
|
|
assert snapshot["subscriber_count"] == 1
|
|
assert snapshot["offline_buffered_events"] == 1
|
|
assert subscriber.get_nowait()[0] == "token"
|
|
finally:
|
|
channel.unsubscribe(subscriber)
|
|
|
|
|
|
def test_stream_runtime_diagnostics_summarizes_active_stream_channels():
|
|
channel = create_stream_channel()
|
|
channel.put_nowait(("token", {"text": "offline"}))
|
|
subscriber = channel.subscribe()
|
|
try:
|
|
with STREAMS_LOCK:
|
|
previous = dict(STREAMS)
|
|
STREAMS.clear()
|
|
STREAMS["stream-one"] = channel
|
|
try:
|
|
payload = _stream_runtime_diagnostics()
|
|
finally:
|
|
with STREAMS_LOCK:
|
|
STREAMS.clear()
|
|
STREAMS.update(previous)
|
|
|
|
assert payload["active_streams"] == 1
|
|
assert payload["total_subscribers"] == 1
|
|
assert payload["total_offline_buffered_events"] == 1
|
|
assert payload["streams"] == [
|
|
{
|
|
"stream_id": "stream-one",
|
|
"subscriber_count": 1,
|
|
"offline_buffered_events": 1,
|
|
}
|
|
]
|
|
finally:
|
|
channel.unsubscribe(subscriber)
|