| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
# mypy: disable-error-code=union-attr |
| 18 |
|
| 19 |
"""Test interactions with sequential xtriggers.""" |
| 20 |
|
| 21 |
from unittest.mock import patch |
| 22 |
import pytest |
| 23 |
|
| 24 |
from cylc.flow.commands import ( |
| 25 |
run_cmd, |
| 26 |
force_trigger_tasks, |
| 27 |
remove_tasks, |
| 28 |
set_prereqs_and_outputs |
| 29 |
) |
| 30 |
from cylc.flow.cycling.integer import IntegerPoint |
| 31 |
from cylc.flow.cycling.iso8601 import ISO8601Point |
| 32 |
from cylc.flow.exceptions import XtriggerConfigError |
| 33 |
from cylc.flow.id import TaskTokens |
| 34 |
from cylc.flow.scheduler import Scheduler |
| 35 |
|
| 36 |
|
| 37 |
def list_cycles(schd: Scheduler): |
| 38 |
"""List the task instance cycle points present in the pool.""" |
| 39 |
return sorted(itask.tokens['cycle'] for itask in schd.pool.get_tasks()) |
| 40 |
|
| 41 |
|
| 42 |
@pytest.fixture() |
| 43 |
def sequential(flow, scheduler): |
| 44 |
id_ = flow({ |
| 45 |
'scheduler': { |
| 46 |
'cycle point format': 'CCYY', |
| 47 |
}, |
| 48 |
'scheduling': { |
| 49 |
'runahead limit': 'P2', |
| 50 |
'initial cycle point': '2000', |
| 51 |
'graph': { |
| 52 |
'P1Y': '@wall_clock => foo', |
| 53 |
} |
| 54 |
} |
| 55 |
}) |
| 56 |
return scheduler(id_) |
| 57 |
|
| 58 |
|
| 59 |
async def test_remove(sequential: Scheduler, start): |
| 60 |
"""It should spawn the next instance when a task is removed. |
| 61 |
|
| 62 |
Ensure that removing a task with a sequential xtrigger does not break the |
| 63 |
chain causing future instances to be removed from the workflow. |
| 64 |
""" |
| 65 |
async with start(sequential): |
| 66 |
# starts with the first task in the first cycle |
| 67 |
assert list_cycles(sequential) == ['2000'] |
| 68 |
# removing it should spawn the next sequential instance |
| 69 |
await run_cmd(remove_tasks(sequential, ['2000'], ["1"])) |
| 70 |
assert list_cycles(sequential) == ['2001'] |
| 71 |
|
| 72 |
|
| 73 |
async def test_trigger(sequential, start): |
| 74 |
"""It should spawn its next instance if triggered ahead of time. |
| 75 |
|
| 76 |
If you manually trigger a sequentially spawned task before its xtriggers |
| 77 |
have become satisfied, then the sequential spawning chain is broken. |
| 78 |
|
| 79 |
The task pool should defend against this to ensure that triggering a task |
| 80 |
doesn't cancel it's future instances. |
| 81 |
""" |
| 82 |
async with start(sequential): |
| 83 |
assert list_cycles(sequential) == ['2000'] |
| 84 |
|
| 85 |
foo1 = sequential.pool.get_task(ISO8601Point('2000'), 'foo') |
| 86 |
await run_cmd(force_trigger_tasks(sequential, [foo1.identity], ["1"])) |
| 87 |
|
| 88 |
await sequential._main_loop() |
| 89 |
assert list_cycles(sequential) == ['2001'] |
| 90 |
|
| 91 |
foo2 = sequential.pool.get_task(ISO8601Point('2001'), 'foo') |
| 92 |
foo2.state_reset(is_runahead=False) |
| 93 |
await run_cmd( |
| 94 |
set_prereqs_and_outputs(sequential, [foo2.identity], ["1"])) |
| 95 |
|
| 96 |
assert list_cycles(sequential) == ['2002'] |
| 97 |
|
| 98 |
|
| 99 |
async def test_set_outputs(sequential, start): |
| 100 |
"""It should spawn its next instance if outputs are set ahead of time. |
| 101 |
|
| 102 |
If you set outputs of a sequentially spawned task before its xtriggers |
| 103 |
have become satisfied, then the sequential spawning chain is broken. |
| 104 |
|
| 105 |
The task pool should defend against this to ensure that setting outputs |
| 106 |
doesn't cancel it's future instances and their downstream tasks. |
| 107 |
""" |
| 108 |
async with start(sequential): |
| 109 |
assert list_cycles(sequential) == ['2000'] |
| 110 |
|
| 111 |
sequential.pool.get_task(ISO8601Point('2000'), 'foo') |
| 112 |
# set foo:succeeded it should spawn next instance |
| 113 |
sequential.pool.set_prereqs_and_outputs( |
| 114 |
{TaskTokens('2000', 'foo')}, ["succeeded"], [], []) |
| 115 |
|
| 116 |
assert list_cycles(sequential) == ['2001'] |
| 117 |
|
| 118 |
|
| 119 |
async def test_set_prereqs(sequential, start): |
| 120 |
"""It should spawn next after manual xtrigger prereq satisfaction.""" |
| 121 |
async with start(sequential): |
| 122 |
assert list_cycles(sequential) == ['2000'] |
| 123 |
|
| 124 |
sequential.pool.get_task(ISO8601Point('2000'), 'foo') |
| 125 |
# satisfy foo's xtriggers - it should spawn next instance |
| 126 |
sequential.pool.set_prereqs_and_outputs( |
| 127 |
{TaskTokens('2000', 'foo')}, [], ['xtrigger/all:succeeded'], []) |
| 128 |
await sequential._main_loop() |
| 129 |
|
| 130 |
assert list_cycles(sequential) == ['2000', '2001'] |
| 131 |
|
| 132 |
|
| 133 |
async def test_reload(sequential, start): |
| 134 |
"""It should set the is_xtrigger_sequential flag on reload. |
| 135 |
|
| 136 |
TODO: test that changes to the sequential status in the config get picked |
| 137 |
up on reload |
| 138 |
""" |
| 139 |
async with start(sequential): |
| 140 |
# the task should be marked as sequential |
| 141 |
pre_reload = sequential.pool.get_task(ISO8601Point('2000'), 'foo') |
| 142 |
assert pre_reload.is_xtrigger_sequential is True |
| 143 |
|
| 144 |
# reload the workflow |
| 145 |
sequential.pool.reload(sequential.config) |
| 146 |
|
| 147 |
# the original task proxy should have been replaced |
| 148 |
post_reload = sequential.pool.get_task(ISO8601Point('2000'), 'foo') |
| 149 |
assert id(pre_reload) != id(post_reload) |
| 150 |
|
| 151 |
# the new task should be marked as sequential |
| 152 |
assert post_reload.is_xtrigger_sequential is True |
| 153 |
|
| 154 |
|
| 155 |
@pytest.mark.parametrize('is_sequential', [True, False]) |
| 156 |
@pytest.mark.parametrize('xtrig_def', [ |
| 157 |
'wall_clock(sequential={})', |
| 158 |
'wall_clock(PT1H, sequential={})', |
| 159 |
'xrandom(1, 1, sequential={})', |
| 160 |
]) |
| 161 |
async def test_sequential_arg_ok( |
| 162 |
flow, scheduler, start, xtrig_def: str, is_sequential: bool |
| 163 |
): |
| 164 |
"""Test passing the sequential argument to xtriggers.""" |
| 165 |
wid = flow({ |
| 166 |
'scheduler': { |
| 167 |
'cycle point format': 'CCYY', |
| 168 |
}, |
| 169 |
'scheduling': { |
| 170 |
'initial cycle point': '2000', |
| 171 |
'runahead limit': 'P1', |
| 172 |
'xtriggers': { |
| 173 |
'myxt': xtrig_def.format(is_sequential), |
| 174 |
}, |
| 175 |
'graph': { |
| 176 |
'P1Y': '@myxt => foo', |
| 177 |
} |
| 178 |
} |
| 179 |
}) |
| 180 |
schd: Scheduler = scheduler(wid) |
| 181 |
expected_num_cycles = 1 if is_sequential else 3 |
| 182 |
async with start(schd): |
| 183 |
itask = schd.pool.get_task(ISO8601Point('2000'), 'foo') |
| 184 |
assert itask.is_xtrigger_sequential is is_sequential |
| 185 |
assert len(list_cycles(schd)) == expected_num_cycles |
| 186 |
|
| 187 |
|
| 188 |
def test_sequential_arg_bad(flow, validate): |
| 189 |
"""Test validation of 'sequential' arg for custom xtrigger function def""" |
| 190 |
wid = flow({ |
| 191 |
'scheduling': { |
| 192 |
'xtriggers': { |
| 193 |
'myxt': 'custom_xt(42)' |
| 194 |
}, |
| 195 |
'graph': { |
| 196 |
'R1': '@myxt => foo' |
| 197 |
} |
| 198 |
} |
| 199 |
}) |
| 200 |
|
| 201 |
def xtrig1(x, sequential): |
| 202 |
"""This uses 'sequential' without a default value""" |
| 203 |
return True |
| 204 |
|
| 205 |
def xtrig2(x, sequential='True'): |
| 206 |
"""This uses 'sequential' with a default of wrong type""" |
| 207 |
return True |
| 208 |
|
| 209 |
for xtrig in (xtrig1, xtrig2): |
| 210 |
with patch( |
| 211 |
'cylc.flow.xtrigger_mgr.get_xtrig_func', |
| 212 |
return_value=xtrig |
| 213 |
): |
| 214 |
with pytest.raises(XtriggerConfigError) as excinfo: |
| 215 |
validate(wid) |
| 216 |
assert ( |
| 217 |
"reserved argument 'sequential' with no boolean default" |
| 218 |
) in str(excinfo.value) |
| 219 |
|
| 220 |
|
| 221 |
def test_sequential_arg_bad2(flow, validate): |
| 222 |
"""Test validation of 'sequential' arg for xtrigger calls""" |
| 223 |
wid = flow({ |
| 224 |
'scheduling': { |
| 225 |
'initial cycle point': '2000', |
| 226 |
'xtriggers': { |
| 227 |
'clock': 'wall_clock(sequential=3)', |
| 228 |
}, |
| 229 |
'graph': { |
| 230 |
'R1': '@clock => foo', |
| 231 |
}, |
| 232 |
}, |
| 233 |
}) |
| 234 |
|
| 235 |
with pytest.raises(XtriggerConfigError) as excinfo: |
| 236 |
validate(wid) |
| 237 |
assert ( |
| 238 |
"invalid argument 'sequential=3' - must be boolean" |
| 239 |
) in str(excinfo.value) |
| 240 |
|
| 241 |
|
| 242 |
@pytest.mark.parametrize('is_sequential', [True, False]) |
| 243 |
async def test_any_sequential(flow, scheduler, start, is_sequential: bool): |
| 244 |
"""Test that a task is marked as sequential if any of its xtriggers are.""" |
| 245 |
wid = flow({ |
| 246 |
'scheduling': { |
| 247 |
'xtriggers': { |
| 248 |
'xt1': 'custom_xt()', |
| 249 |
'xt2': f'custom_xt(sequential={is_sequential})', |
| 250 |
'xt3': 'custom_xt(sequential=False)', |
| 251 |
}, |
| 252 |
'graph': { |
| 253 |
'R1': '@xt1 & @xt2 & @xt3 => foo', |
| 254 |
} |
| 255 |
} |
| 256 |
}) |
| 257 |
|
| 258 |
with patch( |
| 259 |
'cylc.flow.xtrigger_mgr.get_xtrig_func', |
| 260 |
return_value=lambda *a, **k: True |
| 261 |
): |
| 262 |
schd: Scheduler = scheduler(wid) |
| 263 |
async with start(schd): |
| 264 |
itask = schd.pool.get_task(IntegerPoint('1'), 'foo') |
| 265 |
assert itask.is_xtrigger_sequential is is_sequential |
| 266 |
|
| 267 |
|
| 268 |
async def test_override(flow, scheduler, start): |
| 269 |
"""Test that the 'sequential=False' arg can override a default of True.""" |
| 270 |
wid = flow({ |
| 271 |
'scheduling': { |
| 272 |
'sequential xtriggers': True, |
| 273 |
'xtriggers': { |
| 274 |
'xt1': 'custom_xt()', |
| 275 |
'xt2': 'custom_xt(sequential=False)', |
| 276 |
}, |
| 277 |
'graph': { |
| 278 |
'R1': ''' |
| 279 |
@xt1 => foo |
| 280 |
@xt2 => bar |
| 281 |
''', |
| 282 |
} |
| 283 |
} |
| 284 |
}) |
| 285 |
|
| 286 |
with patch( |
| 287 |
'cylc.flow.xtrigger_mgr.get_xtrig_func', |
| 288 |
return_value=lambda *a, **k: True |
| 289 |
): |
| 290 |
schd: Scheduler = scheduler(wid) |
| 291 |
async with start(schd): |
| 292 |
foo = schd.pool.get_task(IntegerPoint('1'), 'foo') |
| 293 |
assert foo.is_xtrigger_sequential is True |
| 294 |
bar = schd.pool.get_task(IntegerPoint('1'), 'bar') |
| 295 |
assert bar.is_xtrigger_sequential is False |