163 lines
5.9 KiB
Python
163 lines
5.9 KiB
Python
"""Finalization retries retain one sealed capture session and one call identity."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import threading
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
import pytest
|
|
|
|
from doctor_workstation.video.launcher import VideoCallRequest
|
|
from doctor_workstation.video.lifecycle import OrderedCallLifecycle
|
|
|
|
|
|
class Repository:
|
|
def __init__(self, outcomes=(), block_attempt=0):
|
|
self.outcomes = list(outcomes)
|
|
self.block_attempt = block_attempt
|
|
self.attempts = []
|
|
self.events = []
|
|
self.entered = threading.Event()
|
|
self.release = threading.Event()
|
|
|
|
def start_call(self, **_payload):
|
|
return {"call_record_id": 71}
|
|
|
|
def start_call_transcription(self, **_payload):
|
|
return True
|
|
|
|
def upsert_call_transcript_segments(self, **_payload):
|
|
return True
|
|
|
|
def finish_call_transcription(self, **payload):
|
|
self.attempts.append(payload)
|
|
self.events.append("finish")
|
|
if len(self.attempts) == self.block_attempt:
|
|
self.entered.set()
|
|
assert self.release.wait(3), "test did not release finalization"
|
|
outcome = self.outcomes.pop(0) if self.outcomes else True
|
|
if isinstance(outcome, Exception):
|
|
raise outcome
|
|
return outcome
|
|
|
|
def end_call(self, **_payload):
|
|
self.events.append("end")
|
|
return True
|
|
|
|
|
|
@pytest.fixture
|
|
def make_lifecycle():
|
|
lifecycles = []
|
|
|
|
def make(**options):
|
|
repository = Repository(**options)
|
|
request = VideoCallRequest(
|
|
sdk_app_id=1400123456,
|
|
user_id="doctor-test",
|
|
user_sig="test-ticket",
|
|
target_user_id="patient-test",
|
|
diagnosis_id=12,
|
|
)
|
|
lifecycle = OrderedCallLifecycle(request, repository, logging.getLogger(__name__))
|
|
lifecycles.append((lifecycle, repository))
|
|
assert lifecycle.start_transcription("tr-finish-retry").result(timeout=2)
|
|
assert lifecycle.save_transcript_segment(
|
|
{"segment_id": "synthetic-1", "text": "synthetic fixture"}
|
|
).result(timeout=2)
|
|
return lifecycle, repository
|
|
|
|
yield make
|
|
for lifecycle, repository in lifecycles:
|
|
repository.release.set()
|
|
lifecycle.end("test-cleanup").result(timeout=3)
|
|
assert lifecycle.wait(3)
|
|
|
|
|
|
@pytest.mark.parametrize("failure", [
|
|
RuntimeError("temporary storage failure"), False,
|
|
{"status": "failed", "segment_count": 0},
|
|
{"status": "partial", "segment_count": 1},
|
|
])
|
|
def test_failed_finish_retries_same_snapshot_without_reopening_capture(make_lifecycle, failure):
|
|
lifecycle, repository = make_lifecycle(outcomes=[failure, True])
|
|
failed = lifecycle.finish_transcription()
|
|
with pytest.raises(RuntimeError):
|
|
failed.result(timeout=2)
|
|
with pytest.raises(RuntimeError, match="already stopped"):
|
|
lifecycle.start_transcription("tr-finish-retry")
|
|
with pytest.raises(RuntimeError, match="already stopped"):
|
|
lifecycle.save_transcript_segment({"segment_id": "late", "text": "synthetic fixture"})
|
|
|
|
retry = lifecycle.finish_transcription()
|
|
assert retry is not failed
|
|
assert retry.result(timeout=2)
|
|
assert lifecycle.finish_transcription() is retry
|
|
assert repository.attempts == [
|
|
{"diagnosis_id": 12, "call_record_id": 71,
|
|
"transcription_session_id": "tr-finish-retry",
|
|
"expected_segment_count": 1, "status": "completed"},
|
|
] * 2
|
|
|
|
|
|
def test_concurrent_finish_retries_share_one_running_write(make_lifecycle):
|
|
lifecycle, repository = make_lifecycle(
|
|
outcomes=[RuntimeError("temporary storage failure"), True], block_attempt=2,
|
|
)
|
|
with pytest.raises(RuntimeError):
|
|
lifecycle.finish_transcription().result(timeout=2)
|
|
try:
|
|
with ThreadPoolExecutor(max_workers=4) as callers:
|
|
futures = list(callers.map(lambda _: lifecycle.finish_transcription(), range(4)))
|
|
assert repository.entered.wait(2)
|
|
assert all(future is futures[0] for future in futures)
|
|
assert len(repository.attempts) == 2
|
|
finally:
|
|
repository.release.set()
|
|
assert futures[0].result(timeout=2)
|
|
|
|
|
|
def test_cancelled_finish_can_retry_without_reopening_capture(make_lifecycle):
|
|
lifecycle, repository = make_lifecycle()
|
|
entered, release = threading.Event(), threading.Event()
|
|
|
|
def occupy_worker():
|
|
entered.set()
|
|
assert release.wait(3)
|
|
|
|
lifecycle._worker.submit("test-block", occupy_worker)
|
|
try:
|
|
assert entered.wait(2)
|
|
cancelled = lifecycle.finish_transcription()
|
|
assert cancelled.cancel()
|
|
retry = lifecycle.finish_transcription()
|
|
assert retry is not cancelled
|
|
finally:
|
|
release.set()
|
|
assert retry.result(timeout=2)
|
|
assert len(repository.attempts) == 1
|
|
|
|
|
|
def test_end_retries_failed_finish_before_ending_exact_call(make_lifecycle):
|
|
lifecycle, repository = make_lifecycle(outcomes=[RuntimeError("temporary failure"), True])
|
|
with pytest.raises(RuntimeError):
|
|
lifecycle.finish_transcription().result(timeout=2)
|
|
assert lifecycle.end("synthetic-hangup").result(timeout=2)
|
|
assert repository.events == ["finish", "finish", "end"]
|
|
assert repository.attempts[-1]["status"] == "partial"
|
|
assert repository.attempts[-1]["transcription_session_id"] == "tr-finish-retry"
|
|
assert lifecycle.finish_transcription().result(timeout=2)
|
|
assert len(repository.attempts) == 2
|
|
|
|
|
|
def test_failed_finish_cannot_submit_after_end_worker_has_stopped(make_lifecycle):
|
|
lifecycle, repository = make_lifecycle(
|
|
outcomes=[RuntimeError("first failure"), RuntimeError("fallback failure")],
|
|
)
|
|
with pytest.raises(RuntimeError):
|
|
lifecycle.finish_transcription().result(timeout=2)
|
|
assert lifecycle.end("synthetic-hangup").result(timeout=2)
|
|
with pytest.raises(RuntimeError, match="already ended"):
|
|
lifecycle.finish_transcription()
|
|
assert len(repository.attempts) == 2
|