Skip to content

Commit 0393a03

Browse files
committed
Fix integration test teardown error
If you don't close DB connections when you're done with them, I will scream
1 parent f2fb28d commit 0393a03

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

cylc/flow/dbstatecheck.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(self, rund, workflow, db_path=None):
7272
if not os.path.exists(db_path):
7373
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), db_path)
7474

75-
self.conn = sqlite3.connect(db_path, timeout=10.0)
75+
self.conn: sqlite3.Connection = sqlite3.connect(db_path, timeout=10.0)
7676

7777
# Get workflow point format.
7878
try:
@@ -84,8 +84,17 @@ def __init__(self, rund, workflow, db_path=None):
8484
self.db_point_fmt = self._get_db_point_format_compat()
8585
self.c7_back_compat_mode = True
8686
except sqlite3.OperationalError:
87+
with suppress(Exception):
88+
self.conn.close()
8789
raise exc # original error
8890

91+
def __enter__(self):
92+
return self
93+
94+
def __exit__(self, exc_type, exc_value, traceback):
95+
"""Close DB connection when leaving context manager."""
96+
self.conn.close()
97+
8998
def adjust_point_to_db(self, cycle, offset):
9099
"""Adjust a cycle point (with offset) to the DB point format.
91100

tests/integration/test_dbstatecheck.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ async def checker(
5252
schd: Scheduler = mod_scheduler(wid, paused_start=False)
5353
async with mod_run(schd):
5454
await mod_complete(schd)
55-
schd.pool.force_trigger_tasks(['1000/good'], [2])
55+
schd.pool.force_trigger_tasks(['1000/good'], ['2'])
5656
# Allow a cycle of the main loop to pass so that flow 2 can be
5757
# added to db
5858
await sleep(1)
59-
yield CylcWorkflowDBChecker(
59+
with CylcWorkflowDBChecker(
6060
'somestring', 'utterbunkum', schd.workflow_db_mgr.pub_path
61-
)
61+
) as _checker:
62+
yield _checker
6263

6364

6465
def test_basic(checker):

tests/unit/test_db_compat.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,13 @@ def test_cylc_7_db_wflow_params_table(_setup_db):
129129
rf'("cycle_point_format", "{ptformat}")'
130130
)
131131
db_file_name = _setup_db([create, insert])
132-
checker = CylcWorkflowDBChecker('foo', 'bar', db_path=db_file_name)
132+
with CylcWorkflowDBChecker('foo', 'bar', db_path=db_file_name) as checker:
133+
with pytest.raises(
134+
sqlite3.OperationalError, match="no such table: workflow_params"
135+
):
136+
checker._get_db_point_format()
133137

134-
with pytest.raises(
135-
sqlite3.OperationalError, match="no such table: workflow_params"
136-
):
137-
checker._get_db_point_format()
138-
139-
assert checker.db_point_fmt == ptformat
138+
assert checker.db_point_fmt == ptformat
140139

141140

142141
def test_pre_830_task_action_timers(_setup_db):

0 commit comments

Comments
 (0)