feat: bound concurrent recap time logging (Closes #607)
This commit is contained in:
1 parent
bf2cf37151
commit
a7ec4437d9
2 files changed
+118
-52
No files matched your search
@@ -1,5 +1,7 @@
|
||||
import asyncio
|
||||
import json
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
@@ -250,15 +252,76 @@ async def test_recap_time_logging_journals_confirmed_ambiguous_and_retryable_out
|
||||
{"identity": "issue:stackchain/dashboard:591:", "status": "retry"},
|
||||
]
|
||||
time_events = [event for event in events if event["kind"] == "gitea_time_logged"]
|
||||
assert [(event["target"], event["status"]) for event in time_events] == [
|
||||
assert {(event["target"], event["status"]) for event in time_events} == {
|
||||
("stackchain/dashboard#590", "pending"),
|
||||
("stackchain/dashboard#589", "completed"),
|
||||
]
|
||||
}
|
||||
persisted = security_db.read_bytes()
|
||||
assert b"private-recap-session" not in persisted
|
||||
assert challenge.json()["detail"]["target"].encode() not in persisted
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_recap_time_logging_attempts_multiple_entries_with_bounded_concurrency(
|
||||
monkeypatch, tmp_path
|
||||
):
|
||||
monkeypatch.setenv("STACKCHAIN_DASHBOARD_AUTH_MODE", "operator")
|
||||
monkeypatch.setenv("STACKCHAIN_DASHBOARD_ACCESS_TOKEN", "correct horse battery staple")
|
||||
monkeypatch.setenv("STACKCHAIN_DASHBOARD_SESSION_SECRET", "a-separate-session-signing-secret-with-enough-entropy")
|
||||
monkeypatch.setenv("STACKCHAIN_SESSION_DB", str(tmp_path / "sessions.sqlite3"))
|
||||
monkeypatch.setenv("STACKCHAIN_LOGIN_ATTEMPT_DB", str(tmp_path / "login.sqlite3"))
|
||||
monkeypatch.setenv("STACKCHAIN_TODAY_DB", str(tmp_path / "today.sqlite3"))
|
||||
monkeypatch.setenv("STACKCHAIN_SECURITY_EVENT_DB", str(tmp_path / "security.sqlite3"))
|
||||
active = 0
|
||||
peak = 0
|
||||
attempted = []
|
||||
|
||||
async def user():
|
||||
return {"id": 1, "login": "Timmy"}
|
||||
|
||||
async def log_time(identity, _seconds):
|
||||
nonlocal active, peak
|
||||
active += 1
|
||||
peak = max(peak, active)
|
||||
attempted.append(identity)
|
||||
try:
|
||||
await asyncio.sleep(0.1)
|
||||
finally:
|
||||
active -= 1
|
||||
|
||||
monkeypatch.setattr(main, "current_user", user)
|
||||
monkeypatch.setattr(main.gitea_proxy, "log_issue_time", log_time)
|
||||
identities = [f"issue:stackchain/dashboard:{number}:" for number in range(607, 612)]
|
||||
payload = {
|
||||
"session_id": "concurrent-recap-session",
|
||||
"items": [
|
||||
{"identity": identity, "estimate_minutes": 10, "actual_minutes": 11}
|
||||
for identity in identities
|
||||
],
|
||||
"log_identities": identities,
|
||||
}
|
||||
transport = httpx.ASGITransport(app=main.app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url="https://test") as client:
|
||||
await client.post("/api/v1/session", json={"access_token": "correct horse battery staple"})
|
||||
headers = {"Origin": "https://test", "X-CSRF-Token": client.cookies["stackchain_csrf"]}
|
||||
grant = await recap_time_grant(client, headers, payload)
|
||||
started = time.perf_counter()
|
||||
response = await client.post(
|
||||
"/api/v1/today/recaps/log-time",
|
||||
json=payload,
|
||||
headers={**headers, "X-Step-Up-Grant": grant},
|
||||
)
|
||||
elapsed = time.perf_counter() - started
|
||||
|
||||
assert response.status_code == 200
|
||||
assert elapsed < 0.35
|
||||
assert 1 < peak <= 3
|
||||
assert set(attempted) == set(identities)
|
||||
assert response.json()["time_logs"] == [
|
||||
{"identity": identity, "status": "logged"} for identity in identities
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_explicit_recap_time_logging_posts_corrected_seconds_once(monkeypatch, tmp_path):
|
||||
monkeypatch.setenv("STACKCHAIN_DASHBOARD_AUTH_MODE", "operator")
|
||||
|
||||
Reference in new issue
Block a user