github-actions[bot] commited on
Commit
24ac5c8
ยท
1 Parent(s): 915f989

deploy: backend from a896c3d

Browse files
Files changed (1) hide show
  1. tests/test_regression_rag_lesson.py +35 -19
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
- seams.stream_delay = 0.8
 
 
414
 
415
- async def scenario(http: httpx.AsyncClient) -> Tuple[List[Dict[str, object]], float, int]:
416
- burst = await asyncio.gather(
417
- *(http.post("/api/rag/lesson/prefetch", json=PAYLOAD, headers=_as(uid)) for uid in ("alice", "bob"))
418
- )
419
- started = time.perf_counter()
420
- bob_open = await http.post("/api/rag/lesson", json=PAYLOAD, headers=_as("bob"))
421
- elapsed = time.perf_counter() - started
422
- await asyncio.sleep(0.9)
423
- return [r.json() for r in burst], elapsed, bob_open.status_code
424
-
425
- statuses, bob_open_seconds, bob_status = _run(scenario)
426
-
427
- assert bob_status == 200
428
- assert sorted(s["status"] for s in statuses) == ["queued", "skipped"], statuses
429
- assert bob_open_seconds < 1.4, (
430
- f"bob's interactive lesson waited {bob_open_seconds:.2f}s behind alice's prefetch slot (statuses={statuses})"
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 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€