Skip to content

Commit eccb753

Browse files
bpo-45624: make test_graphlib not depend on the iteration order of sets (GH-29233) (GH-29293)
the current test depended on integer sets being iterated on in a certain fixed order. That order is different on PyPy (insertion based) and could change in CPython in the future in theory. Make the test robust against a different iteration order by sorting. (cherry picked from commit 7401694) Co-authored-by: Carl Friedrich Bolz-Tereick <[email protected]>
1 parent 823b3e3 commit eccb753

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Lib/test/test_graphlib.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@ def static_order_with_groups(ts):
1313
nodes = ts.get_ready()
1414
for node in nodes:
1515
ts.done(node)
16-
yield nodes
16+
yield tuple(sorted(nodes))
1717

1818
ts = graphlib.TopologicalSorter(graph)
1919
self.assertEqual(list(static_order_with_groups(ts)), list(expected))
2020

2121
ts = graphlib.TopologicalSorter(graph)
22-
self.assertEqual(list(ts.static_order()), list(chain(*expected)))
22+
# need to be a bit careful comparing the result of ts.static_order and
23+
# expected, because the order within a group is dependent on set
24+
# iteration order
25+
it = iter(ts.static_order())
26+
for group in expected:
27+
tsgroup = {next(it) for element in group}
28+
self.assertEqual(set(group), tsgroup)
2329

2430
def _assert_cycle(self, graph, cycle):
2531
ts = graphlib.TopologicalSorter()
@@ -36,7 +42,7 @@ def _assert_cycle(self, graph, cycle):
3642
def test_simple_cases(self):
3743
self._test_graph(
3844
{2: {11}, 9: {11, 8}, 10: {11, 3}, 11: {7, 5}, 8: {7, 3}},
39-
[(3, 5, 7), (11, 8), (2, 10, 9)],
45+
[(3, 5, 7), (8, 11), (2, 9, 10)],
4046
)
4147

4248
self._test_graph({1: {}}, [(1,)])
@@ -80,7 +86,7 @@ def test_no_dependencies(self):
8086

8187
def test_the_node_multiple_times(self):
8288
# Test same node multiple times in dependencies
83-
self._test_graph({1: {2}, 3: {4}, 0: [2, 4, 4, 4, 4, 4]}, [(2, 4), (1, 3, 0)])
89+
self._test_graph({1: {2}, 3: {4}, 0: [2, 4, 4, 4, 4, 4]}, [(2, 4), (0, 1, 3)])
8490

8591
# Test adding the same dependency multiple times
8692
ts = graphlib.TopologicalSorter()
@@ -242,3 +248,6 @@ def check_order_with_hash_seed(seed):
242248
self.assertNotEqual(run1, "")
243249
self.assertNotEqual(run2, "")
244250
self.assertEqual(run1, run2)
251+
252+
if __name__ == "__main__":
253+
unittest.main()

0 commit comments

Comments
 (0)