| 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 |
import asyncio |
| 18 |
from inspect import signature |
| 19 |
import logging |
| 20 |
from pathlib import Path |
| 21 |
from random import random |
| 22 |
|
| 23 |
import pytest |
| 24 |
|
| 25 |
from cylc.flow.async_util import ( |
| 26 |
pipe, |
| 27 |
asyncqgen, |
| 28 |
scandir, |
| 29 |
) |
| 30 |
|
| 31 |
LOG = logging.getLogger('test') |
| 32 |
|
| 33 |
|
| 34 |
@pipe() |
| 35 |
async def a_range(n): |
| 36 |
for num in range(n): |
| 37 |
LOG.info(f'a_range({n})') |
| 38 |
yield num |
| 39 |
|
| 40 |
|
| 41 |
@pipe |
| 42 |
async def even(x): |
| 43 |
LOG.info(f'even({x})') |
| 44 |
return x % 2 == 0 |
| 45 |
|
| 46 |
|
| 47 |
@pipe |
| 48 |
async def mult(x, y, kwarg='useless kwarg'): |
| 49 |
LOG.info(f'mult{x, y}') |
| 50 |
return x * y |
| 51 |
|
| 52 |
|
| 53 |
@pipe |
| 54 |
async def sleepy(x): |
| 55 |
"""A filter which waits a while then passes.""" |
| 56 |
LOG.info(f'sleepy({x})') |
| 57 |
await asyncio.sleep(0.1) |
| 58 |
return True |
| 59 |
|
| 60 |
|
| 61 |
@pytest.mark.parametrize('preserve_order', (True, False)) |
| 62 |
async def test_pipe(preserve_order): |
| 63 |
"""It passes values through the pipe.""" |
| 64 |
pipe = a_range(5) | even | mult(2) |
| 65 |
pipe.preserve_order = preserve_order |
| 66 |
|
| 67 |
result = [] |
| 68 |
async for num in pipe: |
| 69 |
result.append(num) |
| 70 |
|
| 71 |
assert result == [ |
| 72 |
0, |
| 73 |
4, |
| 74 |
8, |
| 75 |
] |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.parametrize('preserve_order', (True, False)) |
| 79 |
async def test_pipe_single(preserve_order): |
| 80 |
"""It allow single-step pipes.""" |
| 81 |
pipe = a_range(5) |
| 82 |
pipe.preserve_order = preserve_order |
| 83 |
|
| 84 |
result = [] |
| 85 |
async for num in pipe: |
| 86 |
result.append(num) |
| 87 |
|
| 88 |
assert result == [ |
| 89 |
0, |
| 90 |
1, |
| 91 |
2, |
| 92 |
3, |
| 93 |
4 |
| 94 |
] |
| 95 |
|
| 96 |
|
| 97 |
@pytest.mark.parametrize('preserve_order', (True, False)) |
| 98 |
async def test_pipe_reusable(preserve_order): |
| 99 |
"""It can be re-used once depleted.""" |
| 100 |
pipe = a_range(5) | even | mult(2) |
| 101 |
pipe.preserve_order = preserve_order |
| 102 |
|
| 103 |
for _ in range(5): |
| 104 |
result = [] |
| 105 |
async for num in pipe: |
| 106 |
result.append(num) |
| 107 |
|
| 108 |
assert result == [ |
| 109 |
0, |
| 110 |
4, |
| 111 |
8, |
| 112 |
] |
| 113 |
|
| 114 |
|
| 115 |
@pytest.mark.parametrize('preserve_order', (True, False)) |
| 116 |
async def test_pipe_filter_stop(preserve_order): |
| 117 |
"""It yields values early with the filter_stop argument.""" |
| 118 |
pipe = a_range(5) | even(filter_stop=False) |
| 119 |
pipe |= mult(10) |
| 120 |
pipe.preserve_order = preserve_order |
| 121 |
|
| 122 |
result = [] |
| 123 |
async for num in pipe: |
| 124 |
result.append(num) |
| 125 |
|
| 126 |
# the even numbers should be multiplied by 10 |
| 127 |
# the odd numbers should be yielded early (so don't get multiplied) |
| 128 |
assert result == [ |
| 129 |
0, |
| 130 |
1, |
| 131 |
20, |
| 132 |
3, |
| 133 |
40, |
| 134 |
] |
| 135 |
|
| 136 |
|
| 137 |
@pipe |
| 138 |
async def one(x): |
| 139 |
await asyncio.sleep(random() / 5) |
| 140 |
return x |
| 141 |
|
| 142 |
|
| 143 |
@pytest.mark.parametrize('preserve_order', (True, False)) |
| 144 |
async def test_pipe_preserve_order(preserve_order): |
| 145 |
"""It should control result order according to pipe configuration.""" |
| 146 |
n = 50 |
| 147 |
pipe = a_range(n) | one | one | one |
| 148 |
pipe.preserve_order = preserve_order |
| 149 |
|
| 150 |
result = [] |
| 151 |
async for item in pipe: |
| 152 |
result.append(item) |
| 153 |
|
| 154 |
# the odds of getting 50 items in order by chance are pretty slim |
| 155 |
assert (result == list(range(n))) is preserve_order |
| 156 |
|
| 157 |
|
| 158 |
@pytest.mark.parametrize('preserve_order', (True, False)) |
| 159 |
async def test_pipe_concurrent(caplog, preserve_order): |
| 160 |
"""It runs pipes concurrently. |
| 161 |
|
| 162 |
It is easy to make something which appears to be concurrent, this |
| 163 |
test is intended to ensure that it actually IS concurrent. |
| 164 |
|
| 165 |
""" |
| 166 |
pipe = a_range(5) | even | sleepy | mult(2) |
| 167 |
pipe.preserve_order = preserve_order |
| 168 |
|
| 169 |
caplog.set_level(logging.INFO, 'test') |
| 170 |
async for num in pipe: |
| 171 |
pass |
| 172 |
|
| 173 |
order = [ |
| 174 |
# a list of the log messages generated by each step of the pipe |
| 175 |
# as it processes an item |
| 176 |
x[2].split('(')[0] |
| 177 |
for x in caplog.record_tuples |
| 178 |
] |
| 179 |
|
| 180 |
assert 'mult' in order |
| 181 |
assert len(order) == 4 * 4 # 4 steps * 4 items yielded by a_range |
| 182 |
|
| 183 |
# ensure that the steps aren't completed in order (as sync code would) |
| 184 |
# the sleep should ensure this |
| 185 |
# NOTE: not the best test but better than nothing |
| 186 |
assert order != [ |
| 187 |
'a_range', |
| 188 |
'even', |
| 189 |
'sleepy', |
| 190 |
'mult' |
| 191 |
] * 4 |
| 192 |
|
| 193 |
|
| 194 |
def test_pipe_str(): |
| 195 |
"""It has helpful textual representations.""" |
| 196 |
pipe = a_range(5) | even(filter_stop=False) | mult(10, kwarg=42) |
| 197 |
assert str(pipe) == 'a_range(5)' |
| 198 |
assert repr(pipe) == 'a_range(5) | even() | mult(10, kwarg=42)' |
| 199 |
|
| 200 |
|
| 201 |
@pipe() # NOTE: these brackets are what the next function is testing |
| 202 |
async def div(x, y): |
| 203 |
return x / y |
| 204 |
|
| 205 |
|
| 206 |
def test_pipe_brackets(): |
| 207 |
"""Ensure that pipe functions can be declared with or without brackets.""" |
| 208 |
pipe = a_range(5) | div |
| 209 |
assert repr(pipe) == 'a_range(5) | div()' |
| 210 |
|
| 211 |
|
| 212 |
@pipe |
| 213 |
async def documented(x: str, y: int = 0): |
| 214 |
"""The docstring for the pipe function.""" |
| 215 |
pass |
| 216 |
|
| 217 |
|
| 218 |
def test_documentation(): |
| 219 |
"""It should preserve the docstring, signature & annotations of |
| 220 |
the wrapped function.""" |
| 221 |
assert documented.__doc__ == 'The docstring for the pipe function.' |
| 222 |
assert documented.__annotations__ == {'x': str, 'y': int} |
| 223 |
assert str(signature(documented)) == '(x: str, y: int = 0)' |
| 224 |
|
| 225 |
|
| 226 |
def test_rewind(): |
| 227 |
"""It should be possible to move throught the pipe stages.""" |
| 228 |
pipe = a_range | mult | even |
| 229 |
assert pipe.fastforward().rewind() == pipe |
| 230 |
|
| 231 |
|
| 232 |
async def test_asyncqgen(): |
| 233 |
"""It should provide an async gen interface to an async queue.""" |
| 234 |
queue = asyncio.Queue() |
| 235 |
|
| 236 |
gen = asyncqgen(queue) |
| 237 |
|
| 238 |
await queue.put(1) |
| 239 |
await queue.put(2) |
| 240 |
await queue.put(3) |
| 241 |
|
| 242 |
ret = [] |
| 243 |
async for item in gen: |
| 244 |
ret.append(item) |
| 245 |
|
| 246 |
assert ret == [1, 2, 3] |
| 247 |
|
| 248 |
|
| 249 |
async def test_scandir(tmp_path: Path): |
| 250 |
"""It should list directory contents (including symlinks).""" |
| 251 |
(tmp_path / 'a').touch() |
| 252 |
(tmp_path / 'b').touch() |
| 253 |
(tmp_path / 'c').symlink_to(tmp_path / 'b') |
| 254 |
|
| 255 |
assert sorted(await scandir(tmp_path)) == [ |
| 256 |
Path(tmp_path, 'a'), |
| 257 |
Path(tmp_path, 'b'), |
| 258 |
Path(tmp_path, 'c') |
| 259 |
] |
| 260 |
|
| 261 |
|
| 262 |
async def test_scandir_non_exist(tmp_path: Path): |
| 263 |
"""scandir() should raise FileNotFoundError if called on a path that |
| 264 |
doesn't exist.""" |
| 265 |
with pytest.raises(FileNotFoundError): |
| 266 |
await scandir(tmp_path / 'HORSE') |