Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

test(ci): use a file-backed SQLite so threaded tests don't race

Juan Denis84c943ce2fb9
diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py
index e727d343..59f212c6 100644
--- a/backend/tests/conftest.py
+++ b/backend/tests/conftest.py
@@ -11,6 +11,42 @@ if _backend not in sys.path:
os.environ.setdefault('FLASK_ENV', 'testing')
+# Tests use a FILE-backed SQLite, not :memory:. Flask-SQLAlchemy serves an
+# in-memory SQLite from a single shared connection (StaticPool), so a test that
+# drives the DB from a background thread (e.g. the agent send_command round-trip
+# e2e) races on that one connection and intermittently dies with
+# ObjectDeletedError / PendingRollbackError. A temp file gives each thread its
+# own connection with SQLite's own file locking — safe and deterministic. Each
+# test still create_all/drop_all's a clean schema (see the `app` fixture).
+if 'TEST_DATABASE_URL' not in os.environ:
+ import tempfile
+ _test_db = os.path.join(tempfile.gettempdir(), 'serverkit_test.db').replace('\\', '/')
+ try:
+ os.remove(_test_db)
+ except OSError:
+ pass
+ os.environ['TEST_DATABASE_URL'] = 'sqlite:///' + _test_db
+
+
+# A file-backed SQLite fsyncs on every commit, which makes the suite ~3x
+# slower than :memory:. For throwaway test data that durability is pure
+# overhead, so disable it and keep the journal in memory — this recovers
+# roughly in-memory speed while keeping the per-connection thread-safety the
+# file gives us. SQLite-only and scoped to the test process.
+import sqlite3 # noqa: E402
+from sqlalchemy import event # noqa: E402
+from sqlalchemy.engine import Engine # noqa: E402
+
+
[email protected]_for(Engine, 'connect')
+def _fast_sqlite_for_tests(dbapi_connection, _record):
+ if isinstance(dbapi_connection, sqlite3.Connection):
+ cur = dbapi_connection.cursor()
+ cur.execute('PRAGMA synchronous=OFF')
+ cur.execute('PRAGMA journal_mode=MEMORY')
+ cur.execute('PRAGMA temp_store=MEMORY')
+ cur.close()
+
@pytest.fixture(scope='function')
def app():