| 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 |
|
| 18 |
import pytest |
| 19 |
|
| 20 |
from cylc.flow import commands |
| 21 |
from cylc.flow.scheduler import Scheduler |
| 22 |
|
| 23 |
|
| 24 |
@pytest.fixture |
| 25 |
def param_workflow(flow, scheduler): |
| 26 |
def _schd(paused_start, queue_limit): |
| 27 |
id_ = flow({ |
| 28 |
'task parameters': { |
| 29 |
'x': '1..10', |
| 30 |
}, |
| 31 |
'scheduling': { |
| 32 |
'queues': { |
| 33 |
'default': { |
| 34 |
'limit': queue_limit, |
| 35 |
}, |
| 36 |
}, |
| 37 |
'graph': { |
| 38 |
'R1': ''' |
| 39 |
<x> |
| 40 |
''', |
| 41 |
}, |
| 42 |
}, |
| 43 |
}) |
| 44 |
return scheduler(id_, paused_start=paused_start) |
| 45 |
return _schd |
| 46 |
|
| 47 |
|
| 48 |
@pytest.mark.parametrize( |
| 49 |
'start_paused,queue_limit', |
| 50 |
[ |
| 51 |
(False, 1), |
| 52 |
(False, 5), |
| 53 |
(True, 1), |
| 54 |
(True, 5), |
| 55 |
] |
| 56 |
) |
| 57 |
async def test_queue_release( |
| 58 |
param_workflow, |
| 59 |
start, |
| 60 |
capture_submission, |
| 61 |
start_paused, |
| 62 |
queue_limit, |
| 63 |
): |
| 64 |
"""Tasks should be released up to the limit if the scheduler is not paused. |
| 65 |
|
| 66 |
When the scheudler is paused the scheduler should not release tasks from |
| 67 |
queues because tasks may subsequently be held by the user and we don't |
| 68 |
want them clogging up the queue limit. |
| 69 |
|
| 70 |
https://github.com/cylc/cylc-flow/issues/4627 |
| 71 |
""" |
| 72 |
expected_submissions = queue_limit if not start_paused else 0 |
| 73 |
|
| 74 |
# start the scheduler (but don't set the main loop running) |
| 75 |
schd: Scheduler = param_workflow(start_paused, queue_limit) |
| 76 |
async with start(schd): |
| 77 |
# capture task submissions (prevents real submissions) |
| 78 |
submitted_tasks = capture_submission(schd) |
| 79 |
|
| 80 |
# release runahead/queued tasks |
| 81 |
# (if scheduler is paused we should not have any submissions) |
| 82 |
# (otherwise a number of tasks up to the limit should be released) |
| 83 |
schd.pool.release_runahead_tasks() |
| 84 |
schd.release_tasks_to_run() |
| 85 |
assert len(submitted_tasks) == expected_submissions |
| 86 |
|
| 87 |
for _ in range(3): |
| 88 |
# release runahead/queued tasks |
| 89 |
# (no further tasks should be released) |
| 90 |
schd.release_tasks_to_run() |
| 91 |
assert len(submitted_tasks) == expected_submissions |
| 92 |
|
| 93 |
|
| 94 |
async def test_queue_held_tasks( |
| 95 |
param_workflow, |
| 96 |
start, |
| 97 |
capture_submission |
| 98 |
): |
| 99 |
"""Held tasks should not be released from queues. |
| 100 |
|
| 101 |
Users can hold tasks whilst they are queued. These held tasks should not |
| 102 |
be released from their queues. |
| 103 |
|
| 104 |
https://github.com/cylc/cylc-flow/issues/4628 |
| 105 |
""" |
| 106 |
schd: Scheduler = param_workflow(paused_start=True, queue_limit=1) |
| 107 |
|
| 108 |
async with start(schd): |
| 109 |
# capture task submissions (prevents real submissions) |
| 110 |
submitted_tasks = capture_submission(schd) |
| 111 |
|
| 112 |
# release runahead tasks to their queues |
| 113 |
schd.pool.release_runahead_tasks() |
| 114 |
|
| 115 |
# hold all tasks and resume the workflow |
| 116 |
# (nothing should have run yet because the workflow started paused) |
| 117 |
await commands.run_cmd(commands.hold(schd, ['*/*'])) |
| 118 |
schd.resume_workflow() |
| 119 |
|
| 120 |
# release queued tasks |
| 121 |
# (no tasks should be released from the queues because they are held) |
| 122 |
schd.release_tasks_to_run() |
| 123 |
assert len(submitted_tasks) == 0 |
| 124 |
|
| 125 |
# un-hold tasks |
| 126 |
await commands.run_cmd(commands.release(schd, ['*/*'])) |
| 127 |
|
| 128 |
# release queued tasks |
| 129 |
# (tasks should now be released from the queues) |
| 130 |
schd.release_tasks_to_run() |
| 131 |
assert len(submitted_tasks) == 1 |