async def test_check_loop_async_integration(caplog): """Test check_loop detects when called from event loop from integration context.""" with pytest.raises(RuntimeError), patch( "homeassistant.util.async_.extract_stack", return_value=[ Mock( filename="/home/paulus/homeassistant/core.py", lineno="23", line="do_something()", ), Mock( filename="/home/paulus/homeassistant/components/hue/light.py", lineno="23", line="self.light.is_on", ), Mock( filename="/home/paulus/aiohue/lights.py", lineno="2", line="something()", ), ], ): hasync.check_loop() assert ( "Detected I/O inside the event loop. This is causing stability issues. Please report issue for hue doing I/O at homeassistant/components/hue/light.py, line 23: self.light.is_on" in caplog.text )
async def test_check_loop_async_custom(caplog): """Test check_loop detects when called from event loop with custom component context.""" with patch( "homeassistant.util.async_.extract_stack", return_value=[ Mock( filename="/home/paulus/homeassistant/core.py", lineno="23", line="do_something()", ), Mock( filename= "/home/paulus/config/custom_components/hue/light.py", lineno="23", line="self.light.is_on", ), Mock( filename="/home/paulus/aiohue/lights.py", lineno="2", line="something()", ), ], ): hasync.check_loop() assert ( "Detected I/O inside the event loop. This is causing stability issues. Please report issue to the custom component author for hue doing I/O at custom_components/hue/light.py, line 23: self.light.is_on" in caplog.text)
async def test_check_loop_async_integration_non_strict(caplog): """Test check_loop detects when called from event loop from integration context.""" with patch( "homeassistant.util.async_.extract_stack", return_value=[ Mock( filename="/home/paulus/homeassistant/core.py", lineno="23", line="do_something()", ), Mock( filename= "/home/paulus/homeassistant/components/hue/light.py", lineno="23", line="self.light.is_on", ), Mock( filename="/home/paulus/aiohue/lights.py", lineno="2", line="something()", ), ], ): hasync.check_loop(banned_function, strict=False) assert ( "Detected blocking call inside the event loop. This is causing stability issues. " "Please report issue for hue doing blocking calls at " "homeassistant/components/hue/light.py, line 23: self.light.is_on" in caplog.text)
async def test_check_loop_async_custom(caplog): """Test check_loop detects when called from event loop with custom component context.""" with pytest.raises(RuntimeError), patch( "homeassistant.util.async_.extract_stack", return_value=[ Mock( filename="/home/paulus/homeassistant/core.py", lineno="23", line="do_something()", ), Mock( filename="/home/paulus/config/custom_components/hue/light.py", lineno="23", line="self.light.is_on", ), Mock( filename="/home/paulus/aiohue/lights.py", lineno="2", line="something()", ), ], ): hasync.check_loop(banned_function) assert ( "Detected blocking call to banned_function inside the event loop. This is " "causing stability issues. Please report issue to the custom integration author " "for hue doing blocking calls at custom_components/hue/light.py, line 23: " "self.light.is_on" in caplog.text )
def _do_get(self) -> Any: if self.recorder_or_dbworker: return super()._do_get() check_loop( self._do_get_db_connection_protected, strict=True, advise_msg=ADVISE_MSG, ) return self._do_get_db_connection_protected()
async def test_check_loop_async(): """Test check_loop detects when called from event loop without integration context.""" with pytest.raises(RuntimeError): hasync.check_loop()
def test_check_loop_sync(caplog): """Test check_loop does nothing when called from thread.""" hasync.check_loop() assert "Detected I/O inside the event loop" not in caplog.text
def test_check_loop_sync(caplog): """Test check_loop does nothing when called from thread.""" hasync.check_loop(banned_function) assert "Detected blocking call inside the event loop" not in caplog.text