Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions docs/changes/0.63.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 2 additions & 0 deletions docs/changes/newsfragments/+test-sqlite-sync.underthehood
Original file line number Diff line number Diff line change
@@ -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.
28 changes: 27 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions tests/dataset/test_sqlite_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading