Avoid race condition with UDP voice server starting in ESPHome (#111644)
* Avoid race condition with UDP server starting * Fix testpull/111649/head
parent
00d11ff68e
commit
890e651bdd
|
@ -160,10 +160,10 @@ class VoiceAssistantUDPServer(asyncio.DatagramProtocol):
|
|||
|
||||
async def _iterate_packets(self) -> AsyncIterable[bytes]:
|
||||
"""Iterate over incoming packets."""
|
||||
if not self.is_running:
|
||||
raise RuntimeError("Not running")
|
||||
|
||||
while data := await self.queue.get():
|
||||
if not self.is_running:
|
||||
break
|
||||
|
||||
yield data
|
||||
|
||||
def _event_callback(self, event: PipelineEvent) -> None:
|
||||
|
|
|
@ -224,9 +224,13 @@ async def test_udp_server_queue(
|
|||
|
||||
voice_assistant_udp_server_v1.close()
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
async for data in voice_assistant_udp_server_v1._iterate_packets():
|
||||
assert data == bytes(1024)
|
||||
# Stopping the UDP server should cause _iterate_packets to break out
|
||||
# immediately without yielding any data.
|
||||
has_data = False
|
||||
async for _data in voice_assistant_udp_server_v1._iterate_packets():
|
||||
has_data = True
|
||||
|
||||
assert not has_data, "Server was stopped"
|
||||
|
||||
|
||||
async def test_error_calls_handle_finished(
|
||||
|
|
Loading…
Reference in New Issue