Debian Codegraph @

vcsgraph/tests/test_graph.py

raw symbols SCIP
1 # Copyright (C) 2007-2011 Canonical Ltd
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17 from unittest import TestCase
18
19 from .. import errors
20 from .. import (
21 graph as _mod_graph,
22 )
23 from .. import (
24 known_graph as _mod_known_graph,
25 )
26 from ..graph import NULL_REVISION
27
28 # Ancestry 1:
29 #
30 # NULL_REVISION
31 # |
32 # rev1
33 # /\
34 # rev2a rev2b
35 # | |
36 # rev3 /
37 # | /
38 # rev4
39 ancestry_1 = {
40 b"rev1": (NULL_REVISION,),
41 b"rev2a": (b"rev1",),
42 b"rev2b": (b"rev1",),
43 b"rev3": (b"rev2a",),
44 b"rev4": (b"rev3", b"rev2b"),
45 }
46
47
48 # Ancestry 2:
49 #
50 # NULL_REVISION
51 # / \
52 # rev1a rev1b
53 # |
54 # rev2a
55 # |
56 # rev3a
57 # |
58 # rev4a
59 ancestry_2 = {
60 b"rev1a": (NULL_REVISION,),
61 b"rev2a": (b"rev1a",),
62 b"rev1b": (NULL_REVISION,),
63 b"rev3a": (b"rev2a",),
64 b"rev4a": (b"rev3a",),
65 }
66
67
68 # Criss cross ancestry
69 #
70 # NULL_REVISION
71 # |
72 # rev1
73 # / \
74 # rev2a rev2b
75 # |\ /|
76 # | X |
77 # |/ \|
78 # rev3a rev3b
79 criss_cross = {
80 b"rev1": (NULL_REVISION,),
81 b"rev2a": (b"rev1",),
82 b"rev2b": (b"rev1",),
83 b"rev3a": (b"rev2a", b"rev2b"),
84 b"rev3b": (b"rev2b", b"rev2a"),
85 }
86
87
88 # Criss-cross 2
89 #
90 # NULL_REVISION
91 # / \
92 # rev1a rev1b
93 # |\ /|
94 # | \ / |
95 # | X |
96 # | / \ |
97 # |/ \|
98 # rev2a rev2b
99 criss_cross2 = {
100 b"rev1a": [NULL_REVISION],
101 b"rev1b": [NULL_REVISION],
102 b"rev2a": [b"rev1a", b"rev1b"],
103 b"rev2b": [b"rev1b", b"rev1a"],
104 }
105
106
107 # Mainline:
108 #
109 # NULL_REVISION
110 # |
111 # rev1
112 # / \
113 # | rev2b
114 # | /
115 # rev2a
116 mainline = {
117 b"rev1": [NULL_REVISION],
118 b"rev2a": [b"rev1", b"rev2b"],
119 b"rev2b": [b"rev1"],
120 }
121
122
123 # feature branch:
124 #
125 # NULL_REVISION
126 # |
127 # rev1
128 # |
129 # rev2b
130 # |
131 # rev3b
132 feature_branch = {b"rev1": [NULL_REVISION], b"rev2b": [b"rev1"], b"rev3b": [b"rev2b"]}
133
134
135 # History shortcut
136 # NULL_REVISION
137 # |
138 # rev1------
139 # / \ \
140 # rev2a rev2b rev2c
141 # | / \ /
142 # rev3a rev3b
143 history_shortcut = {
144 b"rev1": [NULL_REVISION],
145 b"rev2a": [b"rev1"],
146 b"rev2b": [b"rev1"],
147 b"rev2c": [b"rev1"],
148 b"rev3a": [b"rev2a", b"rev2b"],
149 b"rev3b": [b"rev2b", b"rev2c"],
150 }
151
152 # Extended history shortcut
153 # NULL_REVISION
154 # |
155 # a
156 # |\
157 # b |
158 # | |
159 # c |
160 # | |
161 # d |
162 # |\|
163 # e f
164 extended_history_shortcut = {
165 b"a": [NULL_REVISION],
166 b"b": [b"a"],
167 b"c": [b"b"],
168 b"d": [b"c"],
169 b"e": [b"d"],
170 b"f": [b"a", b"d"],
171 }
172
173 # Double shortcut
174 # Both sides will see b'A' first, even though it is actually a decendent of a
175 # different common revision.
176 #
177 # NULL_REVISION
178 # |
179 # a
180 # /|\
181 # / b \
182 # / | \
183 # | c |
184 # | / \ |
185 # | d e |
186 # |/ \|
187 # f g
188
189 double_shortcut = {
190 b"a": [NULL_REVISION],
191 b"b": [b"a"],
192 b"c": [b"b"],
193 b"d": [b"c"],
194 b"e": [b"c"],
195 b"f": [b"a", b"d"],
196 b"g": [b"a", b"e"],
197 }
198
199 # Complex shortcut
200 # This has a failure mode in that a shortcut will find some nodes in common,
201 # but the common searcher won't have time to find that one branch is actually
202 # in common. The extra nodes at the beginning are because we want to avoid
203 # walking off the graph. Specifically, node G should be considered common, but
204 # is likely to be seen by M long before the common searcher finds it.
205 #
206 # NULL_REVISION
207 # |
208 # a
209 # |
210 # b
211 # |
212 # c
213 # |
214 # d
215 # |\
216 # e f
217 # | |\
218 # | g h
219 # |/| |
220 # i j |
221 # | | |
222 # | k |
223 # | | |
224 # | l |
225 # |/|/
226 # m n
227 complex_shortcut = {
228 b"a": [NULL_REVISION],
229 b"b": [b"a"],
230 b"c": [b"b"],
231 b"d": [b"c"],
232 b"e": [b"d"],
233 b"f": [b"d"],
234 b"g": [b"f"],
235 b"h": [b"f"],
236 b"i": [b"e", b"g"],
237 b"j": [b"g"],
238 b"k": [b"j"],
239 b"l": [b"k"],
240 b"m": [b"i", b"l"],
241 b"n": [b"l", b"h"],
242 }
243
244 # NULL_REVISION
245 # |
246 # a
247 # |
248 # b
249 # |
250 # c
251 # |
252 # d
253 # |\
254 # e |
255 # | |
256 # f |
257 # | |
258 # g h
259 # | |\
260 # i | j
261 # |\| |
262 # | k |
263 # | | |
264 # | l |
265 # | | |
266 # | m |
267 # | | |
268 # | n |
269 # | | |
270 # | o |
271 # | | |
272 # | p |
273 # | | |
274 # | q |
275 # | | |
276 # | r |
277 # | | |
278 # | s |
279 # | | |
280 # |/|/
281 # t u
282 complex_shortcut2 = {
283 b"a": [NULL_REVISION],
284 b"b": [b"a"],
285 b"c": [b"b"],
286 b"d": [b"c"],
287 b"e": [b"d"],
288 b"f": [b"e"],
289 b"g": [b"f"],
290 b"h": [b"d"],
291 b"i": [b"g"],
292 b"j": [b"h"],
293 b"k": [b"h", b"i"],
294 b"l": [b"k"],
295 b"m": [b"l"],
296 b"n": [b"m"],
297 b"o": [b"n"],
298 b"p": [b"o"],
299 b"q": [b"p"],
300 b"r": [b"q"],
301 b"s": [b"r"],
302 b"t": [b"i", b"s"],
303 b"u": [b"s", b"j"],
304 }
305
306 # Graph where different walkers will race to find the common and uncommon
307 # nodes.
308 #
309 # NULL_REVISION
310 # |
311 # a
312 # |
313 # b
314 # |
315 # c
316 # |
317 # d
318 # |\
319 # e k
320 # | |
321 # f-+-p
322 # | | |
323 # | l |
324 # | | |
325 # | m |
326 # | |\|
327 # g n q
328 # |\| |
329 # h o |
330 # |/| |
331 # i r |
332 # | | |
333 # | s |
334 # | | |
335 # | t |
336 # | | |
337 # | u |
338 # | | |
339 # | v |
340 # | | |
341 # | w |
342 # | | |
343 # | x |
344 # | |\|
345 # | y z
346 # |/
347 # j
348 #
349 # x is found to be common right away, but is the start of a long series of
350 # common commits.
351 # o is actually common, but the i-j shortcut makes it look like it is actually
352 # unique to j at first, you have to traverse all of x->o to find it.
353 # q,m gives the walker from j a common point to stop searching, as does p,f.
354 # k-n exists so that the second pass still has nodes that are worth searching,
355 # rather than instantly cancelling the extra walker.
356
357 racing_shortcuts = {
358 b"a": [NULL_REVISION],
359 b"b": [b"a"],
360 b"c": [b"b"],
361 b"d": [b"c"],
362 b"e": [b"d"],
363 b"f": [b"e"],
364 b"g": [b"f"],
365 b"h": [b"g"],
366 b"i": [b"h", b"o"],
367 b"j": [b"i", b"y"],
368 b"k": [b"d"],
369 b"l": [b"k"],
370 b"m": [b"l"],
371 b"n": [b"m"],
372 b"o": [b"n", b"g"],
373 b"p": [b"f"],
374 b"q": [b"p", b"m"],
375 b"r": [b"o"],
376 b"s": [b"r"],
377 b"t": [b"s"],
378 b"u": [b"t"],
379 b"v": [b"u"],
380 b"w": [b"v"],
381 b"x": [b"w"],
382 b"y": [b"x"],
383 b"z": [b"x", b"q"],
384 }
385
386
387 # A graph with multiple nodes unique to one side.
388 #
389 # NULL_REVISION
390 # |
391 # a
392 # |
393 # b
394 # |
395 # c
396 # |
397 # d
398 # |\
399 # e f
400 # |\ \
401 # g h i
402 # |\ \ \
403 # j k l m
404 # | |/ x|
405 # | n o p
406 # | |/ |
407 # | q |
408 # | | |
409 # | r |
410 # | | |
411 # | s |
412 # | | |
413 # | t |
414 # | | |
415 # | u |
416 # | | |
417 # | v |
418 # | | |
419 # | w |
420 # | | |
421 # | x |
422 # |/ \ /
423 # y z
424 #
425
426 multiple_interesting_unique = {
427 b"a": (NULL_REVISION,),
428 b"b": [b"a"],
429 b"c": [b"b"],
430 b"d": [b"c"],
431 b"e": [b"d"],
432 b"f": [b"d"],
433 b"g": [b"e"],
434 b"h": [b"e"],
435 b"i": [b"f"],
436 b"j": [b"g"],
437 b"k": [b"g"],
438 b"l": [b"h"],
439 b"m": [b"i"],
440 b"n": [b"k", b"l"],
441 b"o": [b"m"],
442 b"p": [b"m", b"l"],
443 b"q": [b"n", b"o"],
444 b"r": [b"q"],
445 b"s": [b"r"],
446 b"t": [b"s"],
447 b"u": [b"t"],
448 b"v": [b"u"],
449 b"w": [b"v"],
450 b"x": [b"w"],
451 b"y": [b"j", b"x"],
452 b"z": [b"x", b"p"],
453 }
454
455
456 # Shortcut with extra root
457 # We have a long history shortcut, and an extra root, which is why we can't
458 # stop searchers based on seeing NULL_REVISION
459 # NULL_REVISION
460 # | |
461 # a |
462 # |\ |
463 # b | |
464 # | | |
465 # c | |
466 # | | |
467 # d | g
468 # |\|/
469 # e f
470 shortcut_extra_root = {
471 b"a": (NULL_REVISION,),
472 b"b": (b"a",),
473 b"c": (b"b",),
474 b"d": (b"c",),
475 b"e": (b"d",),
476 b"f": (b"a", b"d", b"g"),
477 b"g": (NULL_REVISION,),
478 }
479
480 # NULL_REVISION
481 # |
482 # f
483 # |
484 # e
485 # / \
486 # b d
487 # | \ |
488 # a c
489
490 boundary = {
491 b"a": (b"b",),
492 b"c": (b"b", b"d"),
493 b"b": (b"e",),
494 b"d": (b"e",),
495 b"e": (b"f",),
496 b"f": (NULL_REVISION,),
497 }
498
499
500 # A graph that contains a ghost
501 # NULL_REVISION
502 # |
503 # f
504 # |
505 # e g
506 # / \ /
507 # b d
508 # | \ |
509 # a c
510
511 with_ghost = {
512 b"a": (b"b",),
513 b"c": (b"b", b"d"),
514 b"b": (b"e",),
515 b"d": (b"e", b"g"),
516 b"e": (b"f",),
517 b"f": (NULL_REVISION,),
518 NULL_REVISION: (),
519 }
520
521 # A graph that shows we can shortcut finding revnos when reaching them from the
522 # side.
523 # NULL_REVISION
524 # |
525 # a
526 # |
527 # b
528 # |
529 # c
530 # |
531 # d
532 # |
533 # e
534 # / \
535 # f g
536 # |
537 # h
538 # |
539 # i
540
541 with_tail = {
542 b"a": [NULL_REVISION],
543 b"b": [b"a"],
544 b"c": [b"b"],
545 b"d": [b"c"],
546 b"e": [b"d"],
547 b"f": [b"e"],
548 b"g": [b"e"],
549 b"h": [b"f"],
550 b"i": [b"h"],
551 }
552
553
554 class InstrumentedParentsProvider:
555 def __init__(self, parents_provider):
556 self.calls = []
557 self._real_parents_provider = parents_provider
558 get_cached = getattr(parents_provider, "get_cached_parent_map", None)
559 if get_cached is not None:
560 # Only expose the underlying 'get_cached_parent_map' function if
561 # the wrapped provider has it.
562 self.get_cached_parent_map = self._get_cached_parent_map
563
564 def get_parent_map(self, nodes):
565 self.calls.extend(nodes)
566 return self._real_parents_provider.get_parent_map(nodes)
567
568 def _get_cached_parent_map(self, nodes):
569 self.calls.append(("cached", sorted(nodes)))
570 return self._real_parents_provider.get_cached_parent_map(nodes)
571
572
573 class SharedInstrumentedParentsProvider:
574 def __init__(self, parents_provider, calls, info):
575 self.calls = calls
576 self.info = info
577 self._real_parents_provider = parents_provider
578 get_cached = getattr(parents_provider, "get_cached_parent_map", None)
579 if get_cached is not None:
580 # Only expose the underlying 'get_cached_parent_map' function if
581 # the wrapped provider has it.
582 self.get_cached_parent_map = self._get_cached_parent_map
583
584 def get_parent_map(self, nodes):
585 self.calls.append((self.info, sorted(nodes)))
586 return self._real_parents_provider.get_parent_map(nodes)
587
588 def _get_cached_parent_map(self, nodes):
589 self.calls.append((self.info, "cached", sorted(nodes)))
590 return self._real_parents_provider.get_cached_parent_map(nodes)
591
592
593 class TestGraphBase(TestCase):
594 def make_graph(self, ancestors):
595 return _mod_graph.Graph(_mod_graph.DictParentsProvider(ancestors))
596
597 def make_breaking_graph(self, ancestors, break_on):
598 """Make a Graph that raises an exception if we hit a node."""
599 g = self.make_graph(ancestors)
600 orig_parent_map = g.get_parent_map
601
602 def get_parent_map(keys):
603 bad_keys = set(keys).intersection(break_on)
604 if bad_keys:
605 self.fail(f"key(s) {sorted(bad_keys)} was accessed")
606 return orig_parent_map(keys)
607
608 g.get_parent_map = get_parent_map
609 return g
610
611
612 class TestGraph(TestCase):
613 def make_graph(self, ancestors):
614 return _mod_graph.Graph(_mod_graph.DictParentsProvider(ancestors))
615
616 def build_ancestry(self, tree, ancestors):
617 """Create an ancestry as specified by a graph dict.
618
619 :param tree: A tree to use
620 :param ancestors: a dict of {node: [node_parent, ...]}
621 """
622 pending = [NULL_REVISION]
623 descendants = {}
624 for descendant, parents in ancestors.items():
625 for parent in parents:
626 descendants.setdefault(parent, []).append(descendant)
627 while len(pending) > 0:
628 cur_node = pending.pop()
629 for descendant in descendants.get(cur_node, []):
630 if tree.branch.repository.has_revision(descendant):
631 continue
632 parents = [p for p in ancestors[descendant] if p is not NULL_REVISION]
633 if (
634 len(
635 [
636 p
637 for p in parents
638 if not tree.branch.repository.has_revision(p)
639 ]
640 )
641 > 0
642 ):
643 continue
644 tree.set_parent_ids(parents)
645 left_parent = parents[0] if len(parents) > 0 else NULL_REVISION
646 tree.branch.set_last_revision_info(
647 len(tree.branch._lefthand_history(left_parent)), left_parent
648 )
649 tree.commit(descendant, rev_id=descendant)
650 pending.append(descendant)
651
652 def test_lca(self):
653 """Test finding least common ancestor.
654
655 ancestry_1 should always have a single common ancestor
656 """
657 graph = self.make_graph(ancestry_1)
658 self.assertRaises(errors.InvalidRevisionId, graph.find_lca, None)
659 self.assertEqual({NULL_REVISION}, graph.find_lca(NULL_REVISION, NULL_REVISION))
660 self.assertEqual({NULL_REVISION}, graph.find_lca(NULL_REVISION, b"rev1"))
661 self.assertEqual({b"rev1"}, graph.find_lca(b"rev1", b"rev1"))
662 self.assertEqual({b"rev1"}, graph.find_lca(b"rev2a", b"rev2b"))
663
664 def test_no_unique_lca(self):
665 """Test error when one revision is not in the graph."""
666 graph = self.make_graph(ancestry_1)
667 self.assertRaises(
668 errors.NoCommonAncestor, graph.find_unique_lca, b"rev1", b"1rev"
669 )
670
671 def test_lca_criss_cross(self):
672 """Test least-common-ancestor after a criss-cross merge."""
673 graph = self.make_graph(criss_cross)
674 self.assertEqual({b"rev2a", b"rev2b"}, graph.find_lca(b"rev3a", b"rev3b"))
675 self.assertEqual({b"rev2b"}, graph.find_lca(b"rev3a", b"rev3b", b"rev2b"))
676
677 def test_lca_shortcut(self):
678 """Test least-common ancestor on this history shortcut."""
679 graph = self.make_graph(history_shortcut)
680 self.assertEqual({b"rev2b"}, graph.find_lca(b"rev3a", b"rev3b"))
681
682 def test_lefthand_distance_smoke(self):
683 """A simple does it work test for graph.lefthand_distance(keys)."""
684 graph = self.make_graph(history_shortcut)
685 distance_graph = graph.find_lefthand_distances([b"rev3b", b"rev2a"])
686 self.assertEqual({b"rev2a": 2, b"rev3b": 3}, distance_graph)
687
688 def test_lefthand_distance_ghosts(self):
689 """A simple does it work test for graph.lefthand_distance(keys)."""
690 nodes = {b"nonghost": [NULL_REVISION], b"toghost": [b"ghost"]}
691 graph = self.make_graph(nodes)
692 distance_graph = graph.find_lefthand_distances([b"nonghost", b"toghost"])
693 self.assertEqual({b"nonghost": 1, b"toghost": -1}, distance_graph)
694
695 def test_recursive_unique_lca(self):
696 """Test finding a unique least common ancestor.
697
698 ancestry_1 should always have a single common ancestor
699 """
700 graph = self.make_graph(ancestry_1)
701 self.assertEqual(
702 NULL_REVISION, graph.find_unique_lca(NULL_REVISION, NULL_REVISION)
703 )
704 self.assertEqual(NULL_REVISION, graph.find_unique_lca(NULL_REVISION, b"rev1"))
705 self.assertEqual(b"rev1", graph.find_unique_lca(b"rev1", b"rev1"))
706 self.assertEqual(b"rev1", graph.find_unique_lca(b"rev2a", b"rev2b"))
707 self.assertEqual(
708 (
709 b"rev1",
710 1,
711 ),
712 graph.find_unique_lca(b"rev2a", b"rev2b", count_steps=True),
713 )
714
715 def assertRemoveDescendants(self, expected, graph, revisions):
716 parents = graph.get_parent_map(revisions)
717 self.assertEqual(expected, graph._remove_simple_descendants(revisions, parents))
718
719 def test__remove_simple_descendants(self):
720 graph = self.make_graph(ancestry_1)
721 self.assertRemoveDescendants(
722 {b"rev1"}, graph, {b"rev1", b"rev2a", b"rev2b", b"rev3", b"rev4"}
723 )
724
725 def test__remove_simple_descendants_disjoint(self):
726 graph = self.make_graph(ancestry_1)
727 self.assertRemoveDescendants({b"rev1", b"rev3"}, graph, {b"rev1", b"rev3"})
728
729 def test__remove_simple_descendants_chain(self):
730 graph = self.make_graph(ancestry_1)
731 self.assertRemoveDescendants({b"rev1"}, graph, {b"rev1", b"rev2a", b"rev3"})
732
733 def test__remove_simple_descendants_siblings(self):
734 graph = self.make_graph(ancestry_1)
735 self.assertRemoveDescendants(
736 {b"rev2a", b"rev2b"}, graph, {b"rev2a", b"rev2b", b"rev3"}
737 )
738
739 def test_unique_lca_criss_cross(self):
740 """Ensure we don't pick non-unique lcas in a criss-cross."""
741 graph = self.make_graph(criss_cross)
742 self.assertEqual(b"rev1", graph.find_unique_lca(b"rev3a", b"rev3b"))
743 lca, steps = graph.find_unique_lca(b"rev3a", b"rev3b", count_steps=True)
744 self.assertEqual(b"rev1", lca)
745 self.assertEqual(2, steps)
746
747 def test_unique_lca_null_revision(self):
748 """Ensure we pick NULL_REVISION when necessary."""
749 graph = self.make_graph(criss_cross2)
750 self.assertEqual(b"rev1b", graph.find_unique_lca(b"rev2a", b"rev1b"))
751 self.assertEqual(NULL_REVISION, graph.find_unique_lca(b"rev2a", b"rev2b"))
752
753 def test_unique_lca_null_revision2(self):
754 """Ensure we pick NULL_REVISION when necessary."""
755 graph = self.make_graph(ancestry_2)
756 self.assertEqual(NULL_REVISION, graph.find_unique_lca(b"rev4a", b"rev1b"))
757
758 def test_lca_double_shortcut(self):
759 graph = self.make_graph(double_shortcut)
760 self.assertEqual(b"c", graph.find_unique_lca(b"f", b"g"))
761
762 def test_graph_difference(self):
763 graph = self.make_graph(ancestry_1)
764 self.assertEqual((set(), set()), graph.find_difference(b"rev1", b"rev1"))
765 self.assertEqual(
766 (set(), {b"rev1"}), graph.find_difference(NULL_REVISION, b"rev1")
767 )
768 self.assertEqual(
769 ({b"rev1"}, set()), graph.find_difference(b"rev1", NULL_REVISION)
770 )
771 self.assertEqual(
772 ({b"rev2a", b"rev3"}, {b"rev2b"}), graph.find_difference(b"rev3", b"rev2b")
773 )
774 self.assertEqual(
775 ({b"rev4", b"rev3", b"rev2a"}, set()),
776 graph.find_difference(b"rev4", b"rev2b"),
777 )
778
779 def test_graph_difference_separate_ancestry(self):
780 graph = self.make_graph(ancestry_2)
781 self.assertEqual(
782 ({b"rev1a"}, {b"rev1b"}), graph.find_difference(b"rev1a", b"rev1b")
783 )
784 self.assertEqual(
785 ({b"rev1a", b"rev2a", b"rev3a", b"rev4a"}, {b"rev1b"}),
786 graph.find_difference(b"rev4a", b"rev1b"),
787 )
788
789 def test_graph_difference_criss_cross(self):
790 graph = self.make_graph(criss_cross)
791 self.assertEqual(
792 ({b"rev3a"}, {b"rev3b"}), graph.find_difference(b"rev3a", b"rev3b")
793 )
794 self.assertEqual(
795 (set(), {b"rev3b", b"rev2b"}), graph.find_difference(b"rev2a", b"rev3b")
796 )
797
798 def test_graph_difference_extended_history(self):
799 graph = self.make_graph(extended_history_shortcut)
800 self.assertEqual(({b"e"}, {b"f"}), graph.find_difference(b"e", b"f"))
801 self.assertEqual(({b"f"}, {b"e"}), graph.find_difference(b"f", b"e"))
802
803 def test_graph_difference_double_shortcut(self):
804 graph = self.make_graph(double_shortcut)
805 self.assertEqual(
806 ({b"d", b"f"}, {b"e", b"g"}), graph.find_difference(b"f", b"g")
807 )
808
809 def test_graph_difference_complex_shortcut(self):
810 graph = self.make_graph(complex_shortcut)
811 self.assertEqual(
812 ({b"m", b"i", b"e"}, {b"n", b"h"}), graph.find_difference(b"m", b"n")
813 )
814
815 def test_graph_difference_complex_shortcut2(self):
816 graph = self.make_graph(complex_shortcut2)
817 self.assertEqual(({b"t"}, {b"j", b"u"}), graph.find_difference(b"t", b"u"))
818
819 def test_graph_difference_shortcut_extra_root(self):
820 graph = self.make_graph(shortcut_extra_root)
821 self.assertEqual(({b"e"}, {b"f", b"g"}), graph.find_difference(b"e", b"f"))
822
823 def test_iter_topo_order(self):
824 graph = self.make_graph(ancestry_1)
825 args = [b"rev2a", b"rev3", b"rev1"]
826 topo_args = list(graph.iter_topo_order(args))
827 self.assertEqual(set(args), set(topo_args))
828 self.assertGreater(topo_args.index(b"rev2a"), topo_args.index(b"rev1"))
829 self.assertLess(topo_args.index(b"rev2a"), topo_args.index(b"rev3"))
830
831 def test_is_ancestor(self):
832 graph = self.make_graph(ancestry_1)
833 self.assertEqual(True, graph.is_ancestor(b"null:", b"null:"))
834 self.assertEqual(True, graph.is_ancestor(b"null:", b"rev1"))
835 self.assertEqual(False, graph.is_ancestor(b"rev1", b"null:"))
836 self.assertEqual(True, graph.is_ancestor(b"null:", b"rev4"))
837 self.assertEqual(False, graph.is_ancestor(b"rev4", b"null:"))
838 self.assertEqual(False, graph.is_ancestor(b"rev4", b"rev2b"))
839 self.assertEqual(True, graph.is_ancestor(b"rev2b", b"rev4"))
840 self.assertEqual(False, graph.is_ancestor(b"rev2b", b"rev3"))
841 self.assertEqual(False, graph.is_ancestor(b"rev3", b"rev2b"))
842 instrumented_provider = InstrumentedParentsProvider(graph)
843 instrumented_graph = _mod_graph.Graph(instrumented_provider)
844 instrumented_graph.is_ancestor(b"rev2a", b"rev2b")
845 self.assertNotIn(b"null:", instrumented_provider.calls)
846
847 def test_is_between(self):
848 graph = self.make_graph(ancestry_1)
849 self.assertEqual(True, graph.is_between(b"null:", b"null:", b"null:"))
850 self.assertEqual(True, graph.is_between(b"rev1", b"null:", b"rev1"))
851 self.assertEqual(True, graph.is_between(b"rev1", b"rev1", b"rev4"))
852 self.assertEqual(True, graph.is_between(b"rev4", b"rev1", b"rev4"))
853 self.assertEqual(True, graph.is_between(b"rev3", b"rev1", b"rev4"))
854 self.assertEqual(False, graph.is_between(b"rev4", b"rev1", b"rev3"))
855 self.assertEqual(False, graph.is_between(b"rev1", b"rev2a", b"rev4"))
856 self.assertEqual(False, graph.is_between(b"null:", b"rev1", b"rev4"))
857
858 def test_is_ancestor_boundary(self):
859 """Ensure that we avoid searching the whole graph.
860
861 This requires searching through b as a common ancestor, so we
862 can identify that e is common.
863 """
864 graph = self.make_graph(boundary)
865 instrumented_provider = InstrumentedParentsProvider(graph)
866 graph = _mod_graph.Graph(instrumented_provider)
867 self.assertFalse(graph.is_ancestor(b"a", b"c"))
868 self.assertNotIn(b"null:", instrumented_provider.calls)
869
870 def test_iter_ancestry(self):
871 nodes = boundary.copy()
872 nodes[NULL_REVISION] = ()
873 graph = self.make_graph(nodes)
874 expected = nodes.copy()
875 expected.pop(b"a") # b'a' is not in the ancestry of b'c', all the
876 # other nodes are
877 self.assertEqual(expected, dict(graph.iter_ancestry([b"c"])))
878 self.assertEqual(nodes, dict(graph.iter_ancestry([b"a", b"c"])))
879
880 def test_iter_ancestry_with_ghost(self):
881 graph = self.make_graph(with_ghost)
882 expected = with_ghost.copy()
883 # b'a' is not in the ancestry of b'c', and b'g' is a ghost
884 expected[b"g"] = None
885 self.assertEqual(expected, dict(graph.iter_ancestry([b"a", b"c"])))
886 expected.pop(b"a")
887 self.assertEqual(expected, dict(graph.iter_ancestry([b"c"])))
888
889 def test_filter_candidate_lca(self):
890 """Test filter_candidate_lca for a corner case.
891
892 This tests the case where we encounter the end of iteration for b'e'
893 in the same pass as we discover that b'd' is an ancestor of b'e', and
894 therefore b'e' can't be an lca.
895
896 To compensate for different dict orderings on other Python
897 implementations, we mirror b'd' and b'e' with b'b' and b'a'.
898 """
899 # This test is sensitive to the iteration order of dicts. It will
900 # pass incorrectly if b'e' and b'a' sort before b'c'
901 #
902 # NULL_REVISION
903 # / \
904 # a e
905 # | |
906 # b d
907 # \ /
908 # c
909 graph = self.make_graph(
910 {
911 b"c": [b"b", b"d"],
912 b"d": [b"e"],
913 b"b": [b"a"],
914 b"a": [NULL_REVISION],
915 b"e": [NULL_REVISION],
916 }
917 )
918 self.assertEqual({b"c"}, graph.heads([b"a", b"c", b"e"]))
919
920 def test_heads_null(self):
921 graph = self.make_graph(ancestry_1)
922 self.assertEqual({b"null:"}, graph.heads([b"null:"]))
923 self.assertEqual({b"rev1"}, graph.heads([b"null:", b"rev1"]))
924 self.assertEqual({b"rev1"}, graph.heads([b"rev1", b"null:"]))
925 self.assertEqual({b"rev1"}, graph.heads({b"rev1", b"null:"}))
926 self.assertEqual({b"rev1"}, graph.heads((b"rev1", b"null:")))
927
928 def test_heads_one(self):
929 # A single node will always be a head
930 graph = self.make_graph(ancestry_1)
931 self.assertEqual({b"null:"}, graph.heads([b"null:"]))
932 self.assertEqual({b"rev1"}, graph.heads([b"rev1"]))
933 self.assertEqual({b"rev2a"}, graph.heads([b"rev2a"]))
934 self.assertEqual({b"rev2b"}, graph.heads([b"rev2b"]))
935 self.assertEqual({b"rev3"}, graph.heads([b"rev3"]))
936 self.assertEqual({b"rev4"}, graph.heads([b"rev4"]))
937
938 def test_heads_single(self):
939 graph = self.make_graph(ancestry_1)
940 self.assertEqual({b"rev4"}, graph.heads([b"null:", b"rev4"]))
941 self.assertEqual({b"rev2a"}, graph.heads([b"rev1", b"rev2a"]))
942 self.assertEqual({b"rev2b"}, graph.heads([b"rev1", b"rev2b"]))
943 self.assertEqual({b"rev3"}, graph.heads([b"rev1", b"rev3"]))
944 self.assertEqual({b"rev4"}, graph.heads([b"rev1", b"rev4"]))
945 self.assertEqual({b"rev4"}, graph.heads([b"rev2a", b"rev4"]))
946 self.assertEqual({b"rev4"}, graph.heads([b"rev2b", b"rev4"]))
947 self.assertEqual({b"rev4"}, graph.heads([b"rev3", b"rev4"]))
948
949 def test_heads_two_heads(self):
950 graph = self.make_graph(ancestry_1)
951 self.assertEqual({b"rev2a", b"rev2b"}, graph.heads([b"rev2a", b"rev2b"]))
952 self.assertEqual({b"rev3", b"rev2b"}, graph.heads([b"rev3", b"rev2b"]))
953
954 def test_heads_criss_cross(self):
955 graph = self.make_graph(criss_cross)
956 self.assertEqual({b"rev2a"}, graph.heads([b"rev2a", b"rev1"]))
957 self.assertEqual({b"rev2b"}, graph.heads([b"rev2b", b"rev1"]))
958 self.assertEqual({b"rev3a"}, graph.heads([b"rev3a", b"rev1"]))
959 self.assertEqual({b"rev3b"}, graph.heads([b"rev3b", b"rev1"]))
960 self.assertEqual({b"rev2a", b"rev2b"}, graph.heads([b"rev2a", b"rev2b"]))
961 self.assertEqual({b"rev3a"}, graph.heads([b"rev3a", b"rev2a"]))
962 self.assertEqual({b"rev3a"}, graph.heads([b"rev3a", b"rev2b"]))
963 self.assertEqual({b"rev3a"}, graph.heads([b"rev3a", b"rev2a", b"rev2b"]))
964 self.assertEqual({b"rev3b"}, graph.heads([b"rev3b", b"rev2a"]))
965 self.assertEqual({b"rev3b"}, graph.heads([b"rev3b", b"rev2b"]))
966 self.assertEqual({b"rev3b"}, graph.heads([b"rev3b", b"rev2a", b"rev2b"]))
967 self.assertEqual({b"rev3a", b"rev3b"}, graph.heads([b"rev3a", b"rev3b"]))
968 self.assertEqual(
969 {b"rev3a", b"rev3b"}, graph.heads([b"rev3a", b"rev3b", b"rev2a", b"rev2b"])
970 )
971
972 def test_heads_shortcut(self):
973 graph = self.make_graph(history_shortcut)
974
975 self.assertEqual(
976 {b"rev2a", b"rev2b", b"rev2c"}, graph.heads([b"rev2a", b"rev2b", b"rev2c"])
977 )
978 self.assertEqual({b"rev3a", b"rev3b"}, graph.heads([b"rev3a", b"rev3b"]))
979 self.assertEqual(
980 {b"rev3a", b"rev3b"}, graph.heads([b"rev2a", b"rev3a", b"rev3b"])
981 )
982 self.assertEqual({b"rev2a", b"rev3b"}, graph.heads([b"rev2a", b"rev3b"]))
983 self.assertEqual({b"rev2c", b"rev3a"}, graph.heads([b"rev2c", b"rev3a"]))
984
985 def _run_heads_break_deeper(self, graph_dict, search):
986 """Run heads on a graph-as-a-dict.
987
988 If the search asks for the parents of b'deeper' the test will fail.
989 """
990
991 class stub:
992 pass
993
994 def get_parent_map(keys):
995 result = {}
996 for key in keys:
997 if key == b"deeper":
998 self.fail("key deeper was accessed")
999 result[key] = graph_dict[key]
1000 return result
1001
1002 an_obj = stub()
1003 an_obj.get_parent_map = get_parent_map
1004 graph = _mod_graph.Graph(an_obj)
1005 return graph.heads(search)
1006
1007 def test_heads_limits_search(self):
1008 # test that a heads query does not search all of history
1009 graph_dict = {
1010 b"left": [b"common"],
1011 b"right": [b"common"],
1012 b"common": [b"deeper"],
1013 }
1014 self.assertEqual(
1015 {b"left", b"right"},
1016 self._run_heads_break_deeper(graph_dict, [b"left", b"right"]),
1017 )
1018
1019 def test_heads_limits_search_assymetric(self):
1020 # test that a heads query does not search all of history
1021 graph_dict = {
1022 b"left": [b"midleft"],
1023 b"midleft": [b"common"],
1024 b"right": [b"common"],
1025 b"common": [b"aftercommon"],
1026 b"aftercommon": [b"deeper"],
1027 }
1028 self.assertEqual(
1029 {b"left", b"right"},
1030 self._run_heads_break_deeper(graph_dict, [b"left", b"right"]),
1031 )
1032
1033 def test_heads_limits_search_common_search_must_continue(self):
1034 # test that common nodes are still queried, preventing
1035 # all-the-way-to-origin behaviour in the following graph:
1036 graph_dict = {
1037 b"h1": [b"shortcut", b"common1"],
1038 b"h2": [b"common1"],
1039 b"shortcut": [b"common2"],
1040 b"common1": [b"common2"],
1041 b"common2": [b"deeper"],
1042 }
1043 self.assertEqual(
1044 {b"h1", b"h2"}, self._run_heads_break_deeper(graph_dict, [b"h1", b"h2"])
1045 )
1046
1047 def test_breadth_first_search_start_ghosts(self):
1048 graph = self.make_graph({})
1049 # with_ghosts reports the ghosts
1050 search = graph._make_breadth_first_searcher([b"a-ghost"])
1051 self.assertEqual((set(), {b"a-ghost"}), search.next_with_ghosts())
1052 self.assertRaises(StopIteration, search.next_with_ghosts)
1053 # next includes them
1054 search = graph._make_breadth_first_searcher([b"a-ghost"])
1055 self.assertEqual({b"a-ghost"}, next(search))
1056 self.assertRaises(StopIteration, next, search)
1057
1058 def test_breadth_first_search_deep_ghosts(self):
1059 graph = self.make_graph(
1060 {
1061 b"head": [b"present"],
1062 b"present": [b"child", b"ghost"],
1063 b"child": [],
1064 }
1065 )
1066 # with_ghosts reports the ghosts
1067 search = graph._make_breadth_first_searcher([b"head"])
1068 self.assertEqual(({b"head"}, set()), search.next_with_ghosts())
1069 self.assertEqual(({b"present"}, set()), search.next_with_ghosts())
1070 self.assertEqual(({b"child"}, {b"ghost"}), search.next_with_ghosts())
1071 self.assertRaises(StopIteration, search.next_with_ghosts)
1072 # next includes them
1073 search = graph._make_breadth_first_searcher([b"head"])
1074 self.assertEqual({b"head"}, next(search))
1075 self.assertEqual({b"present"}, next(search))
1076 self.assertEqual({b"child", b"ghost"}, next(search))
1077 self.assertRaises(StopIteration, next, search)
1078
1079 def test_breadth_first_search_change_next_to_next_with_ghosts(self):
1080 # To make the API robust, we allow calling both next() and
1081 # next_with_ghosts() on the same searcher.
1082 graph = self.make_graph(
1083 {
1084 b"head": [b"present"],
1085 b"present": [b"child", b"ghost"],
1086 b"child": [],
1087 }
1088 )
1089 # start with next_with_ghosts
1090 search = graph._make_breadth_first_searcher([b"head"])
1091 self.assertEqual(({b"head"}, set()), search.next_with_ghosts())
1092 self.assertEqual({b"present"}, next(search))
1093 self.assertEqual(({b"child"}, {b"ghost"}), search.next_with_ghosts())
1094 self.assertRaises(StopIteration, next, search)
1095 # start with next
1096 search = graph._make_breadth_first_searcher([b"head"])
1097 self.assertEqual({b"head"}, next(search))
1098 self.assertEqual(({b"present"}, set()), search.next_with_ghosts())
1099 self.assertEqual({b"child", b"ghost"}, next(search))
1100 self.assertRaises(StopIteration, search.next_with_ghosts)
1101
1102 def test_breadth_first_change_search(self):
1103 # Changing the search should work with both next and next_with_ghosts.
1104 graph = self.make_graph(
1105 {
1106 b"head": [b"present"],
1107 b"present": [b"stopped"],
1108 b"other": [b"other_2"],
1109 b"other_2": [],
1110 }
1111 )
1112 search = graph._make_breadth_first_searcher([b"head"])
1113 self.assertEqual(({b"head"}, set()), search.next_with_ghosts())
1114 self.assertEqual(({b"present"}, set()), search.next_with_ghosts())
1115 self.assertEqual({b"present"}, search.stop_searching_any([b"present"]))
1116 self.assertEqual(
1117 ({b"other"}, {b"other_ghost"}),
1118 search.start_searching([b"other", b"other_ghost"]),
1119 )
1120 self.assertEqual(({b"other_2"}, set()), search.next_with_ghosts())
1121 self.assertRaises(StopIteration, search.next_with_ghosts)
1122 # next includes them
1123 search = graph._make_breadth_first_searcher([b"head"])
1124 self.assertEqual({b"head"}, next(search))
1125 self.assertEqual({b"present"}, next(search))
1126 self.assertEqual({b"present"}, search.stop_searching_any([b"present"]))
1127 search.start_searching([b"other", b"other_ghost"])
1128 self.assertEqual({b"other_2"}, next(search))
1129 self.assertRaises(StopIteration, next, search)
1130
1131 def assertSeenAndResult(self, instructions, search, next):
1132 """Check the results of .seen and get_result() for a seach.
1133
1134 :param instructions: A list of tuples:
1135 (seen, recipe, included_keys, starts, stops).
1136 seen, recipe and included_keys are results to check on the search
1137 and the searches get_result(). starts and stops are parameters to
1138 pass to start_searching and stop_searching_any during each
1139 iteration, if they are not None.
1140 :param search: The search to use.
1141 :param next: A callable to advance the search.
1142 """
1143 for seen, recipe, included_keys, starts, stops in instructions:
1144 # Adjust for recipe contract changes that don't vary for all the
1145 # current tests.
1146 recipe = ("search",) + recipe
1147 next()
1148 if starts is not None:
1149 search.start_searching(starts)
1150 if stops is not None:
1151 search.stop_searching_any(stops)
1152 state = search.get_state()
1153 self.assertEqual(set(included_keys), state[2])
1154 self.assertEqual(seen, search.seen)
1155
1156 def test_breadth_first_get_result_excludes_current_pending(self):
1157 graph = self.make_graph(
1158 {
1159 b"head": [b"child"],
1160 b"child": [NULL_REVISION],
1161 NULL_REVISION: [],
1162 }
1163 )
1164 search = graph._make_breadth_first_searcher([b"head"])
1165 # At the start, nothing has been seen, to its all excluded:
1166 state = search.get_state()
1167 self.assertEqual(({b"head"}, {b"head"}, set()), state)
1168 self.assertEqual(set(), search.seen)
1169 # using next:
1170 expected = [
1171 ({b"head"}, ({b"head"}, {b"child"}, 1), [b"head"], None, None),
1172 (
1173 {b"head", b"child"},
1174 ({b"head"}, {NULL_REVISION}, 2),
1175 [b"head", b"child"],
1176 None,
1177 None,
1178 ),
1179 (
1180 {b"head", b"child", NULL_REVISION},
1181 ({b"head"}, set(), 3),
1182 [b"head", b"child", NULL_REVISION],
1183 None,
1184 None,
1185 ),
1186 ]
1187 self.assertSeenAndResult(expected, search, search.__next__)
1188 # using next_with_ghosts:
1189 search = graph._make_breadth_first_searcher([b"head"])
1190 self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1191
1192 def test_breadth_first_get_result_starts_stops(self):
1193 graph = self.make_graph(
1194 {
1195 b"head": [b"child"],
1196 b"child": [NULL_REVISION],
1197 b"otherhead": [b"otherchild"],
1198 b"otherchild": [b"excluded"],
1199 b"excluded": [NULL_REVISION],
1200 NULL_REVISION: [],
1201 }
1202 )
1203 search = graph._make_breadth_first_searcher([])
1204 # Starting with nothing and adding a search works:
1205 search.start_searching([b"head"])
1206 # head has been seen:
1207 state = search.get_state()
1208 self.assertEqual(({b"head"}, {b"child"}, {b"head"}), state)
1209 self.assertEqual({b"head"}, search.seen)
1210 # using next:
1211 expected = [
1212 # stop at child, and start a new search at otherhead:
1213 # - otherhead counts as seen immediately when start_searching is
1214 # called.
1215 (
1216 {b"head", b"child", b"otherhead"},
1217 ({b"head", b"otherhead"}, {b"child", b"otherchild"}, 2),
1218 [b"head", b"otherhead"],
1219 [b"otherhead"],
1220 [b"child"],
1221 ),
1222 (
1223 {b"head", b"child", b"otherhead", b"otherchild"},
1224 ({b"head", b"otherhead"}, {b"child", b"excluded"}, 3),
1225 [b"head", b"otherhead", b"otherchild"],
1226 None,
1227 None,
1228 ),
1229 # stop searching excluded now
1230 (
1231 {b"head", b"child", b"otherhead", b"otherchild", b"excluded"},
1232 ({b"head", b"otherhead"}, {b"child", b"excluded"}, 3),
1233 [b"head", b"otherhead", b"otherchild"],
1234 None,
1235 [b"excluded"],
1236 ),
1237 ]
1238 self.assertSeenAndResult(expected, search, search.__next__)
1239 # using next_with_ghosts:
1240 search = graph._make_breadth_first_searcher([])
1241 search.start_searching([b"head"])
1242 self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1243
1244 def test_breadth_first_stop_searching_not_queried(self):
1245 # A client should be able to say b'stop node X' even if X has not been
1246 # returned to the client.
1247 graph = self.make_graph(
1248 {
1249 b"head": [b"child", b"ghost1"],
1250 b"child": [NULL_REVISION],
1251 NULL_REVISION: [],
1252 }
1253 )
1254 search = graph._make_breadth_first_searcher([b"head"])
1255 expected = [
1256 # NULL_REVISION and ghost1 have not been returned
1257 (
1258 {b"head"},
1259 ({b"head"}, {b"child", NULL_REVISION, b"ghost1"}, 1),
1260 [b"head"],
1261 None,
1262 [NULL_REVISION, b"ghost1"],
1263 ),
1264 # ghost1 has been returned, NULL_REVISION is to be returned in the
1265 # next iteration.
1266 (
1267 {b"head", b"child", b"ghost1"},
1268 ({b"head"}, {b"ghost1", NULL_REVISION}, 2),
1269 [b"head", b"child"],
1270 None,
1271 [NULL_REVISION, b"ghost1"],
1272 ),
1273 ]
1274 self.assertSeenAndResult(expected, search, search.__next__)
1275 # using next_with_ghosts:
1276 search = graph._make_breadth_first_searcher([b"head"])
1277 self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1278
1279 def test_breadth_first_stop_searching_late(self):
1280 # A client should be able to say b'stop node X' and have it excluded
1281 # from the result even if X was seen in an older iteration of the
1282 # search.
1283 graph = self.make_graph(
1284 {
1285 b"head": [b"middle"],
1286 b"middle": [b"child"],
1287 b"child": [NULL_REVISION],
1288 NULL_REVISION: [],
1289 }
1290 )
1291 search = graph._make_breadth_first_searcher([b"head"])
1292 expected = [
1293 ({b"head"}, ({b"head"}, {b"middle"}, 1), [b"head"], None, None),
1294 (
1295 {b"head", b"middle"},
1296 ({b"head"}, {b"child"}, 2),
1297 [b"head", b"middle"],
1298 None,
1299 None,
1300 ),
1301 # b'middle' came from the previous iteration, but we don't stop
1302 # searching it until *after* advancing the searcher.
1303 (
1304 {b"head", b"middle", b"child"},
1305 ({b"head"}, {b"middle", b"child"}, 1),
1306 [b"head"],
1307 None,
1308 [b"middle", b"child"],
1309 ),
1310 ]
1311 self.assertSeenAndResult(expected, search, search.__next__)
1312 # using next_with_ghosts:
1313 search = graph._make_breadth_first_searcher([b"head"])
1314 self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1315
1316 def test_breadth_first_get_result_ghosts_are_excluded(self):
1317 graph = self.make_graph(
1318 {
1319 b"head": [b"child", b"ghost"],
1320 b"child": [NULL_REVISION],
1321 NULL_REVISION: [],
1322 }
1323 )
1324 search = graph._make_breadth_first_searcher([b"head"])
1325 # using next:
1326 expected = [
1327 ({b"head"}, ({b"head"}, {b"ghost", b"child"}, 1), [b"head"], None, None),
1328 (
1329 {b"head", b"child", b"ghost"},
1330 ({b"head"}, {NULL_REVISION, b"ghost"}, 2),
1331 [b"head", b"child"],
1332 None,
1333 None,
1334 ),
1335 ]
1336 self.assertSeenAndResult(expected, search, search.__next__)
1337 # using next_with_ghosts:
1338 search = graph._make_breadth_first_searcher([b"head"])
1339 self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1340
1341 def test_breadth_first_get_result_starting_a_ghost_ghost_is_excluded(self):
1342 graph = self.make_graph(
1343 {
1344 b"head": [b"child"],
1345 b"child": [NULL_REVISION],
1346 NULL_REVISION: [],
1347 }
1348 )
1349 search = graph._make_breadth_first_searcher([b"head"])
1350 # using next:
1351 expected = [
1352 (
1353 {b"head", b"ghost"},
1354 ({b"head", b"ghost"}, {b"child", b"ghost"}, 1),
1355 [b"head"],
1356 [b"ghost"],
1357 None,
1358 ),
1359 (
1360 {b"head", b"child", b"ghost"},
1361 ({b"head", b"ghost"}, {NULL_REVISION, b"ghost"}, 2),
1362 [b"head", b"child"],
1363 None,
1364 None,
1365 ),
1366 ]
1367 self.assertSeenAndResult(expected, search, search.__next__)
1368 # using next_with_ghosts:
1369 search = graph._make_breadth_first_searcher([b"head"])
1370 self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1371
1372 def test_breadth_first_revision_count_includes_NULL_REVISION(self):
1373 graph = self.make_graph(
1374 {
1375 b"head": [NULL_REVISION],
1376 NULL_REVISION: [],
1377 }
1378 )
1379 search = graph._make_breadth_first_searcher([b"head"])
1380 # using next:
1381 expected = [
1382 ({b"head"}, ({b"head"}, {NULL_REVISION}, 1), [b"head"], None, None),
1383 (
1384 {b"head", NULL_REVISION},
1385 ({b"head"}, set(), 2),
1386 [b"head", NULL_REVISION],
1387 None,
1388 None,
1389 ),
1390 ]
1391 self.assertSeenAndResult(expected, search, search.__next__)
1392 # using next_with_ghosts:
1393 search = graph._make_breadth_first_searcher([b"head"])
1394 self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1395
1396 def test_breadth_first_search_get_result_after_StopIteration(self):
1397 # StopIteration should not invalid anything..
1398 graph = self.make_graph(
1399 {
1400 b"head": [NULL_REVISION],
1401 NULL_REVISION: [],
1402 }
1403 )
1404 search = graph._make_breadth_first_searcher([b"head"])
1405 # using next:
1406 expected = [
1407 ({b"head"}, ({b"head"}, {NULL_REVISION}, 1), [b"head"], None, None),
1408 (
1409 {b"head", b"ghost", NULL_REVISION},
1410 ({b"head", b"ghost"}, {b"ghost"}, 2),
1411 [b"head", NULL_REVISION],
1412 [b"ghost"],
1413 None,
1414 ),
1415 ]
1416 self.assertSeenAndResult(expected, search, search.__next__)
1417 self.assertRaises(StopIteration, next, search)
1418 self.assertEqual({b"head", b"ghost", NULL_REVISION}, search.seen)
1419 state = search.get_state()
1420 self.assertEqual(
1421 ({b"ghost", b"head"}, {b"ghost"}, {b"head", NULL_REVISION}), state
1422 )
1423 # using next_with_ghosts:
1424 search = graph._make_breadth_first_searcher([b"head"])
1425 self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1426 self.assertRaises(StopIteration, next, search)
1427 self.assertEqual({b"head", b"ghost", NULL_REVISION}, search.seen)
1428 state = search.get_state()
1429 self.assertEqual(
1430 ({b"ghost", b"head"}, {b"ghost"}, {b"head", NULL_REVISION}), state
1431 )
1432
1433
1434 class TestFindUniqueAncestors(TestGraphBase):
1435 def assertFindUniqueAncestors(self, graph, expected, node, common):
1436 actual = graph.find_unique_ancestors(node, common)
1437 self.assertEqual(expected, sorted(actual))
1438
1439 def test_empty_set(self):
1440 graph = self.make_graph(ancestry_1)
1441 self.assertFindUniqueAncestors(graph, [], b"rev1", [b"rev1"])
1442 self.assertFindUniqueAncestors(graph, [], b"rev2b", [b"rev2b"])
1443 self.assertFindUniqueAncestors(graph, [], b"rev3", [b"rev1", b"rev3"])
1444
1445 def test_single_node(self):
1446 graph = self.make_graph(ancestry_1)
1447 self.assertFindUniqueAncestors(graph, [b"rev2a"], b"rev2a", [b"rev1"])
1448 self.assertFindUniqueAncestors(graph, [b"rev2b"], b"rev2b", [b"rev1"])
1449 self.assertFindUniqueAncestors(graph, [b"rev3"], b"rev3", [b"rev2a"])
1450
1451 def test_minimal_ancestry(self):
1452 graph = self.make_breaking_graph(
1453 extended_history_shortcut, [NULL_REVISION, b"a", b"b"]
1454 )
1455 self.assertFindUniqueAncestors(graph, [b"e"], b"e", [b"d"])
1456
1457 graph = self.make_breaking_graph(extended_history_shortcut, [b"b"])
1458 self.assertFindUniqueAncestors(graph, [b"f"], b"f", [b"a", b"d"])
1459
1460 graph = self.make_breaking_graph(complex_shortcut, [b"a", b"b"])
1461 self.assertFindUniqueAncestors(graph, [b"h"], b"h", [b"i"])
1462 self.assertFindUniqueAncestors(graph, [b"e", b"g", b"i"], b"i", [b"h"])
1463 self.assertFindUniqueAncestors(graph, [b"h"], b"h", [b"g"])
1464 self.assertFindUniqueAncestors(graph, [b"h"], b"h", [b"j"])
1465
1466 def test_in_ancestry(self):
1467 graph = self.make_graph(ancestry_1)
1468 self.assertFindUniqueAncestors(graph, [], b"rev1", [b"rev3"])
1469 self.assertFindUniqueAncestors(graph, [], b"rev2b", [b"rev4"])
1470
1471 def test_multiple_revisions(self):
1472 graph = self.make_graph(ancestry_1)
1473 self.assertFindUniqueAncestors(graph, [b"rev4"], b"rev4", [b"rev3", b"rev2b"])
1474 self.assertFindUniqueAncestors(
1475 graph, [b"rev2a", b"rev3", b"rev4"], b"rev4", [b"rev2b"]
1476 )
1477
1478 def test_complex_shortcut(self):
1479 graph = self.make_graph(complex_shortcut)
1480 self.assertFindUniqueAncestors(graph, [b"h", b"n"], b"n", [b"m"])
1481 self.assertFindUniqueAncestors(graph, [b"e", b"i", b"m"], b"m", [b"n"])
1482
1483 def test_complex_shortcut2(self):
1484 graph = self.make_graph(complex_shortcut2)
1485 self.assertFindUniqueAncestors(graph, [b"j", b"u"], b"u", [b"t"])
1486 self.assertFindUniqueAncestors(graph, [b"t"], b"t", [b"u"])
1487
1488 def test_multiple_interesting_unique(self):
1489 graph = self.make_graph(multiple_interesting_unique)
1490 self.assertFindUniqueAncestors(graph, [b"j", b"y"], b"y", [b"z"])
1491 self.assertFindUniqueAncestors(graph, [b"p", b"z"], b"z", [b"y"])
1492
1493 def test_racing_shortcuts(self):
1494 graph = self.make_graph(racing_shortcuts)
1495 self.assertFindUniqueAncestors(graph, [b"p", b"q", b"z"], b"z", [b"y"])
1496 self.assertFindUniqueAncestors(graph, [b"h", b"i", b"j", b"y"], b"j", [b"z"])
1497
1498
1499 class TestGraphFindDistanceToNull(TestGraphBase):
1500 """Test an api that should be able to compute a revno."""
1501
1502 def assertFindDistance(self, revno, graph, target_id, known_ids):
1503 """Assert the output of Graph.find_distance_to_null()."""
1504 actual = graph.find_distance_to_null(target_id, known_ids)
1505 self.assertEqual(revno, actual)
1506
1507 def test_nothing_known(self):
1508 graph = self.make_graph(ancestry_1)
1509 self.assertFindDistance(0, graph, NULL_REVISION, [])
1510 self.assertFindDistance(1, graph, b"rev1", [])
1511 self.assertFindDistance(2, graph, b"rev2a", [])
1512 self.assertFindDistance(2, graph, b"rev2b", [])
1513 self.assertFindDistance(3, graph, b"rev3", [])
1514 self.assertFindDistance(4, graph, b"rev4", [])
1515
1516 def test_rev_is_ghost(self):
1517 graph = self.make_graph(ancestry_1)
1518 try:
1519 graph.find_distance_to_null(
1520 b"rev_missing",
1521 [],
1522 )
1523 except errors.GhostRevisionsHaveNoRevno as e:
1524 self.assertEqual(b"rev_missing", e.revision_id)
1525 self.assertEqual(b"rev_missing", e.ghost_revision_id)
1526 else:
1527 self.fail("Expected GhostRevisionsHaveNoRevno")
1528
1529 def test_ancestor_is_ghost(self):
1530 graph = self.make_graph({b"rev": [b"parent"]})
1531 try:
1532 graph.find_distance_to_null(b"rev", [])
1533 except errors.GhostRevisionsHaveNoRevno as e:
1534 self.assertEqual(b"rev", e.revision_id)
1535 self.assertEqual(b"parent", e.ghost_revision_id)
1536 else:
1537 self.fail("Expected GhostRevisionsHaveNoRevno")
1538
1539 def test_known_in_ancestry(self):
1540 graph = self.make_graph(ancestry_1)
1541 self.assertFindDistance(2, graph, b"rev2a", [(b"rev1", 1)])
1542 self.assertFindDistance(3, graph, b"rev3", [(b"rev2a", 2)])
1543
1544 def test_known_in_ancestry_limits(self):
1545 graph = self.make_breaking_graph(ancestry_1, [b"rev1"])
1546 self.assertFindDistance(4, graph, b"rev4", [(b"rev3", 3)])
1547
1548 def test_target_is_ancestor(self):
1549 graph = self.make_graph(ancestry_1)
1550 self.assertFindDistance(2, graph, b"rev2a", [(b"rev3", 3)])
1551
1552 def test_target_is_ancestor_limits(self):
1553 """We shouldn't search all history if we run into ourselves."""
1554 graph = self.make_breaking_graph(ancestry_1, [b"rev1"])
1555 self.assertFindDistance(3, graph, b"rev3", [(b"rev4", 4)])
1556
1557 def test_target_parallel_to_known_limits(self):
1558 # Even though the known revision isn't part of the other ancestry, they
1559 # eventually converge
1560 graph = self.make_breaking_graph(with_tail, [b"a"])
1561 self.assertFindDistance(6, graph, b"f", [(b"g", 6)])
1562 self.assertFindDistance(7, graph, b"h", [(b"g", 6)])
1563 self.assertFindDistance(8, graph, b"i", [(b"g", 6)])
1564 self.assertFindDistance(6, graph, b"g", [(b"i", 8)])
1565
1566
1567 class TestFindMergeOrder(TestGraphBase):
1568 def assertMergeOrder(self, expected, graph, tip, base_revisions):
1569 self.assertEqual(expected, graph.find_merge_order(tip, base_revisions))
1570
1571 def test_parents(self):
1572 graph = self.make_graph(ancestry_1)
1573 self.assertMergeOrder([b"rev3", b"rev2b"], graph, b"rev4", [b"rev3", b"rev2b"])
1574 self.assertMergeOrder([b"rev3", b"rev2b"], graph, b"rev4", [b"rev2b", b"rev3"])
1575
1576 def test_ancestors(self):
1577 graph = self.make_graph(ancestry_1)
1578 self.assertMergeOrder([b"rev1", b"rev2b"], graph, b"rev4", [b"rev1", b"rev2b"])
1579 self.assertMergeOrder([b"rev1", b"rev2b"], graph, b"rev4", [b"rev2b", b"rev1"])
1580
1581 def test_shortcut_one_ancestor(self):
1582 # When we have enough info, we can stop searching
1583 graph = self.make_breaking_graph(ancestry_1, [b"rev3", b"rev2b", b"rev4"])
1584 # Single ancestors shortcut right away
1585 self.assertMergeOrder([b"rev3"], graph, b"rev4", [b"rev3"])
1586
1587 def test_shortcut_after_one_ancestor(self):
1588 graph = self.make_breaking_graph(ancestry_1, [b"rev2a", b"rev2b"])
1589 self.assertMergeOrder([b"rev3", b"rev1"], graph, b"rev4", [b"rev1", b"rev3"])
1590
1591
1592 class TestFindDescendants(TestGraphBase):
1593 def test_find_descendants_rev1_rev3(self):
1594 graph = self.make_graph(ancestry_1)
1595 descendants = graph.find_descendants(b"rev1", b"rev3")
1596 self.assertEqual({b"rev1", b"rev2a", b"rev3"}, descendants)
1597
1598 def test_find_descendants_rev1_rev4(self):
1599 graph = self.make_graph(ancestry_1)
1600 descendants = graph.find_descendants(b"rev1", b"rev4")
1601 self.assertEqual({b"rev1", b"rev2a", b"rev2b", b"rev3", b"rev4"}, descendants)
1602
1603 def test_find_descendants_rev2a_rev4(self):
1604 graph = self.make_graph(ancestry_1)
1605 descendants = graph.find_descendants(b"rev2a", b"rev4")
1606 self.assertEqual({b"rev2a", b"rev3", b"rev4"}, descendants)
1607
1608
1609 class TestFindLefthandMerger(TestGraphBase):
1610 def check_merger(self, result, ancestry, merged, tip):
1611 graph = self.make_graph(ancestry)
1612 self.assertEqual(result, graph.find_lefthand_merger(merged, tip))
1613
1614 def test_find_lefthand_merger_rev2b(self):
1615 self.check_merger(b"rev4", ancestry_1, b"rev2b", b"rev4")
1616
1617 def test_find_lefthand_merger_rev2a(self):
1618 self.check_merger(b"rev2a", ancestry_1, b"rev2a", b"rev4")
1619
1620 def test_find_lefthand_merger_rev4(self):
1621 self.check_merger(None, ancestry_1, b"rev4", b"rev2a")
1622
1623 def test_find_lefthand_merger_f(self):
1624 self.check_merger(b"i", complex_shortcut, b"f", b"m")
1625
1626 def test_find_lefthand_merger_g(self):
1627 self.check_merger(b"i", complex_shortcut, b"g", b"m")
1628
1629 def test_find_lefthand_merger_h(self):
1630 self.check_merger(b"n", complex_shortcut, b"h", b"n")
1631
1632
1633 class TestGetChildMap(TestGraphBase):
1634 def test_get_child_map(self):
1635 graph = self.make_graph(ancestry_1)
1636 child_map = graph.get_child_map([b"rev4", b"rev3", b"rev2a", b"rev2b"])
1637 self.assertEqual(
1638 {
1639 b"rev1": [b"rev2a", b"rev2b"],
1640 b"rev2a": [b"rev3"],
1641 b"rev2b": [b"rev4"],
1642 b"rev3": [b"rev4"],
1643 },
1644 child_map,
1645 )
1646
1647
1648 class TestCachingParentsProvider(TestCase):
1649 """Test CachingParentsProvider.
1650
1651 These tests run with:
1652
1653 self.inst_pp, a recording parents provider with a graph of a->b, and b is a
1654 ghost.
1655 self.caching_pp, a CachingParentsProvider layered on inst_pp.
1656 """
1657
1658 def setUp(self):
1659 super().setUp()
1660 dict_pp = _mod_graph.DictParentsProvider({b"a": [b"b"]})
1661 self.inst_pp = InstrumentedParentsProvider(dict_pp)
1662 self.caching_pp = _mod_graph.CachingParentsProvider(self.inst_pp)
1663
1664 def test_get_parent_map(self):
1665 """Requesting the same revision should be returned from cache."""
1666 self.assertEqual({}, self.caching_pp._cache)
1667 self.assertEqual({b"a": (b"b",)}, self.caching_pp.get_parent_map([b"a"]))
1668 self.assertEqual([b"a"], self.inst_pp.calls)
1669 self.assertEqual({b"a": (b"b",)}, self.caching_pp.get_parent_map([b"a"]))
1670 # No new call, as it should have been returned from the cache
1671 self.assertEqual([b"a"], self.inst_pp.calls)
1672 self.assertEqual({b"a": (b"b",)}, self.caching_pp._cache)
1673
1674 def test_get_parent_map_not_present(self):
1675 """The cache should also track when a revision doesn't exist."""
1676 self.assertEqual({}, self.caching_pp.get_parent_map([b"b"]))
1677 self.assertEqual([b"b"], self.inst_pp.calls)
1678 self.assertEqual({}, self.caching_pp.get_parent_map([b"b"]))
1679 # No new calls
1680 self.assertEqual([b"b"], self.inst_pp.calls)
1681
1682 def test_get_parent_map_mixed(self):
1683 """Anything that can be returned from cache, should be."""
1684 self.assertEqual({}, self.caching_pp.get_parent_map([b"b"]))
1685 self.assertEqual([b"b"], self.inst_pp.calls)
1686 self.assertEqual({b"a": (b"b",)}, self.caching_pp.get_parent_map([b"a", b"b"]))
1687 self.assertEqual([b"b", b"a"], self.inst_pp.calls)
1688
1689 def test_get_parent_map_repeated(self):
1690 """Asking for the same parent 2x will only forward 1 request."""
1691 self.assertEqual(
1692 {b"a": (b"b",)}, self.caching_pp.get_parent_map([b"b", b"a", b"b"])
1693 )
1694 # Use sorted because we don't care about the order, just that each is
1695 # only present 1 time.
1696 self.assertEqual([b"a", b"b"], sorted(self.inst_pp.calls))
1697
1698 def test_note_missing_key(self):
1699 """After noting that a key is missing it is cached."""
1700 self.caching_pp.note_missing_key(b"b")
1701 self.assertEqual({}, self.caching_pp.get_parent_map([b"b"]))
1702 self.assertEqual([], self.inst_pp.calls)
1703 self.assertEqual({b"b"}, self.caching_pp.missing_keys)
1704
1705 def test_get_cached_parent_map(self):
1706 self.assertEqual({}, self.caching_pp.get_cached_parent_map([b"a"]))
1707 self.assertEqual([], self.inst_pp.calls)
1708 self.assertEqual({b"a": (b"b",)}, self.caching_pp.get_parent_map([b"a"]))
1709 self.assertEqual([b"a"], self.inst_pp.calls)
1710 self.assertEqual({b"a": (b"b",)}, self.caching_pp.get_cached_parent_map([b"a"]))
1711
1712
1713 class TestCachingParentsProviderExtras(TestCase):
1714 """Test the behaviour when parents are provided that were not requested."""
1715
1716 def setUp(self):
1717 super().setUp()
1718
1719 class ExtraParentsProvider:
1720 def get_parent_map(self, keys):
1721 return {
1722 b"rev1": [],
1723 b"rev2": [
1724 b"rev1",
1725 ],
1726 }
1727
1728 self.inst_pp = InstrumentedParentsProvider(ExtraParentsProvider())
1729 self.caching_pp = _mod_graph.CachingParentsProvider(
1730 get_parent_map=self.inst_pp.get_parent_map
1731 )
1732
1733 def test_uncached(self):
1734 self.caching_pp.disable_cache()
1735 self.assertEqual({b"rev1": ()}, self.caching_pp.get_parent_map([b"rev1"]))
1736 self.assertEqual([b"rev1"], self.inst_pp.calls)
1737 self.assertIs(None, self.caching_pp._cache)
1738
1739 def test_cache_initially_empty(self):
1740 self.assertEqual({}, self.caching_pp._cache)
1741
1742 def test_cached(self):
1743 self.assertEqual({b"rev1": ()}, self.caching_pp.get_parent_map([b"rev1"]))
1744 self.assertEqual([b"rev1"], self.inst_pp.calls)
1745 self.assertEqual({b"rev1": (), b"rev2": (b"rev1",)}, self.caching_pp._cache)
1746 self.assertEqual({b"rev1": ()}, self.caching_pp.get_parent_map([b"rev1"]))
1747 self.assertEqual([b"rev1"], self.inst_pp.calls)
1748
1749 def test_disable_cache_clears_cache(self):
1750 # Put something in the cache
1751 self.caching_pp.get_parent_map([b"rev1"])
1752 self.assertEqual(2, len(self.caching_pp._cache))
1753 self.caching_pp.disable_cache()
1754 self.assertIs(None, self.caching_pp._cache)
1755
1756 def test_enable_cache_raises(self):
1757 try:
1758 self.caching_pp.enable_cache()
1759 except AssertionError as e:
1760 self.assertEqual("Cache enabled when already enabled.", str(e))
1761 else:
1762 self.fail("Expected AssertionError")
1763
1764 def test_cache_misses(self):
1765 self.caching_pp.get_parent_map([b"rev3"])
1766 self.caching_pp.get_parent_map([b"rev3"])
1767 self.assertEqual([b"rev3"], self.inst_pp.calls)
1768
1769 def test_no_cache_misses(self):
1770 self.caching_pp.disable_cache()
1771 self.caching_pp.enable_cache(cache_misses=False)
1772 self.caching_pp.get_parent_map([b"rev3"])
1773 self.caching_pp.get_parent_map([b"rev3"])
1774 self.assertEqual([b"rev3", b"rev3"], self.inst_pp.calls)
1775
1776 def test_cache_extras(self):
1777 self.assertEqual({}, self.caching_pp.get_parent_map([b"rev3"]))
1778 self.assertEqual(
1779 {b"rev2": (b"rev1",)}, self.caching_pp.get_parent_map([b"rev2"])
1780 )
1781 self.assertEqual([b"rev3"], self.inst_pp.calls)
1782
1783 def test_extras_using_cached(self):
1784 self.assertEqual({}, self.caching_pp.get_cached_parent_map([b"rev3"]))
1785 self.assertEqual({}, self.caching_pp.get_parent_map([b"rev3"]))
1786 self.assertEqual(
1787 {b"rev2": (b"rev1",)}, self.caching_pp.get_cached_parent_map([b"rev2"])
1788 )
1789 self.assertEqual([b"rev3"], self.inst_pp.calls)
1790
1791
1792 class TestCollapseLinearRegions(TestCase):
1793 def assertCollapsed(self, collapsed, original):
1794 self.assertEqual(collapsed, _mod_graph.collapse_linear_regions(original))
1795
1796 def test_collapse_nothing(self):
1797 d = {1: [2, 3], 2: [], 3: []}
1798 self.assertCollapsed({1: (2, 3), 2: (), 3: ()}, d)
1799 d = {1: [2], 2: [3, 4], 3: [5], 4: [5], 5: []}
1800 self.assertCollapsed({1: (2,), 2: (3, 4), 3: (5,), 4: (5,), 5: ()}, d)
1801
1802 def test_collapse_chain(self):
1803 # Any time we have a linear chain, we should be able to collapse
1804 d = {1: [2], 2: [3], 3: [4], 4: [5], 5: []}
1805 self.assertCollapsed({1: (5,), 5: ()}, d)
1806 d = {5: [4], 4: [3], 3: [2], 2: [1], 1: []}
1807 self.assertCollapsed({5: (1,), 1: ()}, d)
1808 d = {5: [3], 3: [4], 4: [1], 1: [2], 2: []}
1809 self.assertCollapsed({5: (2,), 2: ()}, d)
1810
1811 def test_collapse_with_multiple_children(self):
1812 # 7
1813 # |
1814 # 6
1815 # / \
1816 # 4 5
1817 # | |
1818 # 2 3
1819 # \ /
1820 # 1
1821 #
1822 # 4 and 5 cannot be removed because 6 has 2 children
1823 # 2 and 3 cannot be removed because 1 has 2 parents
1824 d = {1: [2, 3], 2: [4], 4: [6], 3: [5], 5: [6], 6: [7], 7: []}
1825 self.assertCollapsed(
1826 {1: (2, 3), 2: (4,), 4: (6,), 3: (5,), 5: (6,), 6: (7,), 7: ()}, d
1827 )
1828
1829
1830 class TestGraphThunkIdsToKeys(TestCase):
1831 def test_heads(self):
1832 # A
1833 # |\
1834 # B C
1835 # |/
1836 # D
1837 d = {
1838 (b"D",): [(b"B",), (b"C",)],
1839 (b"C",): [(b"A",)],
1840 (b"B",): [(b"A",)],
1841 (b"A",): [],
1842 }
1843 g = _mod_graph.Graph(_mod_graph.DictParentsProvider(d))
1844 graph_thunk = _mod_graph.GraphThunkIdsToKeys(g)
1845 self.assertEqual([b"D"], sorted(graph_thunk.heads([b"D", b"A"])))
1846 self.assertEqual([b"D"], sorted(graph_thunk.heads([b"D", b"B"])))
1847 self.assertEqual([b"D"], sorted(graph_thunk.heads([b"D", b"C"])))
1848 self.assertEqual([b"B", b"C"], sorted(graph_thunk.heads([b"B", b"C"])))
1849
1850 def test_add_node(self):
1851 d = {(b"C",): [(b"A",)], (b"B",): [(b"A",)], (b"A",): []}
1852 g = _mod_known_graph.KnownGraph(d)
1853 graph_thunk = _mod_graph.GraphThunkIdsToKeys(g)
1854 graph_thunk.add_node(b"D", [b"A", b"C"])
1855 self.assertEqual([b"B", b"D"], sorted(graph_thunk.heads([b"D", b"B", b"A"])))
1856
1857 def test_merge_sort(self):
1858 d = {(b"C",): [(b"A",)], (b"B",): [(b"A",)], (b"A",): []}
1859 g = _mod_known_graph.KnownGraph(d)
1860 graph_thunk = _mod_graph.GraphThunkIdsToKeys(g)
1861 graph_thunk.add_node(b"D", [b"A", b"C"])
1862 self.assertEqual(
1863 [(b"C", 0, (2,), False), (b"A", 0, (1,), True)],
1864 [
1865 (n.key, n.merge_depth, n.revno, n.end_of_merge)
1866 for n in graph_thunk.merge_sort(b"C")
1867 ],
1868 )
1869
1870
1871 class TestStackedParentsProvider(TestCase):
1872 def setUp(self):
1873 super().setUp()
1874 self.calls = []
1875
1876 def get_shared_provider(self, info, ancestry, has_cached):
1877 pp = _mod_graph.DictParentsProvider(ancestry)
1878 if has_cached:
1879 pp.get_cached_parent_map = pp.get_parent_map
1880 return SharedInstrumentedParentsProvider(pp, self.calls, info)
1881
1882 def test_stacked_parents_provider(self):
1883 parents1 = _mod_graph.DictParentsProvider({b"rev2": [b"rev3"]})
1884 parents2 = _mod_graph.DictParentsProvider({b"rev1": [b"rev4"]})
1885 stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
1886 self.assertEqual(
1887 {b"rev1": (b"rev4",), b"rev2": (b"rev3",)},
1888 stacked.get_parent_map(
1889 (
1890 b"rev1",
1891 b"rev2",
1892 )
1893 ),
1894 )
1895 self.assertEqual(
1896 {b"rev2": (b"rev3",), b"rev1": (b"rev4",)},
1897 stacked.get_parent_map((b"rev2", b"rev1")),
1898 )
1899 self.assertEqual(
1900 {b"rev2": (b"rev3",)}, stacked.get_parent_map((b"rev2", b"rev2"))
1901 )
1902 self.assertEqual(
1903 {b"rev1": (b"rev4",)}, stacked.get_parent_map((b"rev1", b"rev1"))
1904 )
1905
1906 def test_stacked_parents_provider_overlapping(self):
1907 # rev2 is availible in both providers.
1908 # 1
1909 # |
1910 # 2
1911 parents1 = _mod_graph.DictParentsProvider({b"rev2": [b"rev1"]})
1912 parents2 = _mod_graph.DictParentsProvider({b"rev2": [b"rev1"]})
1913 stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
1914 self.assertEqual({b"rev2": (b"rev1",)}, stacked.get_parent_map([b"rev2"]))
1915
1916 def test_handles_no_get_cached_parent_map(self):
1917 # this shows that we both handle when a provider doesn't implement
1918 # get_cached_parent_map
1919 pp1 = self.get_shared_provider(b"pp1", {b"rev2": (b"rev1",)}, has_cached=False)
1920 pp2 = self.get_shared_provider(b"pp2", {b"rev2": (b"rev1",)}, has_cached=True)
1921 stacked = _mod_graph.StackedParentsProvider([pp1, pp2])
1922 self.assertEqual({b"rev2": (b"rev1",)}, stacked.get_parent_map([b"rev2"]))
1923 # No call on b'pp1' because it doesn't provide get_cached_parent_map
1924 self.assertEqual([(b"pp2", "cached", [b"rev2"])], self.calls)
1925
1926 def test_query_order(self):
1927 # We should call get_cached_parent_map on all providers before we call
1928 # get_parent_map. Further, we should track what entries we have found,
1929 # and not re-try them.
1930 pp1 = self.get_shared_provider(b"pp1", {b"a": []}, has_cached=True)
1931 pp2 = self.get_shared_provider(b"pp2", {b"c": [b"b"]}, has_cached=False)
1932 pp3 = self.get_shared_provider(b"pp3", {b"b": [b"a"]}, has_cached=True)
1933 stacked = _mod_graph.StackedParentsProvider([pp1, pp2, pp3])
1934 self.assertEqual(
1935 {b"a": (), b"b": (b"a",), b"c": (b"b",)},
1936 stacked.get_parent_map([b"a", b"b", b"c", b"d"]),
1937 )
1938 self.assertEqual(
1939 [
1940 (b"pp1", "cached", [b"a", b"b", b"c", b"d"]),
1941 # No call to pp2, because it doesn't have cached
1942 (b"pp3", "cached", [b"b", b"c", b"d"]),
1943 (b"pp1", [b"c", b"d"]),
1944 (b"pp2", [b"c", b"d"]),
1945 (b"pp3", [b"d"]),
1946 ],
1947 self.calls,
1948 )
1949
1950 def test_honours_live_mutation_of_iterable(self):
1951 # Mutating the iterable that was passed to StackedParentsProvider
1952 # after construction must affect subsequent get_parent_map calls,
1953 # so that callers passing a lazy/joined list (e.g. breezy's
1954 # _LazyListJoin) see freshly added fallback repositories.
1955 providers = [_mod_graph.DictParentsProvider({b"rev1": [b"rev0"]})]
1956 stacked = _mod_graph.StackedParentsProvider(providers)
1957 self.assertEqual(
1958 {b"rev1": (b"rev0",)}, stacked.get_parent_map([b"rev1", b"rev2"])
1959 )
1960 providers.append(_mod_graph.DictParentsProvider({b"rev2": [b"rev1"]}))
1961 self.assertEqual(
1962 {b"rev1": (b"rev0",), b"rev2": (b"rev1",)},
1963 stacked.get_parent_map([b"rev1", b"rev2"]),
1964 )