fix(remote): measure the idle time of a remote session on a monotonic clock

This commit is contained in:
imSp4rky
2026-11-30 17:06:15 -07:00
parent 34a3820eb8
commit e813737506
6 changed files with 59 additions and 31 deletions
+7 -7
View File
@@ -7,7 +7,6 @@ import json
import logging
import threading
from collections.abc import Callable, Iterator
from datetime import UTC, datetime
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from threading import Event, Semaphore, Thread
@@ -160,6 +159,11 @@ def server(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch, tmp_
app = web.Application(middlewares=[record, cors_middleware, no_5xx_middleware, api_key_authentication])
app["config"] = {"users": [KEY, OTHER_KEY]}
setup_routes(app, remote_only=True)
async def close_store(_: web.Application) -> None:
await session_store.get_session_store().close()
app.on_cleanup.append(close_store)
loop = asyncio.new_event_loop()
runner = web.AppRunner(app)
loop.run_until_complete(runner.setup())
@@ -220,10 +224,6 @@ def example_messages(caplog: pytest.LogCaptureFixture) -> list[str]:
return [r.getMessage() for r in caplog.records if r.name == "EXAMPLE"]
def idle_seconds(entry: Any) -> float:
return (datetime.now(UTC) - entry.last_accessed).total_seconds()
@pytest.mark.parametrize(
"server,channel",
[(True, "none"), (False, "refused"), (True, "failed")],
@@ -496,7 +496,7 @@ def test_an_open_channel_keeps_the_session_alive(server: SimpleNamespace, monkey
session_id = service._session_id
entry = store.peek(session_id)
assert wait_until(lambda: entry.channel.attached, 5)
assert wait_until(lambda: idle_seconds(entry) > 3 * ttl, 10)
assert wait_until(lambda: entry.idle_seconds > 3 * ttl, 10)
assert on_loop(server, store.cleanup_expired()) == 0
assert store.peek(session_id) is entry
assert count(server, "GET", SESSION_ROUTE, session_id) == 0
@@ -504,7 +504,7 @@ def test_an_open_channel_keeps_the_session_alive(server: SimpleNamespace, monkey
service.lifetime.close_channel()
assert wait_until(lambda: not entry.channel.attached, 5)
assert wait_until(lambda: idle_seconds(entry) > 2 * ttl, 10)
assert wait_until(lambda: entry.idle_seconds > 2 * ttl, 10)
assert on_loop(server, store.cleanup_expired()) == 1
assert store.peek(session_id) is None
assert count(server, "GET", SESSION_ROUTE, session_id) == 0