From 2582172ad1d1f67e1f4b7f9bdc2741f5825695d6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 18 Mar 2024 21:41:39 -1000 Subject: [PATCH] Create tasks eagerly with core create_task (#113781) --- homeassistant/core.py | 4 +++- tests/components/stream/test_hls.py | 13 ++++++++++--- tests/components/stream/test_recorder.py | 1 + 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index c9760194a92..a2c14814c99 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -701,7 +701,9 @@ class HomeAssistant: target: target to call. """ - self.loop.call_soon_threadsafe(self.async_create_task, target, name) + self.loop.call_soon_threadsafe( + functools.partial(self.async_create_task, target, name, eager_start=True) + ) @callback def async_create_task( diff --git a/tests/components/stream/test_hls.py b/tests/components/stream/test_hls.py index 02fbfc96ab7..23ba2e4ab34 100644 --- a/tests/components/stream/test_hls.py +++ b/tests/components/stream/test_hls.py @@ -297,6 +297,9 @@ async def test_stream_retries( stream.set_update_callback(update_callback) + open_future1 = hass.loop.create_future() + open_future2 = hass.loop.create_future() + futures = [open_future2, open_future1] cur_time = 0 def time_side_effect(): @@ -308,18 +311,22 @@ async def test_stream_retries( cur_time += 40 return cur_time + def av_open_side_effect(*args, **kwargs): + hass.loop.call_soon_threadsafe(futures.pop().set_result, None) + raise av.error.InvalidDataError(-2, "error") + with patch("av.open") as av_open, patch( "homeassistant.components.stream.time" ) as mock_time, patch( "homeassistant.components.stream.STREAM_RESTART_INCREMENT", 0 ): - av_open.side_effect = av.error.InvalidDataError(-2, "error") + av_open.side_effect = av_open_side_effect mock_time.time.side_effect = time_side_effect # Request stream. Enable retries which are disabled by default in tests. should_retry.return_value = True await stream.start() - stream._thread.join() - stream._thread = None + await open_future1 + await open_future2 assert av_open.call_count == 2 await hass.async_block_till_done() diff --git a/tests/components/stream/test_recorder.py b/tests/components/stream/test_recorder.py index 9ab9b7a680a..35b8c2442f4 100644 --- a/tests/components/stream/test_recorder.py +++ b/tests/components/stream/test_recorder.py @@ -237,6 +237,7 @@ async def test_record_stream_audio( # Fire the IdleTimer future = dt_util.utcnow() + timedelta(seconds=30) async_fire_time_changed(hass, future) + await hass.async_block_till_done() await make_recording