MatthewFlamm / pytest-homeassistant-custom-component

Package to automatically extract testing plugins from Home Assistant for custom component testing
MIT License
66 stars 10 forks source link

Cannot have multiple tests update state #155

Closed TECH7Fox closed 1 year ago

TECH7Fox commented 1 year ago

It seems that when I have multiple entity tests that trigger a state update, the given hass object isn't working correctly. I get this for every test, except the first one:

tests/mock_ami_client.py:14: in trigger_event
    handler(event)
custom_components/asterisk/sensor.py:195: in handle_dtmf
    self.hass.async_add_job(self.async_update_ha_state)
vendor/lib/python3.10/site-packages/pytest_homeassistant_custom_component/common.py:205: in async_add_job
    return orig_async_add_job(target, *args)
vendor/lib/python3.10/site-packages/homeassistant/core.py:456: in async_add_job
    return self.async_add_hass_job(HassJob(target), *args)
vendor/lib/python3.10/site-packages/homeassistant/core.py:492: in async_add_hass_job
    task = self.loop.create_task(hassjob.target(*args), name=hassjob.name)
../../.pyenv/versions/3.10.6/lib/python3.10/asyncio/base_events.py:436: in create_task
    self._check_closed()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <_UnixSelectorEventLoop running=False closed=True debug=False>

    def _check_closed(self):
        if self._closed:
>           raise RuntimeError('Event loop is closed')
E           RuntimeError: Event loop is closed

../../.pyenv/versions/3.10.6/lib/python3.10/asyncio/base_events.py:515: RuntimeError

And it really is only the first one that works. When I remove the first test, the second test succeeds and the others still fail. Is this a known issue?

Here is the source: https://github.com/TECH7Fox/asterisk-hass-integration/blob/asterisk-ami/tests/test_sensors.py

I could set all the sensors in one test, but I rather have them separated since they effect each other.

Hope someone can help me with this. Anyway, thanks for this very useful package!

MatthewFlamm commented 1 year ago

I wonder if using pytest_asyncio asyncio_mode = auto is actually mandatory with how the plugin works. See #129. This is the readme as being maybe required, but if https://github.com/TECH7Fox/asterisk-hass-integration/pull/76 works, then I will update the readme here accordingly.

TECH7Fox commented 1 year ago

Thanks for helping! With asyncio_mode = auto it's still the same problem.

From your PR CI:

=========================== short test summary info ============================
FAILED tests/test_sensors.py::test_connected_line_sensor - RuntimeError: Event loop is closed
FAILED tests/test_sensors.py::test_dtmf_sent_sensor - RuntimeError: Event loop is closed
FAILED tests/test_sensors.py::test_dtmf_received_sensor - RuntimeError: Event loop is closed
=================== 3 failed, 3 passed, 3 warnings in 1.11s ====================
sys:1: RuntimeWarning: coroutine 'Entity.async_update_ha_state' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
MatthewFlamm commented 1 year ago

Sorry I'm out of any other specific ideas. It looks like your code could be mixing sync and async contexts, but I'm unsure if that would cause this problem. Do you get any errors when testing live on a server?

TECH7Fox commented 1 year ago

Thanks for your help @MatthewFlamm.

Do you get any errors when testing live on a server?

You mean installing on a actual HA server? On my dev HA container it works fine, no errors.

Do you know another component with multiple tests that perform a state update?

MatthewFlamm commented 1 year ago

157 has multiple tests that update the state of a sensor successfully.

TECH7Fox commented 1 year ago

I tried to create all the event handlers async and await the async_update_ha_state and the tests worked. But I can't use that since the library I use can't call async event handlers.

Weird that the tests fail but when trying it out it all works fine. For some reason using self.hass.async_add_job(self.async_update_ha_state) in tests doesn't really work. (Also tried self.hass.add_job)

MatthewFlamm commented 1 year ago

Sounds like you now know the problem and it doesn't sound like it is this library. I would check whether any built in tests help you in this.