Prepared for playback engine
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import socket
|
||||
import time
|
||||
@@ -7,6 +8,7 @@ import pytest
|
||||
from dynalab_core import Core
|
||||
from dynalab_core.config import CoreConfig
|
||||
from dynalab_core.protocols.constants import PROTOCOL_VERSION
|
||||
from dynalab_core.protocols.common import VersionDescriptor
|
||||
from dynalab_core.protocols.json.errors import (
|
||||
JsonServerStartupError,
|
||||
JsonServerTimeoutError,
|
||||
@@ -91,7 +93,7 @@ def test_json_server_logs_invalid_handshake(
|
||||
with socket.create_connection(("127.0.0.1", port), timeout=1) as peer:
|
||||
server_hello = peer.makefile("rb").readline()
|
||||
assert server_hello
|
||||
peer.sendall(b"not-json\n")
|
||||
peer.sendall(b'{"secret":"must-not-appear-in-logs"}\n')
|
||||
|
||||
rejection = _wait_for_event(caplog, "connector.handshake_rejected")
|
||||
finally:
|
||||
@@ -101,6 +103,69 @@ def test_json_server_logs_invalid_handshake(
|
||||
assert rejection.reason == "invalid_frame"
|
||||
assert rejection.connection_id
|
||||
assert rejection.peer_address
|
||||
assert "must-not-appear-in-logs" not in caplog.text
|
||||
|
||||
|
||||
def test_json_server_reports_unexpected_thread_startup_failure(
|
||||
caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
async def fail_start_server(*args: object, **kwargs: object) -> None:
|
||||
raise RuntimeError("unexpected startup failure")
|
||||
|
||||
monkeypatch.setattr(asyncio, "start_server", fail_start_server)
|
||||
core = Core(CoreConfig(port=find_available_port(8765)))
|
||||
|
||||
with caplog.at_level(logging.DEBUG, logger="dynalab_core"):
|
||||
try:
|
||||
with pytest.raises(JsonServerStartupError):
|
||||
core.start()
|
||||
finally:
|
||||
core.stop()
|
||||
|
||||
failures = [
|
||||
record
|
||||
for record in caplog.records
|
||||
if getattr(record, "event", None) == "json_server.thread_failed"
|
||||
]
|
||||
assert len(failures) == 1
|
||||
assert failures[0].startup_complete is False
|
||||
assert failures[0].exception_type == "RuntimeError"
|
||||
assert failures[0].exc_info is not None
|
||||
|
||||
|
||||
def test_rejected_handshake_is_not_logged_as_connected(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
port = find_available_port(8765)
|
||||
core = Core(CoreConfig(port=port))
|
||||
connector_uuid = uuid4()
|
||||
connector_hello = ConnectorHello(
|
||||
connector_uuid=connector_uuid,
|
||||
protocol_version=VersionDescriptor(type="alpha", major=999, minor=0, patch=0),
|
||||
connector_name="Incompatible connector",
|
||||
connector_version="0.1.0-test",
|
||||
signals=[SignalDescriptor(id=uuid4(), name="Dummy signal", type="number")],
|
||||
)
|
||||
|
||||
with caplog.at_level(logging.DEBUG, logger="dynalab_core"):
|
||||
core.start()
|
||||
try:
|
||||
with socket.create_connection(("127.0.0.1", port), timeout=1) as peer:
|
||||
peer_file = peer.makefile("rb")
|
||||
assert peer_file.readline()
|
||||
peer.sendall(connector_hello.model_dump_json().encode("utf-8") + b"\n")
|
||||
assert b'"type":"handshake_rejected"' in peer_file.readline()
|
||||
rejection = _wait_for_event(caplog, "endpoint.handshake_rejected")
|
||||
finally:
|
||||
core.stop()
|
||||
|
||||
assert rejection.reason == "protocol_version_mismatch"
|
||||
assert rejection.connector_uuid == str(connector_uuid)
|
||||
assert not any(
|
||||
getattr(record, "event", None) == "connector.connected"
|
||||
and getattr(record, "connector_uuid", None) == str(connector_uuid)
|
||||
for record in caplog.records
|
||||
)
|
||||
|
||||
|
||||
def test_json_server_waits_for_connection_handlers_on_stop(
|
||||
@@ -133,6 +198,8 @@ def test_json_server_waits_for_connection_handlers_on_stop(
|
||||
|
||||
events = [getattr(record, "event", None) for record in caplog.records]
|
||||
assert "json_server.stop_timeout" not in events
|
||||
assert "connector.connected" in events
|
||||
assert events.index("connector.registered") < events.index("connector.connected")
|
||||
assert core._json_server._stopped_event.is_set()
|
||||
assert not core._json_server._handler_tasks
|
||||
assert core._connector_registry.get(connector_uuid) is None
|
||||
|
||||
Reference in New Issue
Block a user