From 342ee3a77705f4ba49a93032eee8b628bca151e0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 9 Feb 2023 02:54:37 -0600 Subject: [PATCH 1/2] Skip db locking test with postgresql/MySQL https://github.com/home-assistant/core/pull/87756#issuecomment-1423828389 --- tests/components/recorder/test_init.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index d2252cbba4c80c..8993c4b73f1985 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -1525,16 +1525,14 @@ def test_entity_id_filter(hass_recorder): async def test_database_lock_and_unlock( async_setup_recorder_instance: SetupRecorderInstanceT, hass: HomeAssistant, - recorder_db_url: str, tmp_path, ): """Test writing events during lock getting written after unlocking.""" - if recorder_db_url == "sqlite://": - # Use file DB, in memory DB cannot do write locks. - recorder_db_url = "sqlite:///" + str(tmp_path / "pytest.db") + # Database locking is only used for SQLite + # Use file DB, in memory DB cannot do write locks. config = { recorder.CONF_COMMIT_INTERVAL: 0, - recorder.CONF_DB_URL: recorder_db_url, + recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"), } await async_setup_recorder_instance(hass, config) await hass.async_block_till_done() @@ -1570,17 +1568,14 @@ def _get_db_events(): async def test_database_lock_and_overflow( async_setup_recorder_instance: SetupRecorderInstanceT, hass: HomeAssistant, - recorder_db_url: str, tmp_path, ): """Test writing events during lock leading to overflow the queue causes the database to unlock.""" + # Database locking is only used for SQLite # Use file DB, in memory DB cannot do write locks. - if recorder_db_url == "sqlite://": - # Use file DB, in memory DB cannot do write locks. - recorder_db_url = "sqlite:///" + str(tmp_path / "pytest.db") config = { recorder.CONF_COMMIT_INTERVAL: 0, - recorder.CONF_DB_URL: recorder_db_url, + recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"), } await async_setup_recorder_instance(hass, config) await hass.async_block_till_done() From 0005e89f63bdd9acc3a96efc1cc9f2f0a1fadb5a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 9 Feb 2023 03:06:19 -0600 Subject: [PATCH 2/2] switch to the guards instead --- tests/components/recorder/test_init.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index 8993c4b73f1985..7ed03552245fdc 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -1525,14 +1525,20 @@ def test_entity_id_filter(hass_recorder): async def test_database_lock_and_unlock( async_setup_recorder_instance: SetupRecorderInstanceT, hass: HomeAssistant, + recorder_db_url: str, tmp_path, ): """Test writing events during lock getting written after unlocking.""" - # Database locking is only used for SQLite - # Use file DB, in memory DB cannot do write locks. + if recorder_db_url.startswith(("mysql://", "postgresql://")): + # Database locking is only used for SQLite + return + + if recorder_db_url == "sqlite://": + # Use file DB, in memory DB cannot do write locks. + recorder_db_url = "sqlite:///" + str(tmp_path / "pytest.db") config = { recorder.CONF_COMMIT_INTERVAL: 0, - recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"), + recorder.CONF_DB_URL: recorder_db_url, } await async_setup_recorder_instance(hass, config) await hass.async_block_till_done() @@ -1568,14 +1574,21 @@ def _get_db_events(): async def test_database_lock_and_overflow( async_setup_recorder_instance: SetupRecorderInstanceT, hass: HomeAssistant, + recorder_db_url: str, tmp_path, ): """Test writing events during lock leading to overflow the queue causes the database to unlock.""" - # Database locking is only used for SQLite + if recorder_db_url.startswith(("mysql://", "postgresql://")): + # Database locking is only used for SQLite + return + # Use file DB, in memory DB cannot do write locks. + if recorder_db_url == "sqlite://": + # Use file DB, in memory DB cannot do write locks. + recorder_db_url = "sqlite:///" + str(tmp_path / "pytest.db") config = { recorder.CONF_COMMIT_INTERVAL: 0, - recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"), + recorder.CONF_DB_URL: recorder_db_url, } await async_setup_recorder_instance(hass, config) await hass.async_block_till_done()