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
This commit is contained in:
Pascal Vizeli 2016-11-17 05:00:08 +01:00 committed by Paulus Schoutsen
parent a862bc4edc
commit c8bc1e3c5d
2 changed files with 38 additions and 11 deletions

View File

@ -210,8 +210,7 @@ class HomeAssistant(object):
target: target to call. target: target to call.
args: parameters for method to call. args: parameters for method to call.
""" """
run_callback_threadsafe( self.loop.call_soon_threadsafe(self.async_add_job, target, *args)
self.loop, self.async_add_job, target, *args).result()
@callback @callback
def async_add_job(self, target: Callable[..., None], *args: Any) -> None: def async_add_job(self, target: Callable[..., None], *args: Any) -> None:

View File

@ -154,12 +154,21 @@ class TestHomeAssistant(unittest.TestCase):
"""Test Coro.""" """Test Coro."""
call_count.append('call') call_count.append('call')
for i in range(50): for i in range(2):
self.hass.add_job(test_coro()) 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() self.hass.block_till_done()
assert len(call_count) == 50 assert len(call_count) == 2
def test_async_add_job_pending_tasks_executor(self): def test_async_add_job_pending_tasks_executor(self):
"""Run a executor in pending tasks.""" """Run a executor in pending tasks."""
@ -169,12 +178,21 @@ class TestHomeAssistant(unittest.TestCase):
"""Test executor.""" """Test executor."""
call_count.append('call') 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) 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() self.hass.block_till_done()
assert len(call_count) == 40 assert len(call_count) == 2
def test_async_add_job_pending_tasks_callback(self): def test_async_add_job_pending_tasks_callback(self):
"""Run a callback in pending tasks.""" """Run a callback in pending tasks."""
@ -185,12 +203,22 @@ class TestHomeAssistant(unittest.TestCase):
"""Test callback.""" """Test callback."""
call_count.append('call') 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) 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() 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): class TestEvent(unittest.TestCase):