/
test_plan.py
407 lines (344 loc) · 13.9 KB
/
test_plan.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#
# Copyright 2020 Two Sigma Open Source, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import operator
import os
import pathlib
import pickle
import re
import tempfile
import networkx as nx
import uberjob
from uberjob._errors import NodeError
from uberjob._util.traceback import get_stack_frame
from uberjob.graph import Call
from uberjob.progress import console_progress, default_progress, html_progress
from .util import UberjobTestCase, copy_with_line_offset
class PlanTestCase(UberjobTestCase):
def test_run_empty_plan(self):
uberjob.run(uberjob.Plan())
def test_failure_to_bind_raises_type_error_and_does_not_alter_plan(self):
def f(a, b=0, *args, c, d=0, **kwargs):
pass
def g(a, b=0, *, c, d=0):
pass
p = uberjob.Plan()
for func, args, kwargs in [
(f, (), {}),
(f, (0,), {}),
(f, (), {"c": 0}),
(g, (0, 0, 0), {}),
(g, (0, 0), {"z": 0}),
]:
with self.subTest(func=func, args=args, kwargs=kwargs):
with self.assert_failed_to_bind():
p.call(func, *args, **kwargs)
self.assertEqual(p.graph.number_of_nodes(), 0)
def test_failed_run(self):
def f(a, b):
raise ValueError("Oops")
p = uberjob.Plan()
x = p.call(f, 2, 3)
with self.assert_call_exception(ValueError, "Oops"):
uberjob.run(p, output=x)
def test_multiple_dependencies_on_single_node(self):
p = uberjob.Plan()
x = p.call(lambda value: value, 1)
y = p.call(lambda *args, **kwargs: (args, kwargs), x, x, a=x, b=x)
p.add_dependency(x, y)
self.assertEqual(uberjob.run(p, output=y), ((1, 1), {"a": 1, "b": 1}))
def test_structured_output(self):
p = uberjob.Plan()
x = p.call(lambda: 1)
y = p.call(lambda n: n + 2, x)
self.assertEqual(uberjob.run(p, output=[x, y]), [1, 3])
self.assertEqual(uberjob.run(p, output=(x, y)), (1, 3))
self.assertEqual(
uberjob.run(p, output={"a": x, "b": [x, y], "c": (1, 2, 3, 4)}),
{"a": 1, "b": [1, 3], "c": (1, 2, 3, 4)},
)
self.assertEqual(
uberjob.run(p, output={(x, y): "a", (3, 4): "b"}),
{(1, 3): "a", (3, 4): "b"},
)
self.assertEqual(uberjob.run(p, output={x, y}), {1, 3})
self.assertEqual(uberjob.run(p, output=7), 7)
self.assertEqual(uberjob.run(p, output=[]), [])
self.assertEqual(uberjob.run(p, output=dict()), dict())
def test_latest_key_wins_in_dict_collisions(self):
p = uberjob.Plan()
x = {"a": 1, "b": 2, "c": 3}
x[p.lit("b")] = 4
x[p.lit("a")] = 5
self.assertEqual(len(x), 5) # Because, e.g., "a" is not lit("a")
self.assertEqual(uberjob.run(p, output=x), {"a": 5, "b": 4, "c": 3})
def test_stack_frame_function(self):
stack_frame1 = get_stack_frame(1)
stack_frame2 = get_stack_frame(1)
self.assertEqual(stack_frame1.path, __file__)
self.assertEqual(stack_frame1.line + 1, stack_frame2.line)
def test_stack_frame_basic(self):
plan = uberjob.Plan()
stack_frame = get_stack_frame(1)
x = plan.call(operator.truediv, 1, 0)
with self.assert_call_exception(
expected_stack_frame=copy_with_line_offset(stack_frame, 1)
):
uberjob.run(plan, output=x)
def test_has_a_cycle(self):
plan = uberjob.Plan()
x = plan.call(operator.add, 1, 2)
y = plan.call(operator.add, x, 4)
plan.add_dependency(y, x)
with self.assertRaises(nx.HasACycle):
uberjob.run(plan, output=y)
def test_plan_type_validation(self):
plan = uberjob.Plan()
with self.assertRaises(TypeError):
plan.call(1)
with self.assertRaises(TypeError):
plan.lit(plan.lit(1))
a = plan.call(operator.add, 1, 2)
with self.assertRaises(TypeError):
plan.add_dependency(a, 1)
with self.assertRaises(TypeError):
plan.add_dependency(1, a)
def test_progress(self):
plan = uberjob.Plan()
x = plan.call(operator.add, 2, 2)
y = plan.call(operator.add, x, 3)
uberjob.run(plan, output=y, progress=None)
uberjob.run(plan, output=y, progress=False)
uberjob.run(plan, output=y, progress=True)
uberjob.run(plan, output=y, progress=default_progress)
uberjob.run(plan, output=y, progress=console_progress)
uberjob.run(plan, output=y, progress=[])
uberjob.run(plan, output=y, progress=[console_progress])
uberjob.run(plan, output=y, progress=[console_progress, console_progress])
with tempfile.TemporaryDirectory() as tempdir:
for path in (
os.path.join(tempdir, "progress.html"),
pathlib.Path(tempdir) / "progress2.html",
):
uberjob.run(plan, output=y, progress=html_progress(path))
with self.assertRaises(TypeError):
uberjob.run(plan, output=y, progress=7)
def test_transform_physical(self):
plan = uberjob.Plan()
y = plan.call(operator.sub, 7, 4)
x = plan.call(pow, 2, y)
def replace_pow_with_add(p, output_node):
nodes = [
node
for node in p.graph.nodes()
if isinstance(node, Call) and node.fn == pow
]
relabeling = {
node: Call(operator.add, stack_frame=node.stack_frame) for node in nodes
}
nx.relabel.relabel_nodes(p.graph, relabeling, copy=False)
return p, relabeling.get(output_node, output_node)
self.assertEqual(
uberjob.run(plan, output=x, transform_physical=replace_pow_with_add), 5
)
def test_scope_error_checking(self):
plan = uberjob.Plan()
with self.assertRaisesRegex(
Exception,
re.escape("Plan scopes must be entered and exited in stack order."),
):
with plan.scope(1):
s = plan.scope(2)
s.__enter__()
def test_add_dependency_error_checking(self):
plan1 = uberjob.Plan()
plan2 = uberjob.Plan()
x = plan1.lit(1)
y = plan2.lit(2)
with self.assertRaisesRegex(KeyError, re.escape("does not contain the node")):
plan1.add_dependency(x, y)
def test_schedulers(self):
plan = uberjob.Plan()
x = plan.call(operator.add, 2, 2)
y = plan.call(operator.add, x, 3)
for scheduler in [None, "default", "random"]:
self.assertEqual(uberjob.run(plan, output=y, scheduler=scheduler), 7)
with self.assertRaises(ValueError):
uberjob.run(plan, output=y, scheduler="fizz")
def test_max_errors(self):
n = 10
count_slot = [0]
def fizz():
count_slot[0] += 1
raise ValueError()
plan = uberjob.Plan()
calls = [plan.call(fizz) for _ in range(n)]
for max_errors in [None, *range(n)]:
count_slot[0] = 0
with self.subTest(max_errors=max_errors):
with self.assertRaises(uberjob.CallError):
uberjob.run(
plan,
output=calls,
max_workers=1,
max_errors=max_errors,
progress=None,
)
self.assertEqual(
count_slot[0], n if max_errors is None else max_errors + 1
)
def test_max_workers_and_max_errors_validation(self):
plan = uberjob.Plan()
for arg_name in ["max_workers", "max_errors", "stale_check_max_workers"]:
for arg_value in ["hello", 1.0]:
with self.subTest(arg_name=arg_name, arg_value=arg_value):
with self.assertRaises(TypeError):
uberjob.run(plan, **{arg_name: arg_value})
for arg_name in ["max_workers", "stale_check_max_workers"]:
for arg_value in [-1, 0]:
with self.subTest(arg_name=arg_name, arg_value=arg_value):
with self.assertRaises(ValueError):
uberjob.run(plan, **{arg_name: arg_value})
with self.assertRaises(ValueError):
uberjob.run(plan, max_errors=-1)
def test_retry_vanilla(self):
count_slot = [0]
def fizz():
count_slot[0] += 1
raise ValueError()
plan = uberjob.Plan()
call = plan.call(fizz)
for retry in [None, 1, 2, 3]:
count_slot[0] = 0
with self.subTest(retry=retry):
with self.assertRaises(uberjob.CallError):
uberjob.run(plan, output=call, retry=retry)
self.assertEqual(count_slot[0], 1 if retry is None else retry)
def test_retry_custom(self):
retry_count_slot = [0]
fizz_count_slot = [0]
def create_retry(attempts):
def inner_retry(f):
def wrapper(*args, **kwargs):
for attempt_index in range(attempts):
retry_count_slot[0] += 1
try:
return f(*args, **kwargs)
except Exception:
is_last_attempt = attempt_index == attempts - 1
if is_last_attempt:
raise
return wrapper
return inner_retry
def fizz(x):
fizz_count_slot[0] += 1
if fizz_count_slot[0] < 5:
raise ValueError()
return x * x
plan = uberjob.Plan()
y = plan.call(fizz, 3)
with self.assertRaises(uberjob.CallError):
uberjob.run(plan, output=y, retry=create_retry(2))
self.assertEqual(retry_count_slot[0], 2)
self.assertEqual(fizz_count_slot[0], 2)
retry_count_slot = [0]
fizz_count_slot = [0]
self.assertEqual(uberjob.run(plan, output=y, retry=create_retry(999)), 9)
self.assertEqual(retry_count_slot[0], 5)
self.assertEqual(fizz_count_slot[0], 5)
def test_retry_custom_receives_args(self):
observed_args = None
observed_kwargs = None
def magic_retry(f):
def wrapper(*args, **kwargs):
nonlocal observed_args
nonlocal observed_kwargs
observed_args = args
observed_kwargs = kwargs
return f(*args, **kwargs)
return wrapper
def fizz(a, b, c, d):
return a + b + c + d
plan = uberjob.Plan()
x = plan.call(fizz, 1, 2, c=3, d=4)
self.assertEqual(uberjob.run(plan, output=x, retry=magic_retry), 10)
self.assertEqual(observed_args, (1, 2))
self.assertEqual(observed_kwargs, {"c": 3, "d": 4})
def test_retry_validation(self):
plan = uberjob.Plan()
with self.assertRaises(TypeError):
uberjob.run(plan, retry="hello")
with self.assertRaises(ValueError):
uberjob.run(plan, retry=0)
with self.assertRaises(ValueError):
uberjob.run(plan, retry=-1)
def test_traceback_manipulation(self):
def x():
raise OSError("buzz")
def y():
try:
return x()
except Exception as e:
raise ValueError("fizz") from e
def z():
return y()
plan = uberjob.Plan()
call = plan.call(z)
with self.assert_call_exception(
expected_exception_chain_traceback_summary=[["z", "y"], ["y", "x"]]
):
uberjob.run(plan, output=call)
def bad_retry1(f):
raise Exception()
with self.assert_call_exception(
expected_exception_chain_traceback_summary=[["bad_retry1"]]
):
uberjob.run(plan, output=call, retry=bad_retry1)
def bad_retry2(f):
def wrapper(*args, **kwargs):
raise ValueError()
return wrapper
with self.assert_call_exception(
expected_exception_chain_traceback_summary=[["wrapper"]]
):
uberjob.run(plan, output=call, retry=bad_retry2)
def test_serialize_call_error(self):
plan = uberjob.Plan()
call = plan.call(operator.truediv, 1, 0)
exception = None
try:
uberjob.run(plan, output=call)
except uberjob.CallError as e:
exception = e
self.assertIsNotNone(exception)
pickled_exception = pickle.dumps(exception)
unpickled_exception = pickle.loads(pickled_exception)
self.assertIsInstance(unpickled_exception, uberjob.CallError)
self.assertIsInstance(unpickled_exception.call, Call)
self.assertIs(unpickled_exception.call.fn, operator.truediv)
def test_serialize_node_error(self):
plan = uberjob.Plan()
call = plan.call(pow, 2, 2)
exception = None
try:
raise NodeError(call)
except NodeError as e:
exception = e
self.assertIsNotNone(exception)
pickled_exception = pickle.dumps(exception)
unpickled_exception = pickle.loads(pickled_exception)
self.assertIsInstance(unpickled_exception, NodeError)
self.assertIsInstance(unpickled_exception.node, Call)
self.assertIs(unpickled_exception.node.fn, pow)