|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Test bidirectional streaming event stream handling.""" |
| 5 | + |
| 6 | +import asyncio |
| 7 | + |
| 8 | +from smithy_core.aio.eventstream import DuplexEventStream |
| 9 | + |
| 10 | +from aws_sdk_polly.models import ( |
| 11 | + CloseStreamEvent, |
| 12 | + StartSpeechSynthesisStreamActionStream, |
| 13 | + StartSpeechSynthesisStreamActionStreamCloseStreamEvent, |
| 14 | + StartSpeechSynthesisStreamActionStreamTextEvent, |
| 15 | + StartSpeechSynthesisStreamEventStream, |
| 16 | + StartSpeechSynthesisStreamEventStreamAudioEvent, |
| 17 | + StartSpeechSynthesisStreamEventStreamServiceFailureException, |
| 18 | + StartSpeechSynthesisStreamEventStreamServiceQuotaExceededException, |
| 19 | + StartSpeechSynthesisStreamEventStreamStreamClosedEvent, |
| 20 | + StartSpeechSynthesisStreamEventStreamThrottlingException, |
| 21 | + StartSpeechSynthesisStreamEventStreamUnknown, |
| 22 | + StartSpeechSynthesisStreamEventStreamValidationException, |
| 23 | + StartSpeechSynthesisStreamInput, |
| 24 | + StartSpeechSynthesisStreamOutput, |
| 25 | + TextEvent, |
| 26 | +) |
| 27 | + |
| 28 | +from . import ( |
| 29 | + ENGINE, |
| 30 | + OUTPUT_FORMAT, |
| 31 | + REGION, |
| 32 | + SAMPLE_RATE, |
| 33 | + TEST_TEXT, |
| 34 | + VOICE_ID, |
| 35 | + create_polly_client, |
| 36 | +) |
| 37 | + |
| 38 | +ERROR_EVENT_TYPES = ( |
| 39 | + StartSpeechSynthesisStreamEventStreamValidationException, |
| 40 | + StartSpeechSynthesisStreamEventStreamServiceQuotaExceededException, |
| 41 | + StartSpeechSynthesisStreamEventStreamServiceFailureException, |
| 42 | + StartSpeechSynthesisStreamEventStreamThrottlingException, |
| 43 | +) |
| 44 | + |
| 45 | + |
| 46 | +async def _send_text( |
| 47 | + stream: DuplexEventStream[ |
| 48 | + StartSpeechSynthesisStreamActionStream, |
| 49 | + StartSpeechSynthesisStreamEventStream, |
| 50 | + StartSpeechSynthesisStreamOutput, |
| 51 | + ], |
| 52 | +) -> None: |
| 53 | + """Send text input and close the input stream.""" |
| 54 | + await stream.input_stream.send( |
| 55 | + StartSpeechSynthesisStreamActionStreamTextEvent(value=TextEvent(text=TEST_TEXT)) |
| 56 | + ) |
| 57 | + await stream.input_stream.send( |
| 58 | + StartSpeechSynthesisStreamActionStreamCloseStreamEvent(CloseStreamEvent()) |
| 59 | + ) |
| 60 | + await stream.input_stream.close() |
| 61 | + |
| 62 | + |
| 63 | +async def _receive_audio( |
| 64 | + stream: DuplexEventStream[ |
| 65 | + StartSpeechSynthesisStreamActionStream, |
| 66 | + StartSpeechSynthesisStreamEventStream, |
| 67 | + StartSpeechSynthesisStreamOutput, |
| 68 | + ], |
| 69 | +) -> tuple[int, int | None]: |
| 70 | + """Receive synthesized audio and the final stream summary.""" |
| 71 | + audio_bytes = 0 |
| 72 | + request_characters: int | None = None |
| 73 | + |
| 74 | + _, output_stream = await stream.await_output() |
| 75 | + if output_stream is None: |
| 76 | + return audio_bytes, request_characters |
| 77 | + |
| 78 | + async for event in output_stream: |
| 79 | + if isinstance(event, StartSpeechSynthesisStreamEventStreamAudioEvent): |
| 80 | + if event.value.audio_chunk: |
| 81 | + audio_bytes += len(event.value.audio_chunk) |
| 82 | + elif isinstance(event, StartSpeechSynthesisStreamEventStreamStreamClosedEvent): |
| 83 | + request_characters = event.value.request_characters |
| 84 | + break |
| 85 | + elif isinstance(event, ERROR_EVENT_TYPES): |
| 86 | + raise event.value |
| 87 | + elif isinstance(event, StartSpeechSynthesisStreamEventStreamUnknown): |
| 88 | + raise RuntimeError(f"Received unknown event in stream: {event.tag}") |
| 89 | + else: |
| 90 | + raise RuntimeError( |
| 91 | + f"Received unexpected event type in stream: {type(event).__name__}" |
| 92 | + ) |
| 93 | + |
| 94 | + return audio_bytes, request_characters |
| 95 | + |
| 96 | + |
| 97 | +async def test_start_speech_synthesis_stream() -> None: |
| 98 | + """Test bidirectional streaming with text input and audio output.""" |
| 99 | + client = create_polly_client(REGION) |
| 100 | + |
| 101 | + stream = await client.start_speech_synthesis_stream( |
| 102 | + input=StartSpeechSynthesisStreamInput( |
| 103 | + engine=ENGINE, |
| 104 | + output_format=OUTPUT_FORMAT, |
| 105 | + sample_rate=SAMPLE_RATE, |
| 106 | + voice_id=VOICE_ID, |
| 107 | + ) |
| 108 | + ) |
| 109 | + |
| 110 | + results = await asyncio.gather(_send_text(stream), _receive_audio(stream)) |
| 111 | + audio_bytes, request_characters = results[1] |
| 112 | + |
| 113 | + assert audio_bytes > 0, "Expected to receive synthesized audio" |
| 114 | + assert request_characters == len(TEST_TEXT) |
0 commit comments