| 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 |
from unittest.mock import MagicMock |
| 18 |
import pytest |
| 19 |
from types import SimpleNamespace |
| 20 |
|
| 21 |
from cylc.flow.prerequisite import Prerequisite |
| 22 |
from cylc.flow.taskdef import TaskDef |
| 23 |
from cylc.flow.cycling.integer import IntegerSequence, IntegerPoint |
| 24 |
from cylc.flow.run_modes import RunMode, disable_task_event_handlers |
| 25 |
from cylc.flow.task_trigger import Dependency, TaskTrigger |
| 26 |
from cylc.flow.task_state import ( |
| 27 |
TaskState, |
| 28 |
TASK_STATUS_PREPARING, |
| 29 |
TASK_STATUS_SUBMIT_FAILED, |
| 30 |
TASK_STATUS_SUBMITTED, |
| 31 |
TASK_STATUS_SUCCEEDED, |
| 32 |
TASK_STATUS_WAITING, |
| 33 |
TASK_STATUS_RUNNING, |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.parametrize( |
| 38 |
'state,is_held', |
| 39 |
[ |
| 40 |
(TASK_STATUS_WAITING, True), |
| 41 |
(TASK_STATUS_SUCCEEDED, False) |
| 42 |
] |
| 43 |
) |
| 44 |
def test_state_comparison(state, is_held): |
| 45 |
"""Test the __call__ method.""" |
| 46 |
tdef = TaskDef('foo', {}, '123', '123') |
| 47 |
tstate = TaskState(tdef, '123', state, is_held) |
| 48 |
|
| 49 |
assert tstate(state, is_held=is_held) |
| 50 |
assert tstate(state) |
| 51 |
assert tstate(is_held=is_held) |
| 52 |
assert tstate(state, 'of', 'flux') |
| 53 |
assert tstate(state, 'of', 'flux', is_held=is_held) |
| 54 |
|
| 55 |
assert not tstate(state + 'x', is_held=not is_held) |
| 56 |
assert not tstate(state, is_held=not is_held) |
| 57 |
assert not tstate(state + 'x', is_held=is_held) |
| 58 |
assert not tstate(state + 'x') |
| 59 |
assert not tstate(is_held=not is_held) |
| 60 |
assert not tstate(state + 'x', 'of', 'flux') |
| 61 |
|
| 62 |
|
| 63 |
@pytest.mark.parametrize( |
| 64 |
'state,is_held,should_reset', |
| 65 |
[ |
| 66 |
(None, None, False), |
| 67 |
(TASK_STATUS_WAITING, None, False), |
| 68 |
(None, True, False), |
| 69 |
(TASK_STATUS_WAITING, True, False), |
| 70 |
(TASK_STATUS_SUCCEEDED, None, True), |
| 71 |
(None, False, True), |
| 72 |
(TASK_STATUS_WAITING, False, True), |
| 73 |
] |
| 74 |
) |
| 75 |
def test_reset(state, is_held, should_reset): |
| 76 |
"""Test that tasks do or don't have their state changed.""" |
| 77 |
tdef = TaskDef('foo', {}, '123', '123') |
| 78 |
# create task state: |
| 79 |
# * status: waiting |
| 80 |
# * is_held: true |
| 81 |
tstate = TaskState(tdef, '123', TASK_STATUS_WAITING, True) |
| 82 |
assert tstate.reset(state, is_held) == should_reset |
| 83 |
if is_held is not None: |
| 84 |
assert tstate.is_held == is_held |
| 85 |
if state is not None: |
| 86 |
assert tstate.status == state |
| 87 |
|
| 88 |
|
| 89 |
def test_task_prereq_duplicates(set_cycling_type): |
| 90 |
"""Test prerequisite duplicates from multiple recurrences are discarded.""" |
| 91 |
|
| 92 |
set_cycling_type() |
| 93 |
|
| 94 |
seq1 = IntegerSequence('R1', "1") |
| 95 |
seq2 = IntegerSequence('R/1/P1', "1") |
| 96 |
|
| 97 |
trig = TaskTrigger('a', "1", 'succeeded', None, None, None, None) |
| 98 |
|
| 99 |
dep = Dependency([trig], [trig], False) |
| 100 |
|
| 101 |
tdef = TaskDef('foo', {}, IntegerPoint("1"), IntegerPoint("1")) |
| 102 |
tdef.add_dependency(dep, seq1) |
| 103 |
tdef.add_dependency(dep, seq2) # duplicate! |
| 104 |
|
| 105 |
tstate = TaskState(tdef, IntegerPoint("1"), TASK_STATUS_WAITING, False) |
| 106 |
|
| 107 |
prereqs = [p._satisfied for p in tstate.prerequisites] |
| 108 |
|
| 109 |
assert prereqs == [{("1", "a", "succeeded"): False}] |
| 110 |
|
| 111 |
|
| 112 |
def test_task_state_order(): |
| 113 |
"""Test is_gt and is_gte methods.""" |
| 114 |
|
| 115 |
tdef = TaskDef('foo', {}, IntegerPoint("1"), IntegerPoint("1")) |
| 116 |
tstate = TaskState(tdef, IntegerPoint("1"), TASK_STATUS_SUBMITTED, False) |
| 117 |
|
| 118 |
assert tstate.is_gt(TASK_STATUS_WAITING) |
| 119 |
assert tstate.is_gt(TASK_STATUS_PREPARING) |
| 120 |
assert tstate.is_gt(TASK_STATUS_SUBMIT_FAILED) |
| 121 |
assert not tstate.is_gt(TASK_STATUS_SUBMITTED) |
| 122 |
assert tstate.is_gte(TASK_STATUS_SUBMITTED) |
| 123 |
assert not tstate.is_gt(TASK_STATUS_RUNNING) |
| 124 |
assert not tstate.is_gte(TASK_STATUS_RUNNING) |
| 125 |
|
| 126 |
|
| 127 |
def test_get_resolved_dependencies(): |
| 128 |
prereq1 = Prerequisite(IntegerPoint('2')) |
| 129 |
prereq1[('1', 'a', 'x')] = True |
| 130 |
prereq1[('1', 'b', 'x')] = False |
| 131 |
prereq1[('1', 'c', 'x')] = 'satisfied from database' |
| 132 |
prereq1[('1', 'd', 'x')] = 'force satisfied' |
| 133 |
prereq2 = Prerequisite(IntegerPoint('2')) |
| 134 |
prereq2[('1', 'e', 'succeeded')] = False |
| 135 |
prereq2[('1', 'e', 'failed')] = True |
| 136 |
task_state = TaskState( |
| 137 |
MagicMock(), IntegerPoint('2'), TASK_STATUS_WAITING, False |
| 138 |
) |
| 139 |
task_state.prerequisites = [prereq1, prereq2] |
| 140 |
assert task_state.get_resolved_dependencies() == [ |
| 141 |
'1/a', |
| 142 |
'1/c', |
| 143 |
'1/d', |
| 144 |
'1/e', |
| 145 |
] |
| 146 |
|
| 147 |
|
| 148 |
@pytest.mark.parametrize( |
| 149 |
'itask_run_mode, disable_handlers, expect', |
| 150 |
( |
| 151 |
('live', True, False), |
| 152 |
('live', False, False), |
| 153 |
('dummy', True, False), |
| 154 |
('dummy', False, False), |
| 155 |
('simulation', True, True), |
| 156 |
('simulation', False, True), |
| 157 |
('skip', True, True), |
| 158 |
('skip', False, False), |
| 159 |
) |
| 160 |
) |
| 161 |
def test_disable_task_event_handlers(itask_run_mode, disable_handlers, expect): |
| 162 |
"""Conditions under which task event handlers should not be used. |
| 163 |
""" |
| 164 |
# Construct a fake itask object: |
| 165 |
itask = SimpleNamespace( |
| 166 |
run_mode=RunMode(itask_run_mode), |
| 167 |
platform={'disable task event handlers': disable_handlers}, |
| 168 |
tdef=SimpleNamespace( |
| 169 |
rtconfig={ |
| 170 |
'skip': {'disable task event handlers': disable_handlers}}) |
| 171 |
) |
| 172 |
# Check method: |
| 173 |
assert disable_task_event_handlers(itask) is expect |