Skip to content

Commit 4851bce

Browse files
authored
Merge pull request #5922 from cylc/8.2.x-sync
🤖 Merge 8.2.x-sync into master
2 parents c07392d + b20a39c commit 4851bce

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

tests/integration/test_xtrigger_mgr.py

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
#
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
"""Tests for the behaviour of xtrigger manager.
17-
"""
16+
"""Tests for the behaviour of xtrigger manager."""
1817

19-
from pytest_mock import mocker
18+
import asyncio
19+
from pathlib import Path
20+
from textwrap import dedent
21+
22+
from cylc.flow.pathutil import get_workflow_run_dir
2023

2124
async def test_2_xtriggers(flow, start, scheduler, monkeypatch):
2225
"""Test that if an itask has 2 wall_clock triggers with different
@@ -118,4 +121,69 @@ async def test_1_xtrigger_2_tasks(flow, start, scheduler, monkeypatch, mocker):
118121
# resulting in two calls to put_xtriggers. This test fails
119122
# on master, but with call count 0 (not 2) because the main
120123
# loop doesn't run in this test.
121-
124+
125+
126+
async def test_xtriggers_restart(flow, start, scheduler, db_select):
127+
"""It should write xtrigger results to the DB and load them on restart."""
128+
# define a workflow which uses a custom xtrigger
129+
id_ = flow({
130+
'scheduler': {
131+
'allow implicit tasks': 'True'
132+
},
133+
'scheduling': {
134+
'xtriggers': {
135+
'mytrig': 'mytrig()'
136+
},
137+
'graph': {
138+
'R1': '@mytrig => foo'
139+
},
140+
}
141+
})
142+
143+
# add a custom xtrigger to the workflow
144+
run_dir = Path(get_workflow_run_dir(id_))
145+
xtrig_dir = run_dir / 'lib/python'
146+
xtrig_dir.mkdir(parents=True)
147+
(xtrig_dir / 'mytrig.py').write_text(dedent('''
148+
from random import random
149+
150+
def mytrig(*args, **kwargs):
151+
# return a different random number each time
152+
return True, {"x": str(random())}
153+
'''))
154+
155+
# start the workflow & run the xtrigger
156+
schd = scheduler(id_)
157+
async with start(schd):
158+
# run all xtriggers
159+
for task in schd.pool.get_tasks():
160+
schd.xtrigger_mgr.call_xtriggers_async(task)
161+
# one xtrigger should have been scheduled to run
162+
assert len(schd.proc_pool.queuings) + len(schd.proc_pool.runnings) == 1
163+
# wait for it to return
164+
for _ in range(50):
165+
await asyncio.sleep(0.1)
166+
schd.proc_pool.process()
167+
if len(schd.proc_pool.runnings) == 0:
168+
break
169+
else:
170+
raise Exception('Process pool did not clear')
171+
172+
# the xtrigger should be written to the DB
173+
db_xtriggers = db_select(schd, True, 'xtriggers')
174+
assert len(db_xtriggers) == 1
175+
assert db_xtriggers[0][0] == 'mytrig()'
176+
assert db_xtriggers[0][1].startswith('{"x":')
177+
178+
# restart the workflow, the xtrigger should *not* run again
179+
schd = scheduler(id_)
180+
async with start(schd):
181+
# run all xtriggers
182+
for task in schd.pool.get_tasks():
183+
schd.xtrigger_mgr.call_xtriggers_async(task)
184+
# the xtrigger should have been loaded from the DB
185+
# (so no xtriggers should be scheduled to run)
186+
assert len(schd.proc_pool.queuings) + len(schd.proc_pool.runnings) == 0
187+
188+
# check the DB to ensure no additional entries have been created
189+
assert db_select(schd, True, 'xtriggers') == db_xtriggers

tests/unit/test_flow_mgr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from cylc.flow.flow_mgr import FlowMgr
2424
from cylc.flow.workflow_db_mgr import WorkflowDatabaseManager
25+
from cylc.flow import CYLC_LOG
2526

2627

2728
FAKE_NOW = datetime.datetime(2020, 12, 25, 17, 5, 55)
@@ -44,7 +45,7 @@ def test_all(
4445
):
4546
db_mgr = WorkflowDatabaseManager()
4647
flow_mgr = FlowMgr(db_mgr)
47-
caplog.set_level(logging.INFO)
48+
caplog.set_level(logging.INFO, CYLC_LOG)
4849

4950
count = 1
5051
meta = "the quick brown fox"

tests/unit/test_id_cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pathlib import Path
2020
import pytest
2121

22+
from cylc.flow import CYLC_LOG
2223
from cylc.flow.async_util import pipe
2324
from cylc.flow.exceptions import InputError, WorkflowFilesError
2425
from cylc.flow.id import detokenise, tokenise, Tokens
@@ -537,7 +538,7 @@ def test_validate_workflow_ids_basic(tmp_run_dir):
537538

538539
def test_validate_workflow_ids_warning(caplog):
539540
"""It should warn when the run number is provided as a cycle point."""
540-
caplog.set_level(logging.WARN)
541+
caplog.set_level(logging.WARN, CYLC_LOG)
541542
_validate_workflow_ids(Tokens('workflow/run1//cycle/task'), src_path='')
542543
assert caplog.messages == []
543544

0 commit comments

Comments
 (0)