Debian Codegraph @

tests/i/test_flow_assignment.py

raw
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 """Test for flow-assignment in triggered/set tasks."""
18
19 import functools
20 import logging
21 import time
22 from typing import Callable
23
24 import pytest
25
26 from cylc.flow.flow_mgr import (
27 FLOW_NEW,
28 FLOW_NONE,
29 )
30 from cylc.flow.scheduler import Scheduler
31 from cylc.flow.commands import (
32 run_cmd,
33 force_trigger_tasks,
34 set_prereqs_and_outputs,
35 )
36
37
38 async def test_trigger_no_flows(one, start):
39 """Test triggering a task with no flows present.
40
41 It should get the flow numbers of the most recent active tasks.
42 """
43 async with start(one):
44
45 # Remove the task (flow 1) --> pool empty
46 task = one.pool.get_tasks()[0]
47 one.pool.remove(task)
48 assert len(one.pool.get_tasks()) == 0
49
50 # Trigger the task, with new flow nums.
51 time.sleep(2) # The flows need different timestamps!
52 await run_cmd(
53 force_trigger_tasks(one, [task.identity], flow=['5', '9']))
54 assert len(one.pool.get_tasks()) == 1
55
56 # Ensure the new flow is in the db.
57 one.pool.workflow_db_mgr.process_queued_ops()
58
59 # Remove the task --> pool empty
60 task = one.pool.get_tasks()[0]
61 one.pool.remove(task)
62 assert len(one.pool.get_tasks()) == 0
63
64 # Trigger the task; it should get flow nums 5, 9
65 await run_cmd(force_trigger_tasks(one, [task.identity], []))
66 assert len(one.pool.get_tasks()) == 1
67 task = one.pool.get_tasks()[0]
68 assert task.flow_nums == {5, 9}
69
70
71 async def test_cli_to_flow_nums(one: Scheduler, start):
72 """Test the flow manager cli_to_flow_nums method."""
73 async with start(one):
74 # flow 1 is already present
75 task = one.pool.get_tasks()[0]
76 assert one.pool.flow_mgr.cli_to_flow_nums([FLOW_NEW]) == {2}
77 one.pool.merge_flows(task, {2})
78 # now we have flows {1, 2}:
79
80 assert one.pool.flow_mgr.cli_to_flow_nums([FLOW_NONE]) == set()
81 assert one.pool.flow_mgr.cli_to_flow_nums([]) == set()
82 assert one.pool.flow_mgr.cli_to_flow_nums([FLOW_NEW]) == {3}
83 assert one.pool.flow_mgr.cli_to_flow_nums(['4', '5']) == {4, 5}
84
85
86 @pytest.mark.parametrize('command', ['trigger', 'set'])
87 async def test_flow_assignment(
88 flow, scheduler, start, command: str, log_filter: Callable
89 ):
90 """Test flow assignment when triggering/setting tasks.
91
92 Active tasks:
93 By default keep existing flows, else merge with requested flows.
94 Inactive tasks:
95 By default assign active flows; else assign requested flows.
96
97 """
98 conf = {
99 'scheduler': {
100 'allow implicit tasks': 'True'
101 },
102 'scheduling': {
103 'graph': {
104 'R1': "foo & bar => a & b & c & d & e"
105 }
106 },
107 'runtime': {
108 'foo': {
109 'outputs': {'x': 'x'}
110 }
111 },
112 }
113 id_ = flow(conf)
114 schd: Scheduler = scheduler(id_, run_mode='simulation', paused_start=True)
115 async with start(schd):
116 if command == 'set':
117 do_command = functools.partial(
118 set_prereqs_and_outputs,
119 schd,
120 outputs=['x'],
121 prerequisites=[],
122 )
123 else:
124 do_command = functools.partial(
125 force_trigger_tasks,
126 schd,
127 )
128
129 active_1, active_2 = schd.pool.get_tasks() # foo, bar in any order
130 schd.pool.merge_flows(
131 active_2, schd.pool.flow_mgr.cli_to_flow_nums([FLOW_NEW]))
132 assert active_1.flow_nums == {1}
133 assert active_2.flow_nums == {1, 2}
134
135 # -----(1. Test active tasks)-----
136
137 await run_cmd(do_command([active_1.identity], flow=[]))
138 if command == "set":
139 # By default active tasks merge existing and active flows.
140 assert active_1.flow_nums == {1, 2}
141 else:
142 # By default active tasks keep existing flows.
143 assert active_1.flow_nums == {1}
144
145 # Else merge existing and requested flows.
146 await run_cmd(do_command([active_1.identity], flow=['2']))
147 assert active_1.flow_nums == {1, 2}
148
149 # (no-flow is ignored for active tasks)
150 await run_cmd(do_command([active_1.identity], flow=[FLOW_NONE]))
151 assert active_1.flow_nums == {1, 2}
152 assert log_filter(
153 contains=("Already active - ignoring"),
154 level=logging.WARNING
155 )
156
157 await run_cmd(do_command([active_1.identity], flow=[FLOW_NEW]))
158 assert active_1.flow_nums == {1, 2, 3}
159
160 # -----(2. Test inactive tasks)-----
161 if command == 'set':
162 do_command = functools.partial(
163 set_prereqs_and_outputs,
164 schd,
165 outputs=[],
166 prerequisites=['all'],
167 )
168
169 # By default inactive tasks get all active flows.
170 await run_cmd(do_command(['1/a'], flow=[]))
171 assert schd.pool._get_task_by_id('1/a').flow_nums == {1, 2, 3}
172
173 # Else assign requested flows.
174 # Run as no-flow:
175 await run_cmd(do_command(['1/b'], flow=[FLOW_NONE]))
176 assert schd.pool._get_task_by_id('1/b').flow_nums == set()
177
178 await run_cmd(do_command(['1/c'], flow=[FLOW_NEW]))
179 assert schd.pool._get_task_by_id('1/c').flow_nums == {4}
180
181 await run_cmd(do_command(['1/d'], flow=[]))
182 assert schd.pool._get_task_by_id('1/d').flow_nums == {1, 2, 3, 4}
183
184 await run_cmd(do_command(['1/e'], flow=["7"]))
185 assert schd.pool._get_task_by_id('1/e').flow_nums == {7}