-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_execute.py
295 lines (229 loc) · 7.63 KB
/
test_execute.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from __future__ import annotations
import pickle
import textwrap
from time import time
import attr
import pytest
from pytask import cli
from pytask import main
from pytask_parallel.backends import PARALLEL_BACKENDS
from pytask_parallel.execute import DefaultBackendNameSpace
from pytask_parallel.execute import ProcessesNameSpace
@attr.s
class DummyTask:
function = attr.ib()
def execute(self):
self.function()
class Session:
pass
@pytest.mark.end_to_end
@pytest.mark.parametrize("parallel_backend", PARALLEL_BACKENDS)
def test_parallel_execution_speedup(tmp_path, parallel_backend):
source = """
import pytask
import time
@pytask.mark.produces("out_1.txt")
def task_1(produces):
time.sleep(5)
produces.write_text("1")
@pytask.mark.produces("out_2.txt")
def task_2(produces):
time.sleep(5)
produces.write_text("2")
"""
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source))
session = main({"paths": tmp_path})
assert session.exit_code == 0
assert session.execution_end - session.execution_start > 10
tmp_path.joinpath("out_1.txt").unlink()
tmp_path.joinpath("out_2.txt").unlink()
session = main(
{"paths": tmp_path, "n_workers": 2, "parallel_backend": parallel_backend}
)
assert session.exit_code == 0
assert session.execution_end - session.execution_start < 10
@pytest.mark.end_to_end
@pytest.mark.parametrize("parallel_backend", PARALLEL_BACKENDS)
def test_parallel_execution_speedup_w_cli(runner, tmp_path, parallel_backend):
source = """
import pytask
import time
@pytask.mark.produces("out_1.txt")
def task_1(produces):
time.sleep(5)
produces.write_text("1")
@pytask.mark.produces("out_2.txt")
def task_2(produces):
time.sleep(5)
produces.write_text("2")
"""
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source))
start = time()
result = runner.invoke(cli, [tmp_path.as_posix()])
end = time()
assert result.exit_code == 0
assert end - start > 10
tmp_path.joinpath("out_1.txt").unlink()
tmp_path.joinpath("out_2.txt").unlink()
start = time()
result = runner.invoke(
cli,
[
tmp_path.as_posix(),
"--n-workers",
"2",
"--parallel-backend",
parallel_backend,
],
)
end = time()
assert result.exit_code == 0
assert "Started 2 workers." in result.output
assert end - start < 10
@pytest.mark.integration
@pytest.mark.parametrize("parallel_backend", PARALLEL_BACKENDS)
def test_pytask_execute_task_w_processes(parallel_backend):
# Local function which cannot be used with multiprocessing.
def myfunc():
return 1
# Verify it cannot be used with multiprocessing because it cannot be pickled.
with pytest.raises(AttributeError):
pickle.dumps(myfunc)
task = DummyTask(myfunc)
session = Session()
session.config = {
"n_workers": 2,
"parallel_backend": parallel_backend,
"show_locals": False,
}
with PARALLEL_BACKENDS[parallel_backend](
max_workers=session.config["n_workers"]
) as executor:
session.executor = executor
backend_name_space = {
"processes": ProcessesNameSpace,
"threads": DefaultBackendNameSpace,
"loky": DefaultBackendNameSpace,
}[parallel_backend]
future = backend_name_space.pytask_execute_task(session, task)
executor.shutdown()
assert future.result() is None
@pytest.mark.end_to_end
@pytest.mark.parametrize("parallel_backend", PARALLEL_BACKENDS)
def test_parallel_execution_delay(tmp_path, parallel_backend):
source = """
import pytask
@pytask.mark.produces("out_1.txt")
def task_1(produces):
produces.write_text("1")
@pytask.mark.produces("out_2.txt")
def task_2(produces):
produces.write_text("2")
"""
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source))
session = main(
{
"paths": tmp_path,
"delay": 3,
"n_workers": 2,
"parallel_backend": parallel_backend,
}
)
assert 3 < session.execution_end - session.execution_start < 10
@pytest.mark.end_to_end
@pytest.mark.parametrize("parallel_backend", PARALLEL_BACKENDS)
def test_stop_execution_when_max_failures_is_reached(tmp_path, parallel_backend):
source = """
import time
import pytask
def task_1(): time.sleep(1)
def task_2(): time.sleep(2); raise NotImplementedError
@pytask.mark.try_last
def task_3():
time.sleep(3)
"""
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source))
session = main(
{
"paths": tmp_path,
"n_workers": 2,
"parallel_backend": parallel_backend,
"max_failures": 1,
}
)
assert len(session.tasks) == 3
assert len(session.execution_reports) == 2
@pytest.mark.end_to_end
@pytest.mark.parametrize("parallel_backend", PARALLEL_BACKENDS)
def test_task_priorities(tmp_path, parallel_backend):
source = """
import pytask
import time
@pytask.mark.try_first
def task_0():
time.sleep(1)
def task_1():
time.sleep(1)
@pytask.mark.try_last
def task_2():
time.sleep(1)
@pytask.mark.try_first
def task_3():
time.sleep(1)
def task_4():
time.sleep(1)
@pytask.mark.try_last
def task_5():
time.sleep(1)
"""
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source))
session = main(
{"paths": tmp_path, "parallel_backend": parallel_backend, "n_workers": 2}
)
assert session.exit_code == 0
first_task_name = session.execution_reports[0].task.name
assert first_task_name.endswith("task_0") or first_task_name.endswith("task_3")
last_task_name = session.execution_reports[-1].task.name
assert last_task_name.endswith("task_2") or last_task_name.endswith("task_5")
@pytest.mark.end_to_end
@pytest.mark.parametrize("parallel_backend", PARALLEL_BACKENDS)
@pytest.mark.parametrize("show_locals", [True, False])
def test_rendering_of_tracebacks_with_rich(
runner, tmp_path, parallel_backend, show_locals
):
source = """
import pytask
def task_raising_error():
a = list(range(5))
raise Exception
"""
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source))
args = [tmp_path.as_posix(), "-n", "2", "--parallel-backend", parallel_backend]
if show_locals:
args.append("--show-locals")
result = runner.invoke(cli, args)
assert result.exit_code == 1
assert "───── Traceback" in result.output
assert ("───── locals" in result.output) is show_locals
assert ("[0, 1, 2, 3, 4]" in result.output) is show_locals
@pytest.mark.end_to_end
@pytest.mark.parametrize("parallel_backend", PARALLEL_BACKENDS)
def test_generators_are_removed_from_depends_on_produces(tmp_path, parallel_backend):
"""Only works with pytask >=0.1.9."""
source = """
from pathlib import Path
import pytask
@pytask.mark.parametrize("produces", [
((x for x in ["out.txt", "out_2.txt"]),),
["in.txt"],
])
def task_example(produces):
produces = {0: produces} if isinstance(produces, Path) else produces
for p in produces.values():
p.write_text("hihi")
"""
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source))
session = main(
{"paths": tmp_path, "parallel_backend": parallel_backend, "n_workers": 2}
)
assert session.exit_code == 0