Added partial detection to async_add_job (#20119)

This commit is contained in:
Andrew Sayre 2019-01-14 17:08:44 -06:00 committed by Paulus Schoutsen
parent 0f3b6f1739
commit e73569c203
2 changed files with 48 additions and 13 deletions

View File

@ -8,6 +8,7 @@ import asyncio
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
import datetime import datetime
import enum import enum
import functools
import logging import logging
import os import os
import pathlib import pathlib
@ -258,11 +259,15 @@ class HomeAssistant:
""" """
task = None task = None
if asyncio.iscoroutine(target): check_target = target
if isinstance(target, functools.partial):
check_target = target.func
if asyncio.iscoroutine(check_target):
task = self.loop.create_task(target) # type: ignore task = self.loop.create_task(target) # type: ignore
elif is_callback(target): elif is_callback(check_target):
self.loop.call_soon(target, *args) self.loop.call_soon(target, *args)
elif asyncio.iscoroutinefunction(target): elif asyncio.iscoroutinefunction(check_target):
task = self.loop.create_task(target(*args)) task = self.loop.create_task(target(*args))
else: else:
task = self.loop.run_in_executor( # type: ignore task = self.loop.run_in_executor( # type: ignore

View File

@ -1,6 +1,7 @@
"""Test to verify that Home Assistant core works.""" """Test to verify that Home Assistant core works."""
# pylint: disable=protected-access # pylint: disable=protected-access
import asyncio import asyncio
import functools
import logging import logging
import os import os
import unittest import unittest
@ -45,11 +46,24 @@ def test_async_add_job_schedule_callback():
assert len(hass.add_job.mock_calls) == 0 assert len(hass.add_job.mock_calls) == 0
@patch('asyncio.iscoroutinefunction', return_value=True) def test_async_add_job_schedule_partial_callback():
def test_async_add_job_schedule_coroutinefunction(mock_iscoro): """Test that we schedule partial coros and add jobs to the job pool."""
"""Test that we schedule coroutines and add jobs to the job pool."""
hass = MagicMock() hass = MagicMock()
job = MagicMock() job = MagicMock()
partial = functools.partial(ha.callback(job))
ha.HomeAssistant.async_add_job(hass, partial)
assert len(hass.loop.call_soon.mock_calls) == 1
assert len(hass.loop.create_task.mock_calls) == 0
assert len(hass.add_job.mock_calls) == 0
def test_async_add_job_schedule_coroutinefunction():
"""Test that we schedule coroutines and add jobs to the job pool."""
hass = MagicMock()
async def job():
pass
ha.HomeAssistant.async_add_job(hass, job) ha.HomeAssistant.async_add_job(hass, job)
assert len(hass.loop.call_soon.mock_calls) == 0 assert len(hass.loop.call_soon.mock_calls) == 0
@ -57,11 +71,26 @@ def test_async_add_job_schedule_coroutinefunction(mock_iscoro):
assert len(hass.add_job.mock_calls) == 0 assert len(hass.add_job.mock_calls) == 0
@patch('asyncio.iscoroutinefunction', return_value=False) def test_async_add_job_schedule_partial_coroutinefunction():
def test_async_add_job_add_threaded_job_to_pool(mock_iscoro): """Test that we schedule partial coros and add jobs to the job pool."""
hass = MagicMock()
async def job():
pass
partial = functools.partial(job)
ha.HomeAssistant.async_add_job(hass, partial)
assert len(hass.loop.call_soon.mock_calls) == 0
assert len(hass.loop.create_task.mock_calls) == 1
assert len(hass.add_job.mock_calls) == 0
def test_async_add_job_add_threaded_job_to_pool():
"""Test that we schedule coroutines and add jobs to the job pool.""" """Test that we schedule coroutines and add jobs to the job pool."""
hass = MagicMock() hass = MagicMock()
job = MagicMock()
def job():
pass
ha.HomeAssistant.async_add_job(hass, job) ha.HomeAssistant.async_add_job(hass, job)
assert len(hass.loop.call_soon.mock_calls) == 0 assert len(hass.loop.call_soon.mock_calls) == 0
@ -69,13 +98,14 @@ def test_async_add_job_add_threaded_job_to_pool(mock_iscoro):
assert len(hass.loop.run_in_executor.mock_calls) == 1 assert len(hass.loop.run_in_executor.mock_calls) == 1
@patch('asyncio.iscoroutine', return_value=True) def test_async_create_task_schedule_coroutine():
def test_async_create_task_schedule_coroutine(mock_iscoro):
"""Test that we schedule coroutines and add jobs to the job pool.""" """Test that we schedule coroutines and add jobs to the job pool."""
hass = MagicMock() hass = MagicMock()
job = MagicMock()
ha.HomeAssistant.async_create_task(hass, job) async def job():
pass
ha.HomeAssistant.async_create_task(hass, job())
assert len(hass.loop.call_soon.mock_calls) == 0 assert len(hass.loop.call_soon.mock_calls) == 0
assert len(hass.loop.create_task.mock_calls) == 1 assert len(hass.loop.create_task.mock_calls) == 1
assert len(hass.add_job.mock_calls) == 0 assert len(hass.add_job.mock_calls) == 0