Debian Codegraph @

taskflow/tests/unit/action_engine/test_compile.py

raw
1 # Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 from taskflow import engines
16 from taskflow.engines.action_engine import compiler
17 from taskflow import exceptions as exc
18 from taskflow.patterns import graph_flow as gf
19 from taskflow.patterns import linear_flow as lf
20 from taskflow.patterns import unordered_flow as uf
21 from taskflow import retry
22 from taskflow import test
23 from taskflow.tests import utils as test_utils
24
25
26 def _replicate_graph_with_names(compilation):
27 # Turn a graph of nodes into a graph of names only so that
28 # testing can use those names instead of having to use the exact
29 # node objects themselves (which is problematic for any end nodes that
30 # are added into the graph *dynamically*, and are not there in the
31 # original/source flow).
32 g = compilation.execution_graph
33 n_g = g.__class__(name=g.name)
34 for node, node_data in g.nodes(data=True):
35 n_g.add_node(node.name, attr_dict=node_data)
36 for u, v, u_v_data in g.edges(data=True):
37 n_g.add_edge(u.name, v.name, attr_dict=u_v_data)
38 return n_g
39
40
41 class PatternCompileTest(test.TestCase):
42 def test_task(self):
43 task = test_utils.DummyTask(name='a')
44 g = _replicate_graph_with_names(
45 compiler.PatternCompiler(task).compile())
46 self.assertEqual(['a'], list(g.nodes()))
47 self.assertEqual([], list(g.edges()))
48
49 def test_retry(self):
50 r = retry.AlwaysRevert('r1')
51 self.assertRaises(TypeError, compiler.PatternCompiler(r).compile)
52
53 def test_wrong_object(self):
54 msg_regex = '^Unknown object .* requested to compile'
55 self.assertRaisesRegex(TypeError, msg_regex,
56 compiler.PatternCompiler(42).compile)
57
58 def test_empty(self):
59 flo = lf.Flow("test")
60 compiler.PatternCompiler(flo).compile()
61
62 def test_linear(self):
63 a, b, c, d = test_utils.make_many(4)
64 flo = lf.Flow("test")
65 flo.add(a, b, c)
66 inner_flo = lf.Flow("sub-test")
67 inner_flo.add(d)
68 flo.add(inner_flo)
69
70 g = _replicate_graph_with_names(
71 compiler.PatternCompiler(flo).compile())
72 self.assertEqual(8, len(g))
73
74 order = list(g.topological_sort())
75 self.assertEqual(['test', 'a', 'b', 'c',
76 "sub-test", 'd', "sub-test[$]",
77 'test[$]'], order)
78 self.assertTrue(g.has_edge('c', "sub-test"))
79 self.assertTrue(g.has_edge("sub-test", 'd'))
80 self.assertEqual({'invariant': True},
81 g.get_edge_data("sub-test", 'd'))
82 self.assertEqual(['test[$]'], list(g.no_successors_iter()))
83 self.assertEqual(['test'], list(g.no_predecessors_iter()))
84
85 def test_invalid(self):
86 a, b, c = test_utils.make_many(3)
87 flo = lf.Flow("test")
88 flo.add(a, b, c)
89 flo.add(flo)
90 self.assertRaises(ValueError,
91 compiler.PatternCompiler(flo).compile)
92
93 def test_unordered(self):
94 a, b, c, d = test_utils.make_many(4)
95 flo = uf.Flow("test")
96 flo.add(a, b, c, d)
97
98 g = _replicate_graph_with_names(
99 compiler.PatternCompiler(flo).compile())
100 self.assertEqual(6, len(g))
101 self.assertCountEqual(g.edges(), [
102 ('test', 'a'),
103 ('test', 'b'),
104 ('test', 'c'),
105 ('test', 'd'),
106 ('a', 'test[$]'),
107 ('b', 'test[$]'),
108 ('c', 'test[$]'),
109 ('d', 'test[$]'),
110 ])
111 self.assertEqual({'test'}, set(g.no_predecessors_iter()))
112
113 def test_linear_nested(self):
114 a, b, c, d = test_utils.make_many(4)
115 flo = lf.Flow("test")
116 flo.add(a, b)
117 inner_flo = uf.Flow("test2")
118 inner_flo.add(c, d)
119 flo.add(inner_flo)
120
121 g = _replicate_graph_with_names(
122 compiler.PatternCompiler(flo).compile())
123 self.assertEqual(8, len(g))
124
125 sub_g = g.subgraph(['a', 'b'])
126 self.assertFalse(sub_g.has_edge('b', 'a'))
127 self.assertTrue(sub_g.has_edge('a', 'b'))
128 self.assertEqual({'invariant': True}, sub_g.get_edge_data("a", "b"))
129
130 sub_g = g.subgraph(['c', 'd'])
131 self.assertEqual(0, sub_g.number_of_edges())
132
133 # This ensures that c and d do not start executing until after b.
134 self.assertTrue(g.has_edge('b', 'test2'))
135 self.assertTrue(g.has_edge('test2', 'c'))
136 self.assertTrue(g.has_edge('test2', 'd'))
137
138 def test_unordered_nested(self):
139 a, b, c, d = test_utils.make_many(4)
140 flo = uf.Flow("test")
141 flo.add(a, b)
142 flo2 = lf.Flow("test2")
143 flo2.add(c, d)
144 flo.add(flo2)
145
146 g = _replicate_graph_with_names(
147 compiler.PatternCompiler(flo).compile())
148 self.assertEqual(8, len(g))
149 self.assertCountEqual(g.edges(), [
150 ('test', 'a'),
151 ('test', 'b'),
152 ('test', 'test2'),
153 ('test2', 'c'),
154 ('c', 'd'),
155 ('d', 'test2[$]'),
156 ('test2[$]', 'test[$]'),
157 ('a', 'test[$]'),
158 ('b', 'test[$]'),
159 ])
160
161 def test_unordered_nested_in_linear(self):
162 a, b, c, d = test_utils.make_many(4)
163 inner_flo = uf.Flow('ut').add(b, c)
164 flo = lf.Flow('lt').add(a, inner_flo, d)
165
166 g = _replicate_graph_with_names(
167 compiler.PatternCompiler(flo).compile())
168 self.assertEqual(8, len(g))
169 self.assertCountEqual(g.edges(), [
170 ('lt', 'a'),
171 ('a', 'ut'),
172 ('ut', 'b'),
173 ('ut', 'c'),
174 ('b', 'ut[$]'),
175 ('c', 'ut[$]'),
176 ('ut[$]', 'd'),
177 ('d', 'lt[$]'),
178 ])
179
180 def test_graph(self):
181 a, b, c, d = test_utils.make_many(4)
182 flo = gf.Flow("test")
183 flo.add(a, b, c, d)
184 compilation = compiler.PatternCompiler(flo).compile()
185 self.assertEqual(6, len(compilation.execution_graph))
186 self.assertEqual(8, compilation.execution_graph.number_of_edges())
187
188 def test_graph_nested(self):
189 a, b, c, d, e, f, g = test_utils.make_many(7)
190 flo = gf.Flow("test")
191 flo.add(a, b, c, d)
192
193 flo2 = lf.Flow('test2')
194 flo2.add(e, f, g)
195 flo.add(flo2)
196
197 g = _replicate_graph_with_names(
198 compiler.PatternCompiler(flo).compile())
199 self.assertEqual(11, len(g))
200 self.assertCountEqual(g.edges(), [
201 ('test', 'a'),
202 ('test', 'b'),
203 ('test', 'c'),
204 ('test', 'd'),
205 ('a', 'test[$]'),
206 ('b', 'test[$]'),
207 ('c', 'test[$]'),
208 ('d', 'test[$]'),
209
210 ('test', 'test2'),
211 ('test2', 'e'),
212 ('e', 'f'),
213 ('f', 'g'),
214
215 ('g', 'test2[$]'),
216 ('test2[$]', 'test[$]'),
217 ])
218
219 def test_graph_nested_graph(self):
220 a, b, c, d, e, f, g = test_utils.make_many(7)
221 flo = gf.Flow("test")
222 flo.add(a, b, c, d)
223
224 flo2 = gf.Flow('test2')
225 flo2.add(e, f, g)
226 flo.add(flo2)
227
228 g = _replicate_graph_with_names(
229 compiler.PatternCompiler(flo).compile())
230 self.assertEqual(11, len(g))
231 self.assertCountEqual(g.edges(), [
232 ('test', 'a'),
233 ('test', 'b'),
234 ('test', 'c'),
235 ('test', 'd'),
236 ('test', 'test2'),
237
238 ('test2', 'e'),
239 ('test2', 'f'),
240 ('test2', 'g'),
241
242 ('e', 'test2[$]'),
243 ('f', 'test2[$]'),
244 ('g', 'test2[$]'),
245
246 ('test2[$]', 'test[$]'),
247 ('a', 'test[$]'),
248 ('b', 'test[$]'),
249 ('c', 'test[$]'),
250 ('d', 'test[$]'),
251 ])
252
253 def test_graph_links(self):
254 a, b, c, d = test_utils.make_many(4)
255 flo = gf.Flow("test")
256 flo.add(a, b, c, d)
257 flo.link(a, b)
258 flo.link(b, c)
259 flo.link(c, d)
260
261 g = _replicate_graph_with_names(
262 compiler.PatternCompiler(flo).compile())
263 self.assertEqual(6, len(g))
264 self.assertCountEqual(g.edges(data=True), [
265 ('test', 'a', {'invariant': True}),
266 ('a', 'b', {'manual': True}),
267 ('b', 'c', {'manual': True}),
268 ('c', 'd', {'manual': True}),
269 ('d', 'test[$]', {'invariant': True}),
270 ])
271 self.assertCountEqual(['test'], g.no_predecessors_iter())
272 self.assertCountEqual(['test[$]'], g.no_successors_iter())
273
274 def test_graph_dependencies(self):
275 a = test_utils.ProvidesRequiresTask('a', provides=['x'], requires=[])
276 b = test_utils.ProvidesRequiresTask('b', provides=[], requires=['x'])
277 flo = gf.Flow("test").add(a, b)
278
279 g = _replicate_graph_with_names(
280 compiler.PatternCompiler(flo).compile())
281 self.assertEqual(4, len(g))
282 self.assertCountEqual(g.edges(data=True), [
283 ('test', 'a', {'invariant': True}),
284 ('a', 'b', {'reasons': {'x'}}),
285 ('b', 'test[$]', {'invariant': True}),
286 ])
287 self.assertCountEqual(['test'], g.no_predecessors_iter())
288 self.assertCountEqual(['test[$]'], g.no_successors_iter())
289
290 def test_graph_nested_requires(self):
291 a = test_utils.ProvidesRequiresTask('a', provides=['x'], requires=[])
292 b = test_utils.ProvidesRequiresTask('b', provides=[], requires=[])
293 c = test_utils.ProvidesRequiresTask('c', provides=[], requires=['x'])
294 inner_flo = lf.Flow("test2").add(b, c)
295 flo = gf.Flow("test").add(a, inner_flo)
296
297 g = _replicate_graph_with_names(
298 compiler.PatternCompiler(flo).compile())
299 self.assertEqual(7, len(g))
300 self.assertCountEqual(g.edges(data=True), [
301 ('test', 'a', {'invariant': True}),
302 ('test2', 'b', {'invariant': True}),
303 ('a', 'test2', {'reasons': {'x'}}),
304 ('b', 'c', {'invariant': True}),
305 ('c', 'test2[$]', {'invariant': True}),
306 ('test2[$]', 'test[$]', {'invariant': True}),
307 ])
308 self.assertCountEqual(['test'], list(g.no_predecessors_iter()))
309 self.assertCountEqual(['test[$]'], list(g.no_successors_iter()))
310
311 def test_graph_nested_provides(self):
312 a = test_utils.ProvidesRequiresTask('a', provides=[], requires=['x'])
313 b = test_utils.ProvidesRequiresTask('b', provides=['x'], requires=[])
314 c = test_utils.ProvidesRequiresTask('c', provides=[], requires=[])
315 inner_flo = lf.Flow("test2").add(b, c)
316 flo = gf.Flow("test").add(a, inner_flo)
317
318 g = _replicate_graph_with_names(
319 compiler.PatternCompiler(flo).compile())
320 self.assertEqual(7, len(g))
321 self.assertCountEqual(g.edges(data=True), [
322 ('test', 'test2', {'invariant': True}),
323 ('a', 'test[$]', {'invariant': True}),
324
325 # The 'x' requirement is produced out of test2...
326 ('test2[$]', 'a', {'reasons': {'x'}}),
327
328 ('test2', 'b', {'invariant': True}),
329 ('b', 'c', {'invariant': True}),
330 ('c', 'test2[$]', {'invariant': True}),
331 ])
332 self.assertCountEqual(['test'], g.no_predecessors_iter())
333 self.assertCountEqual(['test[$]'], g.no_successors_iter())
334
335 def test_empty_flow_in_linear_flow(self):
336 flo = lf.Flow('lf')
337 a = test_utils.ProvidesRequiresTask('a', provides=[], requires=[])
338 b = test_utils.ProvidesRequiresTask('b', provides=[], requires=[])
339 empty_flo = gf.Flow("empty")
340 flo.add(a, empty_flo, b)
341
342 g = _replicate_graph_with_names(
343 compiler.PatternCompiler(flo).compile())
344 self.assertCountEqual(g.edges(), [
345 ("lf", "a"),
346 ("a", "empty"),
347 ("empty", "empty[$]"),
348 ("empty[$]", "b"),
349 ("b", "lf[$]"),
350 ])
351
352 def test_many_empty_in_graph_flow(self):
353 flo = gf.Flow('root')
354
355 a = test_utils.ProvidesRequiresTask('a', provides=[], requires=[])
356 flo.add(a)
357
358 b = lf.Flow('b')
359 b_0 = test_utils.ProvidesRequiresTask('b.0', provides=[], requires=[])
360 b_1 = lf.Flow('b.1')
361 b_2 = lf.Flow('b.2')
362 b_3 = test_utils.ProvidesRequiresTask('b.3', provides=[], requires=[])
363 b.add(b_0, b_1, b_2, b_3)
364 flo.add(b)
365
366 c = lf.Flow('c')
367 c_0 = lf.Flow('c.0')
368 c_1 = lf.Flow('c.1')
369 c_2 = lf.Flow('c.2')
370 c.add(c_0, c_1, c_2)
371 flo.add(c)
372
373 d = test_utils.ProvidesRequiresTask('d', provides=[], requires=[])
374 flo.add(d)
375
376 flo.link(b, d)
377 flo.link(a, d)
378 flo.link(c, d)
379
380 g = _replicate_graph_with_names(
381 compiler.PatternCompiler(flo).compile())
382
383 self.assertTrue(g.has_edge('root', 'a'))
384 self.assertTrue(g.has_edge('root', 'b'))
385 self.assertTrue(g.has_edge('root', 'c'))
386
387 self.assertTrue(g.has_edge('b.0', 'b.1'))
388 self.assertTrue(g.has_edge('b.1[$]', 'b.2'))
389 self.assertTrue(g.has_edge('b.2[$]', 'b.3'))
390
391 self.assertTrue(g.has_edge('c.0[$]', 'c.1'))
392 self.assertTrue(g.has_edge('c.1[$]', 'c.2'))
393
394 self.assertTrue(g.has_edge('a', 'd'))
395 self.assertTrue(g.has_edge('b[$]', 'd'))
396 self.assertTrue(g.has_edge('c[$]', 'd'))
397 self.assertEqual(20, len(g))
398
399 def test_empty_flow_in_nested_flow(self):
400 flow = lf.Flow('lf')
401 a = test_utils.ProvidesRequiresTask('a', provides=[], requires=[])
402 b = test_utils.ProvidesRequiresTask('b', provides=[], requires=[])
403
404 flow2 = lf.Flow("lf-2")
405 c = test_utils.ProvidesRequiresTask('c', provides=[], requires=[])
406 d = test_utils.ProvidesRequiresTask('d', provides=[], requires=[])
407 empty_flow = gf.Flow("empty")
408 flow2.add(c, empty_flow, d)
409 flow.add(a, flow2, b)
410
411 g = _replicate_graph_with_names(
412 compiler.PatternCompiler(flow).compile())
413 for u, v in [('lf', 'a'), ('a', 'lf-2'),
414 ('lf-2', 'c'), ('c', 'empty'),
415 ('empty[$]', 'd'), ('d', 'lf-2[$]'),
416 ('lf-2[$]', 'b'), ('b', 'lf[$]')]:
417 self.assertTrue(g.has_edge(u, v))
418
419 def test_empty_flow_in_graph_flow(self):
420 flow = lf.Flow('lf')
421 a = test_utils.ProvidesRequiresTask('a', provides=['a'], requires=[])
422 b = test_utils.ProvidesRequiresTask('b', provides=[], requires=['a'])
423 empty_flow = lf.Flow("empty")
424 flow.add(a, empty_flow, b)
425
426 compilation = compiler.PatternCompiler(flow).compile()
427 g = compilation.execution_graph
428 self.assertTrue(g.has_edge(flow, a))
429 self.assertTrue(g.has_edge(a, empty_flow))
430
431 empty_flow_successors = list(g.successors(empty_flow))
432 self.assertEqual(1, len(empty_flow_successors))
433 empty_flow_terminal = empty_flow_successors[0]
434 self.assertIs(empty_flow, empty_flow_terminal.flow)
435 self.assertEqual(compiler.FLOW_END,
436 g.nodes[empty_flow_terminal]['kind'])
437 self.assertTrue(g.has_edge(empty_flow_terminal, b))
438
439 def test_empty_flow_in_graph_flow_linkage(self):
440 flow = gf.Flow('lf')
441 a = test_utils.ProvidesRequiresTask('a', provides=[], requires=[])
442 b = test_utils.ProvidesRequiresTask('b', provides=[], requires=[])
443 empty_flow = lf.Flow("empty")
444 flow.add(a, empty_flow, b)
445 flow.link(a, b)
446
447 compilation = compiler.PatternCompiler(flow).compile()
448 g = compilation.execution_graph
449 self.assertTrue(g.has_edge(a, b))
450 self.assertTrue(g.has_edge(flow, a))
451 self.assertTrue(g.has_edge(flow, empty_flow))
452
453 def test_checks_for_dups(self):
454 flo = gf.Flow("test").add(
455 test_utils.DummyTask(name="a"),
456 test_utils.DummyTask(name="a")
457 )
458 e = engines.load(flo)
459 self.assertRaisesRegex(exc.Duplicate,
460 '^Atoms with duplicate names',
461 e.compile)
462
463 def test_checks_for_dups_globally(self):
464 flo = gf.Flow("test").add(
465 gf.Flow("int1").add(test_utils.DummyTask(name="a")),
466 gf.Flow("int2").add(test_utils.DummyTask(name="a")))
467 e = engines.load(flo)
468 self.assertRaisesRegex(exc.Duplicate,
469 '^Atoms with duplicate names',
470 e.compile)
471
472 def test_retry_in_linear_flow(self):
473 flo = lf.Flow("test", retry.AlwaysRevert("c"))
474 compilation = compiler.PatternCompiler(flo).compile()
475 self.assertEqual(3, len(compilation.execution_graph))
476 self.assertEqual(2, compilation.execution_graph.number_of_edges())
477
478 def test_retry_in_unordered_flow(self):
479 flo = uf.Flow("test", retry.AlwaysRevert("c"))
480 compilation = compiler.PatternCompiler(flo).compile()
481 self.assertEqual(3, len(compilation.execution_graph))
482 self.assertEqual(2, compilation.execution_graph.number_of_edges())
483
484 def test_retry_in_graph_flow(self):
485 flo = gf.Flow("test", retry.AlwaysRevert("c"))
486 compilation = compiler.PatternCompiler(flo).compile()
487 g = compilation.execution_graph
488 self.assertEqual(3, len(g))
489 self.assertEqual(2, g.number_of_edges())
490
491 def test_retry_in_nested_flows(self):
492 c1 = retry.AlwaysRevert("c1")
493 c2 = retry.AlwaysRevert("c2")
494 inner_flo = lf.Flow("test2", c2)
495 flo = lf.Flow("test", c1).add(inner_flo)
496
497 g = _replicate_graph_with_names(
498 compiler.PatternCompiler(flo).compile())
499 self.assertEqual(6, len(g))
500 self.assertCountEqual(g.edges(data=True), [
501 ('test', 'c1', {'invariant': True}),
502 ('c1', 'test2', {'invariant': True, 'retry': True}),
503 ('test2', 'c2', {'invariant': True}),
504 ('c2', 'test2[$]', {'invariant': True}),
505 ('test2[$]', 'test[$]', {'invariant': True}),
506 ])
507 self.assertIs(c1, g.nodes['c2']['retry'])
508 self.assertCountEqual(['test'], list(g.no_predecessors_iter()))
509 self.assertCountEqual(['test[$]'], list(g.no_successors_iter()))
510
511 def test_retry_in_linear_flow_with_tasks(self):
512 c = retry.AlwaysRevert("c")
513 a, b = test_utils.make_many(2)
514 flo = lf.Flow("test", c).add(a, b)
515
516 g = _replicate_graph_with_names(
517 compiler.PatternCompiler(flo).compile())
518 self.assertEqual(5, len(g))
519 self.assertCountEqual(g.edges(data=True), [
520 ('test', 'c', {'invariant': True}),
521 ('a', 'b', {'invariant': True}),
522 ('c', 'a', {'invariant': True, 'retry': True}),
523 ('b', 'test[$]', {'invariant': True}),
524 ])
525
526 self.assertCountEqual(['test'], g.no_predecessors_iter())
527 self.assertCountEqual(['test[$]'], g.no_successors_iter())
528 self.assertIs(c, g.nodes['a']['retry'])
529 self.assertIs(c, g.nodes['b']['retry'])
530
531 def test_retry_in_unordered_flow_with_tasks(self):
532 c = retry.AlwaysRevert("c")
533 a, b = test_utils.make_many(2)
534 flo = uf.Flow("test", c).add(a, b)
535
536 g = _replicate_graph_with_names(
537 compiler.PatternCompiler(flo).compile())
538 self.assertEqual(5, len(g))
539 self.assertCountEqual(g.edges(data=True), [
540 ('test', 'c', {'invariant': True}),
541 ('c', 'a', {'invariant': True, 'retry': True}),
542 ('c', 'b', {'invariant': True, 'retry': True}),
543 ('b', 'test[$]', {'invariant': True}),
544 ('a', 'test[$]', {'invariant': True}),
545 ])
546
547 self.assertCountEqual(['test'], list(g.no_predecessors_iter()))
548 self.assertCountEqual(['test[$]'], list(g.no_successors_iter()))
549 self.assertIs(c, g.nodes['a']['retry'])
550 self.assertIs(c, g.nodes['b']['retry'])
551
552 def test_retry_in_graph_flow_with_tasks(self):
553 r = retry.AlwaysRevert("r")
554 a, b, c = test_utils.make_many(3)
555 flo = gf.Flow("test", r).add(a, b, c).link(b, c)
556
557 g = _replicate_graph_with_names(
558 compiler.PatternCompiler(flo).compile())
559 self.assertCountEqual(g.edges(data=True), [
560 ('test', 'r', {'invariant': True}),
561 ('r', 'a', {'invariant': True, 'retry': True}),
562 ('r', 'b', {'invariant': True, 'retry': True}),
563 ('b', 'c', {'manual': True}),
564 ('a', 'test[$]', {'invariant': True}),
565 ('c', 'test[$]', {'invariant': True}),
566 ])
567
568 self.assertCountEqual(['test'], g.no_predecessors_iter())
569 self.assertCountEqual(['test[$]'], g.no_successors_iter())
570 self.assertIs(r, g.nodes['a']['retry'])
571 self.assertIs(r, g.nodes['b']['retry'])
572 self.assertIs(r, g.nodes['c']['retry'])
573
574 def test_retries_hierarchy(self):
575 c1 = retry.AlwaysRevert("c1")
576 c2 = retry.AlwaysRevert("c2")
577 a, b, c, d = test_utils.make_many(4)
578 inner_flo = lf.Flow("test2", c2).add(b, c)
579 flo = lf.Flow("test", c1).add(a, inner_flo, d)
580
581 g = _replicate_graph_with_names(
582 compiler.PatternCompiler(flo).compile())
583 self.assertEqual(10, len(g))
584 self.assertCountEqual(g.edges(data=True), [
585 ('test', 'c1', {'invariant': True}),
586 ('c1', 'a', {'invariant': True, 'retry': True}),
587 ('a', 'test2', {'invariant': True}),
588 ('test2', 'c2', {'invariant': True}),
589 ('c2', 'b', {'invariant': True, 'retry': True}),
590 ('b', 'c', {'invariant': True}),
591 ('c', 'test2[$]', {'invariant': True}),
592 ('test2[$]', 'd', {'invariant': True}),
593 ('d', 'test[$]', {'invariant': True}),
594 ])
595 self.assertIs(c1, g.nodes['a']['retry'])
596 self.assertIs(c1, g.nodes['d']['retry'])
597 self.assertIs(c2, g.nodes['b']['retry'])
598 self.assertIs(c2, g.nodes['c']['retry'])
599 self.assertIs(c1, g.nodes['c2']['retry'])
600 self.assertIsNone(g.nodes['c1'].get('retry'))
601
602 def test_retry_subflows_hierarchy(self):
603 c1 = retry.AlwaysRevert("c1")
604 a, b, c, d = test_utils.make_many(4)
605 inner_flo = lf.Flow("test2").add(b, c)
606 flo = lf.Flow("test", c1).add(a, inner_flo, d)
607
608 g = _replicate_graph_with_names(
609 compiler.PatternCompiler(flo).compile())
610 self.assertEqual(9, len(g))
611 self.assertCountEqual(g.edges(data=True), [
612 ('test', 'c1', {'invariant': True}),
613 ('c1', 'a', {'invariant': True, 'retry': True}),
614 ('a', 'test2', {'invariant': True}),
615 ('test2', 'b', {'invariant': True}),
616 ('b', 'c', {'invariant': True}),
617 ('c', 'test2[$]', {'invariant': True}),
618 ('test2[$]', 'd', {'invariant': True}),
619 ('d', 'test[$]', {'invariant': True}),
620 ])
621 self.assertIs(c1, g.nodes['a']['retry'])
622 self.assertIs(c1, g.nodes['d']['retry'])
623 self.assertIs(c1, g.nodes['b']['retry'])
624 self.assertIs(c1, g.nodes['c']['retry'])
625 self.assertIsNone(g.nodes['c1'].get('retry'))