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
+23 -6
View File
@@ -50,10 +50,12 @@ async def test_get_returns_none_for_missing(store: SessionStore) -> None:
async def test_get_touches_last_accessed(store: SessionStore) -> None:
entry = await store.create("DEMO", _FakeService())
before = entry.last_accessed
entry.idle_since -= 100
await asyncio.sleep(0.01)
fetched = await store.get(entry.session_id)
assert fetched is entry
assert fetched.last_accessed > before
assert fetched.idle_seconds < 100
async def test_delete_removes_and_cancels_bridge(store: SessionStore) -> None:
@@ -99,7 +101,7 @@ async def test_get_and_the_sweep_share_one_expiry_rule(
bridge = InputBridge()
bridge.login_status = status
entry = await store.create("EXAMPLE", _FakeService(), session_id="s", input_bridge=bridge)
entry.last_accessed = datetime.now(UTC) - timedelta(seconds=idle)
entry.idle_since = time.monotonic() - idle
if via == "get":
assert (await store.get("s") is None) is expires
@@ -108,10 +110,25 @@ async def test_get_and_the_sweep_share_one_expiry_rule(
assert store.session_count == int(not expires)
@pytest.mark.parametrize("step", [1, -1], ids=["forward", "back"])
@pytest.mark.parametrize("idle", [0, 1], ids=["active", "idle"])
async def test_a_step_of_the_wall_clock_changes_no_expiry(
store: SessionStore, monkeypatch: pytest.MonkeyPatch, step: int, idle: int
) -> None:
entry = await store.create("EXAMPLE", _FakeService(), session_id="s")
entry.idle_since -= idle * (store.ttl + 100)
stepped = datetime.now(UTC) + timedelta(seconds=step * 2 * (store.ttl + 100))
monkeypatch.setattr(session_store_module, "datetime", SimpleNamespace(now=lambda tz: stepped))
assert entry.summary()["idle_seconds"] // (store.ttl + 100) == idle
assert await store.cleanup_expired() == idle
assert await store.get("s") is (None if idle else entry)
async def test_session_with_work_in_progress_does_not_expire(store: SessionStore) -> None:
"""A request that holds the lock for longer than the TTL keeps its session and cache directory."""
entry = await store.create("EXAMPLE", _FakeService(), session_id="busy")
entry.last_accessed = datetime.now(UTC) - timedelta(seconds=store.ttl + 100)
entry.idle_since = time.monotonic() - store.ttl - 100
async with entry.lock:
assert await store.cleanup_expired() == 0
assert await store.get("busy") is entry
@@ -204,7 +221,7 @@ async def test_get_expiry_removes_cache_dir(store: SessionStore, monkeypatch: py
monkeypatch.setattr(SessionStore, "cleanup_cache_dir", staticmethod(removed.append))
entry = await store.create("EXAMPLE", _FakeService())
entry.cache_tag = "_sessions/k/s/EXAMPLE"
entry.last_accessed = datetime.now(UTC) - timedelta(seconds=store.ttl + 100)
entry.idle_since = time.monotonic() - store.ttl - 100
assert await store.get(entry.session_id) is None
assert removed == ["_sessions/k/s/EXAMPLE"]
@@ -285,7 +302,7 @@ async def test_inline_expiry_applies_staged_service_reload(
applied = _stage_reload(monkeypatch, {"EXAMPLE"})
entry = await store.create("EXAMPLE", _FakeService())
entry.last_accessed = datetime.now(UTC) - timedelta(seconds=store.ttl + 100)
entry.idle_since = time.monotonic() - store.ttl - 100
assert await store.get(entry.session_id) is None
await asyncio.sleep(0.05)
assert applied == [set()]
@@ -296,9 +313,9 @@ async def test_cleanup_expired_applies_staged_service_reload_once(
) -> None:
"""A cleanup sweep that drops several sessions on one tag schedules one reload for that tag."""
applied = _stage_reload(monkeypatch, {"EXAMPLE"})
stale = datetime.now(UTC) - timedelta(seconds=store.ttl + 100)
stale = time.monotonic() - store.ttl - 100
for sid in ("a", "b"):
(await store.create("EXAMPLE", _FakeService(), session_id=sid)).last_accessed = stale
(await store.create("EXAMPLE", _FakeService(), session_id=sid)).idle_since = stale
assert await store.cleanup_expired() == 2
await asyncio.sleep(0.05)