Handle cancellation in ServiceRegistry.async_call (#33644)

This commit is contained in:
Phil Bruckner authored and GitHub committed 2020-04-04 15:36:33 -07:00
1 parent d7e9959442
commit bf1b408038
2 files changed
+83 -18

No files matched your search

+36
View File
@@ -1214,6 +1214,42 @@ async def test_async_functions_with_callback(hass):
assert len(runs) == 3
@pytest.mark.parametrize("cancel_call", [True, False])
async def test_cancel_service_task(hass, cancel_call):
"""Test cancellation."""
service_called = asyncio.Event()
service_cancelled = False
async def service_handler(call):
nonlocal service_cancelled
service_called.set()
try:
await asyncio.sleep(10)
except asyncio.CancelledError:
service_cancelled = True
raise
hass.services.async_register("test_domain", "test_service", service_handler)
call_task = hass.async_create_task(
hass.services.async_call("test_domain", "test_service", blocking=True)
)
tasks_1 = asyncio.all_tasks()
await asyncio.wait_for(service_called.wait(), timeout=1)
tasks_2 = asyncio.all_tasks() - tasks_1
assert len(tasks_2) == 1
service_task = tasks_2.pop()
if cancel_call:
call_task.cancel()
else:
service_task.cancel()
with pytest.raises(asyncio.CancelledError):
await call_task
assert service_cancelled
def test_valid_entity_id():
"""Test valid entity ID."""
for invalid in [