Skip to content

Commit

Permalink
Fix integration test teardown error
Browse files Browse the repository at this point in the history
If you don't close DB connections when you're done with them, I will scream
  • Loading branch information
MetRonnie committed Jun 17, 2024
1 parent f2fb28d commit 0393a03
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
11 changes: 10 additions & 1 deletion cylc/flow/dbstatecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(self, rund, workflow, db_path=None):
if not os.path.exists(db_path):
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), db_path)

self.conn = sqlite3.connect(db_path, timeout=10.0)
self.conn: sqlite3.Connection = sqlite3.connect(db_path, timeout=10.0)

# Get workflow point format.
try:
Expand All @@ -84,8 +84,17 @@ def __init__(self, rund, workflow, db_path=None):
self.db_point_fmt = self._get_db_point_format_compat()
self.c7_back_compat_mode = True
except sqlite3.OperationalError:
with suppress(Exception):
self.conn.close()
raise exc # original error

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
"""Close DB connection when leaving context manager."""
self.conn.close()

def adjust_point_to_db(self, cycle, offset):
"""Adjust a cycle point (with offset) to the DB point format.
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/test_dbstatecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ async def checker(
schd: Scheduler = mod_scheduler(wid, paused_start=False)
async with mod_run(schd):
await mod_complete(schd)
schd.pool.force_trigger_tasks(['1000/good'], [2])
schd.pool.force_trigger_tasks(['1000/good'], ['2'])
# Allow a cycle of the main loop to pass so that flow 2 can be
# added to db
await sleep(1)
yield CylcWorkflowDBChecker(
with CylcWorkflowDBChecker(
'somestring', 'utterbunkum', schd.workflow_db_mgr.pub_path
)
) as _checker:
yield _checker


def test_basic(checker):
Expand Down
13 changes: 6 additions & 7 deletions tests/unit/test_db_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,13 @@ def test_cylc_7_db_wflow_params_table(_setup_db):
rf'("cycle_point_format", "{ptformat}")'
)
db_file_name = _setup_db([create, insert])
checker = CylcWorkflowDBChecker('foo', 'bar', db_path=db_file_name)
with CylcWorkflowDBChecker('foo', 'bar', db_path=db_file_name) as checker:
with pytest.raises(
sqlite3.OperationalError, match="no such table: workflow_params"
):
checker._get_db_point_format()

with pytest.raises(
sqlite3.OperationalError, match="no such table: workflow_params"
):
checker._get_db_point_format()

assert checker.db_point_fmt == ptformat
assert checker.db_point_fmt == ptformat


def test_pre_830_task_action_timers(_setup_db):
Expand Down

0 comments on commit 0393a03

Please sign in to comment.