diff --git a/docs/changes/0.63.0.rst b/docs/changes/0.63.0.rst index d736a2268eb..659713cd630 100644 --- a/docs/changes/0.63.0.rst +++ b/docs/changes/0.63.0.rst @@ -170,5 +170,3 @@ Under the hood: source of each class only once instead of once for every attribute documented on it. (:pr:`8742`) - All temporary files created by the test suite are now placed under pytest's temporary directory root, so setting ``PYTEST_DEBUG_TEMPROOT`` (or ``--basetemp``) relocates all of them. (:pr:`8749`) -- The test suite runs faster, especially on Windows: test databases are now opened with SQLite's - ``PRAGMA synchronous=OFF`` so that commits are not flushed to disk. diff --git a/docs/changes/newsfragments/+test-sqlite-sync.underthehood b/docs/changes/newsfragments/+test-sqlite-sync.underthehood new file mode 100644 index 00000000000..ede794d5ef5 --- /dev/null +++ b/docs/changes/newsfragments/+test-sqlite-sync.underthehood @@ -0,0 +1,2 @@ +The test suite runs faster, especially on Windows: test databases are now opened with SQLite's +``PRAGMA synchronous=OFF`` so that commits are not flushed to disk. diff --git a/tests/conftest.py b/tests/conftest.py index f26ce52b125..cd76a734725 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,7 +6,7 @@ import sys import threading import weakref -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any import matplotlib import pytest @@ -17,6 +17,7 @@ from qcodes.dataset import get_open_connections, initialise_database, new_data_set from qcodes.dataset.descriptions.dependencies import InterDependencies_ from qcodes.dataset.experiment_container import Experiment, new_experiment +from qcodes.dataset.sqlite.connection import AtomicConnection from qcodes.instrument import Instrument from qcodes.monitor.monitor import Monitor from qcodes.parameters import ParamSpecBase @@ -55,6 +56,31 @@ def matplotlib_set_backend() -> None: matplotlib.use("agg") +@pytest.fixture(scope="session", autouse=True) +def disable_sqlite_disk_sync() -> Generator[None, None, None]: + """ + Open every QCoDeS database connection with ``PRAGMA synchronous=OFF``. + + By default SQLite flushes the database to disk on every commit, which + protects against data loss on OS crashes or power failures. Test databases + are thrown away after the test, so these flushes only cost time. This is + especially slow on Windows, and less so where the temporary directory + is a RAM disk (e.g. ``/tmp`` on many Linux systems). Turning this off + does not change what other connections or processes can read. + """ + original_init = AtomicConnection.__init__ + + def init_without_disk_sync( + self: AtomicConnection, *args: Any, **kwargs: Any + ) -> None: + original_init(self, *args, **kwargs) + self.execute("PRAGMA synchronous=OFF") + + with pytest.MonkeyPatch.context() as mp: + mp.setattr(AtomicConnection, "__init__", init_without_disk_sync) + yield + + @pytest.fixture(scope="session", autouse=True) def default_session_config( tmp_path_factory: pytest.TempPathFactory, diff --git a/tests/dataset/test_sqlite_connection.py b/tests/dataset/test_sqlite_connection.py index e55d29f645d..c9055c32af7 100644 --- a/tests/dataset/test_sqlite_connection.py +++ b/tests/dataset/test_sqlite_connection.py @@ -374,6 +374,13 @@ def test_connect() -> None: assert None is conn.row_factory +def test_test_suite_disables_sqlite_disk_sync(tmp_path: "Path") -> None: + # The disable_sqlite_disk_sync fixture in tests/conftest.py speeds up the + # test suite by skipping disk flushes; check that it applies to files. + with closing(connect(tmp_path / "temp.db")) as conn: + assert conn.execute("PRAGMA synchronous").fetchone()[0] == 0 + + def test_open_connections_are_tracked_until_closed(tmp_path: "Path") -> None: conn = connect(tmp_path / "temp.db") assert conn in get_open_connections()