Spaces:
Sleeping
Sleeping
github-actions[bot] commited on
Commit ยท
24ac5c8
1
Parent(s): 915f989
deploy: backend from a896c3d
Browse files
tests/test_regression_rag_lesson.py
CHANGED
|
@@ -408,27 +408,43 @@ def test_prefetch_slots_held_never_block_interactive_lessons(seams: LessonSeams)
|
|
| 408 |
|
| 409 |
def test_prefetch_concurrent_burst_never_queues_interactive_behind_the_semaphore(seams: LessonSeams, monkeypatch: pytest.MonkeyPatch) -> None:
|
| 410 |
"""A prefetch accepted while the last slot is being taken must not leave a job waiting on the semaphore,
|
| 411 |
-
because an interactive open of that lesson joins the job and would wait behind other students' prefetches.
|
|
|
|
|
|
|
|
|
|
| 412 |
monkeypatch.setattr(rag_routes, "_prefetch_semaphore", asyncio.Semaphore(1))
|
| 413 |
-
|
|
|
|
|
|
|
| 414 |
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
)
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
|
| 433 |
|
| 434 |
# โโโ 13. Foolproof lesson guards โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
| 408 |
|
| 409 |
def test_prefetch_concurrent_burst_never_queues_interactive_behind_the_semaphore(seams: LessonSeams, monkeypatch: pytest.MonkeyPatch) -> None:
|
| 410 |
"""A prefetch accepted while the last slot is being taken must not leave a job waiting on the semaphore,
|
| 411 |
+
because an interactive open of that lesson joins the job and would wait behind other students' prefetches.
|
| 412 |
+
|
| 413 |
+
Alice's prefetch generation stays blocked until bob's interactive lesson is back, so bob's lesson finishes only if
|
| 414 |
+
it neither waits on the semaphore nor joins alice's job. The only time limit is a 10 s timeout on a ~0.1 s request."""
|
| 415 |
monkeypatch.setattr(rag_routes, "_prefetch_semaphore", asyncio.Semaphore(1))
|
| 416 |
+
# Each profile names its student, so the fake model can tell whose lesson it is writing.
|
| 417 |
+
monkeypatch.setattr(rag_routes, "_load_learner_profile", lambda uid, _payload: f"Student: {uid}")
|
| 418 |
+
alice_generating, release_alice = threading.Event(), threading.Event()
|
| 419 |
|
| 420 |
+
def stream_holding_alice(
|
| 421 |
+
messages: List[Dict[str, str]], model: str, report_progress: Callable[[str, int], None], stop: threading.Event
|
| 422 |
+
) -> str:
|
| 423 |
+
if "Student: alice" in str(messages):
|
| 424 |
+
alice_generating.set()
|
| 425 |
+
release_alice.wait(timeout=60)
|
| 426 |
+
return seams.stream(messages, model, report_progress, stop)
|
| 427 |
+
|
| 428 |
+
monkeypatch.setattr(rag_routes, "_stream_reasoner_lesson", stream_holding_alice)
|
| 429 |
+
|
| 430 |
+
async def scenario(http: httpx.AsyncClient) -> int:
|
| 431 |
+
try:
|
| 432 |
+
burst = await asyncio.gather(
|
| 433 |
+
*(http.post("/api/rag/lesson/prefetch", json=PAYLOAD, headers=_as(uid)) for uid in ("alice", "bob"))
|
| 434 |
+
)
|
| 435 |
+
# gather starts alice's request first, so alice's prefetch takes the only slot.
|
| 436 |
+
assert [response.json() for response in burst] == [{"status": "queued"}, {"status": "skipped"}]
|
| 437 |
+
assert await asyncio.to_thread(alice_generating.wait, 10), "alice's prefetch never started generating"
|
| 438 |
+
try:
|
| 439 |
+
bob_open = await asyncio.wait_for(http.post("/api/rag/lesson", json=PAYLOAD, headers=_as("bob")), timeout=10)
|
| 440 |
+
except TimeoutError:
|
| 441 |
+
pytest.fail("bob's interactive lesson waited 10s behind alice's prefetch (its semaphore slot or its job)")
|
| 442 |
+
finally:
|
| 443 |
+
release_alice.set()
|
| 444 |
+
await _settle(seams, 2)
|
| 445 |
+
return bob_open.status_code
|
| 446 |
+
|
| 447 |
+
assert _run(scenario) == 200
|
| 448 |
|
| 449 |
|
| 450 |
# โโโ 13. Foolproof lesson guards โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|