54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""In-memory transport double for Phase-1.0W policy tests only."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
|
|
class FakeTransportError(RuntimeError):
|
|
"""Synthetic sequence error."""
|
|
|
|
|
|
class FakeTransport:
|
|
"""Record one synthetic session without any network primitives."""
|
|
|
|
def __init__(self, inbound_chunks: list[bytes]) -> None:
|
|
self._inbound = list(inbound_chunks)
|
|
self._plan: Any = None
|
|
self._command_index = 0
|
|
self._opened_once = False
|
|
self._closed = False
|
|
self.events: list[str] = []
|
|
|
|
def open_once(self, plan: Any) -> None:
|
|
if self._opened_once:
|
|
raise FakeTransportError("fake transport already opened")
|
|
self._opened_once = True
|
|
self._plan = plan
|
|
self.events.append("OPEN")
|
|
|
|
def send_command_token(self, command: str) -> None:
|
|
if not self._opened_once or self._closed or self._plan is None:
|
|
raise FakeTransportError("fake transport is not open")
|
|
if self._command_index >= len(self._plan.commands) or \
|
|
command != self._plan.commands[self._command_index]:
|
|
raise FakeTransportError("unexpected synthetic command")
|
|
self._command_index += 1
|
|
self.events.append(f"COMMAND_{command.upper()}")
|
|
|
|
def receive_chunk(self) -> bytes | None:
|
|
if not self._opened_once or self._closed:
|
|
raise FakeTransportError("fake transport is not open")
|
|
if not self._inbound:
|
|
return None
|
|
self.events.append("RECEIVE")
|
|
return self._inbound.pop(0)
|
|
|
|
def close_once(self) -> None:
|
|
if not self._opened_once or self._closed:
|
|
raise FakeTransportError("fake transport cannot close")
|
|
self._closed = True
|
|
self.events.append("CLOSE")
|