Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion assemblyai/tts/v1/async_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ async def _closed_error(self, ws: Any, exc: Exception) -> Exception:
except asyncio.TimeoutError:
pass
if self.error is not None:
return self.error
return self._surfaced()
code, reason = close_code_and_reason(exc)
if self._ws is not ws or code == 1000: # closed by close() or cleanly
return RuntimeError("the session is not open")
Expand Down
2 changes: 1 addition & 1 deletion assemblyai/tts/v1/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def _closed_error(self, ws: Any, exc: Exception) -> Exception:
if threading.current_thread() is not self._reader:
self._stopped.wait(1)
if self.error is not None:
return self.error
return self._surfaced()
code, reason = close_code_and_reason(exc)
if self._ws is not ws or code == 1000: # closed by close() or cleanly
return RuntimeError("the session is not open")
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/test_tts_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,44 @@ def test_an_error_the_block_already_saw_is_not_raised_twice(server):
assert len(seen) == 1 # the with block exited quietly


@pytest.mark.parametrize(
"code, error",
[
pytest.param(3010, _INPUT_RATE_EXCEEDED, id="error-frame"),
pytest.param(1012, None, id="bare-close"),
],
)
def test_a_send_error_caught_inside_with_is_not_raised_twice(server, code, error):
seen = []
with aai.TTSSession() as session:
server.close_on_next_send(code, error)
try:
session.generate("Too much, too fast.")
except aai.TTSError as err:
seen.append(err)
assert len(seen) == 1
assert seen[0] is session.error


def test_a_send_error_a_handler_raised_still_surfaces_on_exit(server):
# _emit logs a handler's exception, so nothing in the block saw it.
sent = threading.Event()

def send_after_close(event):
try:
server.close_on_next_send(1012)
session.generate("late")
finally:
sent.set()

with pytest.raises(aai.TTSError) as exc_info:
with aai.TTSSession() as session:
session.on(aai.TTSEvents.FlushDone, send_after_close)
session.flush()
assert sent.wait(2)
assert exc_info.value.body["close_code"] == 1012


def test_speak_splits_long_text_across_generate_frames(server):
with aai.TTSSession() as session:
text = "a" * 2000 + "b" * 2000 + "c" * 1000
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test_tts_session_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,27 @@ async def test_a_send_on_a_bare_close_raises_a_tts_error(server):
assert exc_info.value.body["close_code"] == 1012


@pytest.mark.parametrize(
"code, error",
[
pytest.param(3010, _INPUT_RATE_EXCEEDED, id="error-frame"),
pytest.param(1012, None, id="bare-close"),
],
)
async def test_a_send_error_caught_inside_async_with_is_not_raised_twice(
server, code, error
):
seen = []
async with aai.AsyncTTSSession() as session:
server.close_on_next_send(code, error)
try:
await session.generate("Too much, too fast.")
except aai.TTSError as err:
seen.append(err)
assert len(seen) == 1
assert seen[0] is session.error


async def test_a_cancel_drops_every_pipelined_request(server):
server.answer_flush = False
async with aai.AsyncTTSSession() as session:
Expand Down