From c8bc1e3c5d586f9d81c614906698aebc958ea1b4 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Thu, 17 Nov 2016 05:00:08 +0100 Subject: [PATCH] change add_job to use call_soon_threadsafe (#4410) * change add_job to use call_soon_threadsafe * address comments from paulus * Tweak core tests * Fix tests Python 3.4.2 --- homeassistant/core.py | 3 +-- tests/test_core.py | 46 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index c1be6e760d5..90f998508d8 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -210,8 +210,7 @@ class HomeAssistant(object): target: target to call. args: parameters for method to call. """ - run_callback_threadsafe( - self.loop, self.async_add_job, target, *args).result() + self.loop.call_soon_threadsafe(self.async_add_job, target, *args) @callback def async_add_job(self, target: Callable[..., None], *args: Any) -> None: diff --git a/tests/test_core.py b/tests/test_core.py index 60057e57ad1..212c6d41f70 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -154,12 +154,21 @@ class TestHomeAssistant(unittest.TestCase): """Test Coro.""" call_count.append('call') - for i in range(50): + for i in range(2): self.hass.add_job(test_coro()) - assert len(self.hass._pending_tasks) == 50 + @asyncio.coroutine + def wait_finish_callback(): + """Wait until all stuff is scheduled.""" + yield from asyncio.sleep(0, loop=self.hass.loop) + yield from asyncio.sleep(0, loop=self.hass.loop) + + run_coroutine_threadsafe( + wait_finish_callback(), self.hass.loop).result() + + assert len(self.hass._pending_tasks) == 2 self.hass.block_till_done() - assert len(call_count) == 50 + assert len(call_count) == 2 def test_async_add_job_pending_tasks_executor(self): """Run a executor in pending tasks.""" @@ -169,12 +178,21 @@ class TestHomeAssistant(unittest.TestCase): """Test executor.""" call_count.append('call') - for i in range(40): + @asyncio.coroutine + def wait_finish_callback(): + """Wait until all stuff is scheduled.""" + yield from asyncio.sleep(0, loop=self.hass.loop) + yield from asyncio.sleep(0, loop=self.hass.loop) + + for i in range(2): self.hass.add_job(test_executor) - assert len(self.hass._pending_tasks) == 40 + run_coroutine_threadsafe( + wait_finish_callback(), self.hass.loop).result() + + assert len(self.hass._pending_tasks) == 2 self.hass.block_till_done() - assert len(call_count) == 40 + assert len(call_count) == 2 def test_async_add_job_pending_tasks_callback(self): """Run a callback in pending tasks.""" @@ -185,12 +203,22 @@ class TestHomeAssistant(unittest.TestCase): """Test callback.""" call_count.append('call') - for i in range(40): + @asyncio.coroutine + def wait_finish_callback(): + """Wait until all stuff is scheduled.""" + yield from asyncio.sleep(0, loop=self.hass.loop) + yield from asyncio.sleep(0, loop=self.hass.loop) + + for i in range(2): self.hass.add_job(test_callback) - assert len(self.hass._pending_tasks) == 0 + run_coroutine_threadsafe( + wait_finish_callback(), self.hass.loop).result() + self.hass.block_till_done() - assert len(call_count) == 40 + + assert len(self.hass._pending_tasks) == 0 + assert len(call_count) == 2 class TestEvent(unittest.TestCase):