[fix] restart gap
This commit is contained in:
parent
770af1fe76
commit
50af62b3fb
10 changed files with 348 additions and 4 deletions
|
|
@ -11,7 +11,7 @@ from docker.types import Mount
|
|||
from adapter.config.model import SandboxConfig
|
||||
from adapter.docker.runtime import DockerSandboxRuntime
|
||||
from domain.error import SandboxError, SandboxStartError
|
||||
from domain.sandbox import SandboxStatus
|
||||
from domain.sandbox import SandboxSession, SandboxStatus
|
||||
|
||||
CHAT_ID = UUID('123e4567-e89b-12d3-a456-426614174000')
|
||||
NON_CANONICAL_CHAT_ID = '123E4567E89B12D3A456426614174000'
|
||||
|
|
@ -27,6 +27,19 @@ class FakeContainer:
|
|||
self.stop_calls += 1
|
||||
|
||||
|
||||
class FakeListedContainer(FakeContainer):
|
||||
def __init__(
|
||||
self,
|
||||
container_id: str,
|
||||
*,
|
||||
labels: dict[str, str],
|
||||
created_at: str,
|
||||
) -> None:
|
||||
super().__init__(container_id)
|
||||
self.labels = labels
|
||||
self.attrs = {'Created': created_at}
|
||||
|
||||
|
||||
class RunKwargs(TypedDict):
|
||||
detach: bool
|
||||
labels: dict[str, str]
|
||||
|
|
@ -42,8 +55,10 @@ class FakeContainers:
|
|||
def __init__(self, run_result: FakeContainer | None = None) -> None:
|
||||
self.run_calls: list[RunCall] = []
|
||||
self.get_calls: list[str] = []
|
||||
self.list_calls: list[dict[str, object]] = []
|
||||
self.run_result = run_result or FakeContainer('container-123')
|
||||
self.get_result: FakeContainer | Exception | None = None
|
||||
self.list_result: list[object] = []
|
||||
|
||||
def run(
|
||||
self,
|
||||
|
|
@ -73,6 +88,10 @@ class FakeContainers:
|
|||
raise AssertionError('missing get result')
|
||||
return self.get_result
|
||||
|
||||
def list(self, *, filters: dict[str, list[str]]) -> list[object]:
|
||||
self.list_calls.append({'filters': filters})
|
||||
return self.list_result
|
||||
|
||||
|
||||
class FakeDockerClient(DockerClient):
|
||||
def __init__(self, containers: FakeContainers) -> None:
|
||||
|
|
@ -197,3 +216,47 @@ def test_runtime_stop_wraps_docker_errors(tmp_path: Path) -> None:
|
|||
runtime.stop('container-123')
|
||||
|
||||
assert str(excinfo.value) == 'sandbox_stop_failed'
|
||||
|
||||
|
||||
def test_runtime_list_active_sessions_reads_valid_labeled_containers(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
config = build_config(tmp_path)
|
||||
containers = FakeContainers()
|
||||
expires_at = datetime(2026, 4, 2, 12, 5, tzinfo=UTC)
|
||||
containers.list_result = [
|
||||
FakeListedContainer(
|
||||
'container-123',
|
||||
labels={
|
||||
'session_id': str(SESSION_ID),
|
||||
'chat_id': str(CHAT_ID),
|
||||
'expires_at': expires_at.isoformat(),
|
||||
},
|
||||
created_at='2026-04-02T12:00:00Z',
|
||||
),
|
||||
FakeListedContainer(
|
||||
'container-bad',
|
||||
labels={
|
||||
'chat_id': str(CHAT_ID),
|
||||
'expires_at': expires_at.isoformat(),
|
||||
},
|
||||
created_at='2026-04-02T12:01:00Z',
|
||||
),
|
||||
]
|
||||
runtime = DockerSandboxRuntime(config, FakeDockerClient(containers))
|
||||
|
||||
sessions = runtime.list_active_sessions()
|
||||
|
||||
assert sessions == [
|
||||
SandboxSession(
|
||||
session_id=SESSION_ID,
|
||||
chat_id=CHAT_ID,
|
||||
container_id='container-123',
|
||||
status=SandboxStatus.RUNNING,
|
||||
created_at=datetime(2026, 4, 2, 12, 0, tzinfo=UTC),
|
||||
expires_at=expires_at,
|
||||
)
|
||||
]
|
||||
assert containers.list_calls == [
|
||||
{'filters': {'label': ['session_id', 'chat_id', 'expires_at']}}
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue