left4me/l4d2web/tests/test_status_and_server_logs.py
mwiegand 4552af6544
fix(l4d2-web): keep SSE log stream from pinning gunicorn threads
stream_command used a blocking proc.stdout.readline() that never woke
when the underlying journalctl was silent, so Flask never delivered
GeneratorExit on client disconnect — the worker thread and the journalctl
child both leaked permanently and pinned the gunicorn thread pool.

Switch to a select-based read loop with a 15s heartbeat tick (yielded as
""), and translate the tick to an SSE keepalive comment in the log route.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 11:18:56 +02:00

71 lines
2.3 KiB
Python

import pytest
from l4d2web.app import create_app
from l4d2web.auth import hash_password
from l4d2web.db import init_db, session_scope
from l4d2web.models import Blueprint, Server, User
@pytest.fixture
def owner_client_with_server(tmp_path, monkeypatch):
db_url = f"sqlite:///{tmp_path/'status.db'}"
monkeypatch.setenv("DATABASE_URL", db_url)
app = create_app({"TESTING": True, "DATABASE_URL": db_url, "SECRET_KEY": "test"})
init_db()
with session_scope() as session:
user = User(username="alice", password_digest=hash_password("secret"), admin=False)
session.add(user)
session.flush()
blueprint = Blueprint(user_id=user.id, name="default", arguments="[]", config="[]")
session.add(blueprint)
session.flush()
server = Server(user_id=user.id, blueprint_id=blueprint.id, name="alpha", port=27015)
session.add(server)
session.flush()
user_id = user.id
server_id = server.id
client = app.test_client()
with client.session_transaction() as sess:
sess["user_id"] = user_id
return client, server_id
def test_owner_can_stream_server_logs(owner_client_with_server, monkeypatch) -> None:
client, server_id = owner_client_with_server
monkeypatch.setattr(
"l4d2web.services.l4d2_facade.stream_server_logs",
lambda name, lines=200, follow=True: iter(["first", "second"]),
)
response = client.get(f"/servers/{server_id}/logs/stream")
assert response.status_code == 200
def test_log_stream_translates_heartbeat_to_sse_keepalive(owner_client_with_server, monkeypatch) -> None:
client, server_id = owner_client_with_server
monkeypatch.setattr(
"l4d2web.services.l4d2_facade.stream_server_logs",
lambda name, lines=200, follow=True: iter(["first", "", "second"]),
)
response = client.get(f"/servers/{server_id}/logs/stream")
assert response.status_code == 200
body = response.get_data(as_text=True)
assert "data: first\n\n" in body
assert "data: second\n\n" in body
assert ": keepalive\n\n" in body
assert "data: \n\n" not in body
def test_status_precedence() -> None:
from l4d2web.services.status import compute_display_state
assert compute_display_state("start", "stopped") == "starting"