Skip to content

Commit a34500e

Browse files
committed
Open testmondata in readonly mode in xdist workers. This hopefully fixes re #203 sqlite3.OperationalError: database is locked .
1 parent c86f995 commit a34500e

File tree

4 files changed

+58
-40
lines changed

4 files changed

+58
-40
lines changed

testmon/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""PYTEST_DONT_REWRITE"""
2-
VERSION = "2.0.13"
2+
VERSION = "2.0.14.dev0"

testmon/db.py

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ def check_fingerprint_db(files_methods_checksums, record):
4444

4545

4646
class DB:
47-
def __init__(self, datafile):
47+
def __init__(self, datafile, readonly=False):
48+
self._readonly = readonly
4849
new_db = not os.path.exists(datafile)
4950

50-
connection = connect(datafile)
51+
connection = connect(datafile, readonly)
5152
self.con = connection
5253
old_format = self._check_data_version(datafile)
5354

@@ -483,10 +484,11 @@ def delete_filenames(self, con):
483484

484485
def determine_tests(self, exec_id, files_mhashes):
485486
with self.con as con:
486-
con.execute(
487-
f"UPDATE test_execution set forced = NULL WHERE {self._test_execution_fk_column()} = ?",
488-
[exec_id],
489-
)
487+
if not self._readonly:
488+
con.execute(
489+
f"UPDATE test_execution set forced = NULL WHERE {self._test_execution_fk_column()} = ?",
490+
[exec_id],
491+
)
490492
self.delete_filenames(con)
491493
con.executemany(
492494
"INSERT INTO changed_files_mhashes VALUES (?, ?, ?)",
@@ -634,40 +636,52 @@ def fetch_or_create_environment(
634636
self, environment_name, system_packages, python_version
635637
):
636638
with self.con as con:
637-
try:
638-
cursor = con.cursor()
639-
cursor.execute(
640-
"""
641-
INSERT INTO environment VALUES (?, ?, ?, ?)
642-
""",
643-
(
644-
None,
645-
environment_name,
646-
system_packages,
647-
python_version,
648-
),
649-
)
650-
environment_id = cursor.lastrowid
651-
count = cursor.execute(
652-
"""
653-
SELECT count(*) as count FROM environment WHERE environment_name = ?
654-
""",
655-
(environment_name,),
656-
).fetchone()
657-
packages_changed = count["count"] > 1
658-
except sqlite3.IntegrityError:
659-
environment = con.execute(
660-
"""
661-
SELECT
662-
id as id, environment_name as name, system_packages as packages
663-
FROM environment
664-
WHERE environment_name = ?
665-
""",
666-
(environment_name,),
667-
).fetchone()
639+
cursor = con.cursor()
640+
environment = cursor.execute(
641+
"""
642+
SELECT
643+
id, environment_name, system_packages, python_version
644+
FROM environment
645+
WHERE environment_name = ?
646+
""",
647+
(environment_name,),
648+
).fetchone()
649+
650+
if environment:
668651
environment_id = environment["id"]
652+
packages_changed = (
653+
environment["system_packages"] != system_packages
654+
or environment["python_version"] != python_version
655+
)
656+
else:
669657
packages_changed = False
670-
return environment_id, packages_changed
658+
if not environment or packages_changed:
659+
try:
660+
cursor.execute(
661+
"""
662+
INSERT INTO environment (environment_name, system_packages, python_version)
663+
VALUES (?, ?, ?)
664+
""",
665+
(
666+
environment_name,
667+
system_packages,
668+
python_version,
669+
),
670+
)
671+
environment_id = cursor.lastrowid
672+
except sqlite3.IntegrityError:
673+
environment = con.execute(
674+
"""
675+
SELECT
676+
id as id, environment_name as name, system_packages as packages
677+
FROM environment
678+
WHERE environment_name = ?
679+
""",
680+
(environment_name,),
681+
).fetchone()
682+
environment_id = environment["id"]
683+
684+
return environment_id, packages_changed
671685

672686
def initiate_execution(
673687
self,

testmon/pytest_testmon.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def init_testmon_data(config):
175175
database=rpc_proxy,
176176
environment=environment,
177177
system_packages=system_packages,
178+
readonly=parallelism_status(config) == "worker",
178179
)
179180
testmon_data.determine_stable(bool(rpc_proxy))
180181
config.testmon_data = testmon_data

testmon/testmon_core.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def __init__(
141141
environment=None,
142142
system_packages=None,
143143
python_version=None,
144+
readonly=False,
144145
):
145146
self.rootdir = rootdir
146147
self.environment = environment if environment else "default"
@@ -154,7 +155,9 @@ def __init__(
154155
if database:
155156
self.db = database
156157
else:
157-
self.db = db.DB(os.path.join(self.rootdir, get_data_file_path()))
158+
self.db = db.DB(
159+
os.path.join(self.rootdir, get_data_file_path()), readonly=readonly
160+
)
158161

159162
try:
160163
result = self.db.initiate_execution(

0 commit comments

Comments
 (0)