mirror of
https://github.com/harry0703/MoneyPrinterTurbo.git
synced 2026-11-14 18:22:59 +01:00
The Edge stream is handed to the caller through a one-slot queue, and the async producer slept a fixed 10 ms whenever the slot was still taken. Edge sends about 11 chunks (audio and word boundaries) per second of speech and streams them far faster than 100 per second, so nearly every chunk waited: a 466 s English script (5,153 chunks) needed more than the 30 s edge_tts_timeout, so each of the three tries timed out and the task failed with "failed to synthesize audio", although Edge streams it in about 6 s. The producer now waits on an asyncio.Event that the consumer sets after each take, so the next chunk goes in as soon as the slot is free. The one-slot queue, the stopped checks and the cancellation on timeout or callback failure are unchanged.
122 lines
4.9 KiB
Python
122 lines
4.9 KiB
Python
import asyncio
|
|
import edge_tts
|
|
from unittest.mock import patch
|
|
import threading
|
|
import unittest
|
|
from app.services import voice
|
|
|
|
|
|
class TestEdgeProducerShutdown(unittest.TestCase):
|
|
def test_callback_failure_stops_producer_and_closes_generator(self):
|
|
resume = threading.Event()
|
|
closed = threading.Event()
|
|
consumed = []
|
|
|
|
class Stream:
|
|
def stream_sync(self):
|
|
try:
|
|
yield {"type": "audio", "data": b"first"}
|
|
resume.wait(2)
|
|
for index in range(100):
|
|
consumed.append(index)
|
|
yield {"type": "audio", "data": b"tail"}
|
|
finally:
|
|
closed.set()
|
|
|
|
def fail(chunk):
|
|
raise OSError("disk full")
|
|
|
|
with self.assertRaisesRegex(OSError, "disk full"):
|
|
voice._stream_edge_tts_sync_with_timeout(Stream(), fail, 2)
|
|
resume.set()
|
|
self.assertTrue(closed.wait(2), "producer did not finish")
|
|
self.assertLessEqual(len(consumed), 2, "abandoned producer consumed the entire stream")
|
|
|
|
def test_normal_stream_and_producer_error_are_forwarded(self):
|
|
class Stream:
|
|
def stream_sync(self):
|
|
yield {"type": "audio", "data": b"first"}
|
|
raise ValueError("broken stream")
|
|
|
|
chunks = []
|
|
with self.assertRaisesRegex(ValueError, "broken stream"):
|
|
voice._stream_edge_tts_sync_with_timeout(Stream(), chunks.append, 2)
|
|
self.assertEqual(chunks, [{"type": "audio", "data": b"first"}])
|
|
|
|
def test_timed_out_stream_stops_when_blocked_read_returns(self):
|
|
resume = threading.Event()
|
|
closed = threading.Event()
|
|
consumed = []
|
|
class Stream:
|
|
def stream_sync(self):
|
|
try:
|
|
resume.wait(2)
|
|
for index in range(100):
|
|
consumed.append(index)
|
|
yield {"type": "audio", "data": b"late"}
|
|
finally:
|
|
closed.set()
|
|
try:
|
|
with self.assertRaises(TimeoutError):
|
|
voice._stream_edge_tts_sync_with_timeout(Stream(), lambda chunk: None, 0.01)
|
|
finally:
|
|
resume.set()
|
|
self.assertTrue(closed.wait(2))
|
|
self.assertLessEqual(len(consumed), 2)
|
|
|
|
def test_real_sdk_stream_cancelled_without_draining_tail_after_callback_failure(self):
|
|
resume = threading.Event()
|
|
closed = threading.Event()
|
|
fetched = []
|
|
communicate = edge_tts.Communicate("Hello.", "en-US-AriaNeural")
|
|
async def network_stream():
|
|
try:
|
|
yield {"type": "audio", "data": b"first"}
|
|
while not resume.is_set():
|
|
await asyncio.sleep(0.001)
|
|
for index in range(100):
|
|
fetched.append(index)
|
|
yield {"type": "audio", "data": b"tail"}
|
|
finally:
|
|
closed.set()
|
|
def fail(chunk):
|
|
raise OSError("disk full")
|
|
with patch.object(communicate, "stream", new=network_stream):
|
|
try:
|
|
with self.assertRaisesRegex(OSError, "disk full"):
|
|
voice._stream_edge_tts_sync_with_timeout(communicate, fail, 2)
|
|
finally:
|
|
resume.set()
|
|
self.assertTrue(closed.wait(2))
|
|
self.assertLessEqual(len(fetched), 2)
|
|
|
|
def test_real_sdk_blocked_async_read_is_cancelled_after_timeout(self):
|
|
closed = threading.Event()
|
|
started = threading.Event()
|
|
communicate = edge_tts.Communicate("Hello.", "en-US-AriaNeural")
|
|
async def network_stream():
|
|
try:
|
|
started.set()
|
|
await asyncio.sleep(0.2)
|
|
yield {"type": "audio", "data": b"late"}
|
|
finally:
|
|
closed.set()
|
|
with patch.object(communicate, "stream", new=network_stream):
|
|
with self.assertRaises(TimeoutError):
|
|
voice._stream_edge_tts_sync_with_timeout(communicate, lambda chunk: None, 0.05)
|
|
self.assertTrue(started.is_set())
|
|
self.assertTrue(closed.wait(0.1), "SDK read was not cancelled promptly")
|
|
|
|
def test_real_sdk_ready_chunks_are_not_throttled_into_a_timeout(self):
|
|
# Long narrations stream thousands of chunks; each one must be handed
|
|
# over as soon as the previous one is taken, not after a fixed sleep.
|
|
communicate = edge_tts.Communicate("Hello.", "en-US-AriaNeural")
|
|
expected = [index.to_bytes(2, "big") for index in range(500)]
|
|
async def network_stream():
|
|
for data in expected:
|
|
yield {"type": "audio", "data": data}
|
|
chunks = []
|
|
with patch.object(communicate, "stream", new=network_stream):
|
|
voice._stream_edge_tts_sync_with_timeout(communicate, chunks.append, 2)
|
|
self.assertEqual([chunk["data"] for chunk in chunks], expected)
|