Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@
# Delay between retries while waiting for IAM role propagation.
ROLE_PROPAGATION_RETRY_DELAY = 5

# Maximum time to wait for clinical note generation after the stream ends.
CLINICAL_NOTE_TIMEOUT = 300
# Delay between checks while waiting for clinical note generation.
CLINICAL_NOTE_POLL_DELAY = 5

async def _run_medical_scribe_session(role_arn: str, s3_bucket: str) -> None:

async def _run_medical_scribe_session(role_arn: str, s3_bucket: str) -> str:
"""Run a full Medical Scribe streaming session and verify its completion."""
async with await create_transcribe_client(
"us-east-1", transport=AWSCRTHTTPClient()
Expand Down Expand Up @@ -113,6 +118,31 @@ async def _run_medical_scribe_session(role_arn: str, s3_bucket: str) -> None:
assert details.media_encoding == "pcm"
assert details.media_sample_rate_hertz == SAMPLE_RATE

return session_id


async def _wait_for_clinical_note(session_id: str) -> None:
"""Wait for clinical note generation to finish.

It runs after the stream ends and writes to the bucket, so the fixture must
not delete the bucket and role until it is done.
"""
async with await create_transcribe_client(
"us-east-1", transport=AWSCRTHTTPClient()
) as transcribe_client:
async with asyncio.timeout(CLINICAL_NOTE_TIMEOUT):
while True:
response = await transcribe_client.get_medical_scribe_stream(
session_id=session_id
)
details = response.medical_scribe_stream_details
result = details.post_stream_analytics_result if details else None
note = result.clinical_note_generation_result if result else None
if note is not None and note.status != "IN_PROGRESS":
assert note.status == "COMPLETED", note.failure_reason
return
await asyncio.sleep(CLINICAL_NOTE_POLL_DELAY)


async def test_get_medical_scribe_stream(
healthscribe_resources: tuple[str, str],
Expand All @@ -129,12 +159,13 @@ async def test_get_medical_scribe_stream(
role_arn, s3_bucket = healthscribe_resources

last_error: BadRequestException | LimitExceededException | CallError | None = None
session_id: str | None = None
try:
async with asyncio.timeout(ROLE_PROPAGATION_TIMEOUT):
while True:
try:
await _run_medical_scribe_session(role_arn, s3_bucket)
return
session_id = await _run_medical_scribe_session(role_arn, s3_bucket)
break
except (BadRequestException, LimitExceededException) as e:
# BadRequestException: IAM role not yet propagated.
# LimitExceededException: post-stream analytics job quota exceeded
Expand All @@ -152,3 +183,6 @@ async def test_get_medical_scribe_stream(
except TimeoutError:
assert last_error is not None
raise last_error

assert session_id is not None
await _wait_for_clinical_note(session_id)