~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_graph.py

  • Committer: Vincent Ladeuil
  • Date: 2008-01-29 08:40:53 UTC
  • mto: (3206.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 3207.
  • Revision ID: v.ladeuil+lp@free.fr-20080129084053-sunwf549ox6zczqr
Fix two more leaked log files.

* bzrlib/tests/test_http.py:
(TestHttpProxyWhiteBox.tearDown): Call the base class tearDown.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
from bzrlib import (
18
18
    errors,
19
19
    graph as _mod_graph,
 
20
    symbol_versioning,
 
21
    tests,
20
22
    )
21
23
from bzrlib.revision import NULL_REVISION
22
24
from bzrlib.tests import TestCaseWithMemoryTransport
115
117
#     /  \      \
116
118
#  rev2a rev2b rev2c
117
119
#    |  /   \   /
118
 
#  rev3a    reveb
 
120
#  rev3a    rev3b
119
121
history_shortcut = {'rev1': [NULL_REVISION], 'rev2a': ['rev1'],
120
122
                    'rev2b': ['rev1'], 'rev2c': ['rev1'],
121
123
                    'rev3a': ['rev2a', 'rev2b'], 'rev3b': ['rev2b', 'rev2c']}
122
124
 
 
125
# Extended history shortcut
 
126
#  NULL_REVISION
 
127
#       |
 
128
#       a
 
129
#       |\
 
130
#       b |
 
131
#       | |
 
132
#       c |
 
133
#       | |
 
134
#       d |
 
135
#       |\|
 
136
#       e f
 
137
extended_history_shortcut = {'a': [NULL_REVISION],
 
138
                             'b': ['a'],
 
139
                             'c': ['b'],
 
140
                             'd': ['c'],
 
141
                             'e': ['d'],
 
142
                             'f': ['a', 'd'],
 
143
                            }
 
144
 
 
145
# Double shortcut
 
146
# Both sides will see 'A' first, even though it is actually a decendent of a
 
147
# different common revision.
 
148
#
 
149
#  NULL_REVISION
 
150
#       |
 
151
#       a
 
152
#      /|\
 
153
#     / b \
 
154
#    /  |  \
 
155
#   |   c   |
 
156
#   |  / \  |
 
157
#   | d   e |
 
158
#   |/     \|
 
159
#   f       g
 
160
 
 
161
double_shortcut = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'],
 
162
                   'd':['c'], 'e':['c'], 'f':['a', 'd'],
 
163
                   'g':['a', 'e']}
 
164
 
 
165
# Complex shortcut
 
166
# This has a failure mode in that a shortcut will find some nodes in common,
 
167
# but the common searcher won't have time to find that one branch is actually
 
168
# in common. The extra nodes at the top are because we want to avoid
 
169
# walking off the graph. Specifically, node G should be considered common, but
 
170
# is likely to be seen by M long before the common searcher finds it.
 
171
#
 
172
# NULL_REVISION
 
173
#     |
 
174
#     a
 
175
#     |
 
176
#     b
 
177
#     |
 
178
#     c
 
179
#     |
 
180
#     d
 
181
#     |\
 
182
#     e f
 
183
#     | |\
 
184
#     i | h
 
185
#     |\| |
 
186
#     | g |
 
187
#     | | |
 
188
#     | j |
 
189
#     | | |
 
190
#     | k |
 
191
#     | | |
 
192
#     | l |
 
193
#     |/|/
 
194
#     m n
 
195
complex_shortcut = {'d':[NULL_REVISION],
 
196
                    'x':['d'], 'y':['x'],
 
197
                    'e':['y'], 'f':['d'], 'g':['f', 'i'], 'h':['f'],
 
198
                    'i':['e'], 'j':['g'], 'k':['j'],
 
199
                    'l':['k'], 'm':['i', 's'], 'n':['s', 'h'],
 
200
                    'o':['l'], 'p':['o'], 'q':['p'],
 
201
                    'r':['q'], 's':['r'],
 
202
                    }
 
203
 
 
204
# Shortcut with extra root
 
205
# We have a long history shortcut, and an extra root, which is why we can't
 
206
# stop searchers based on seeing NULL_REVISION
 
207
#  NULL_REVISION
 
208
#       |   |
 
209
#       a   |
 
210
#       |\  |
 
211
#       b | |
 
212
#       | | |
 
213
#       c | |
 
214
#       | | |
 
215
#       d | g
 
216
#       |\|/
 
217
#       e f
 
218
shortcut_extra_root = {'a': [NULL_REVISION],
 
219
                       'b': ['a'],
 
220
                       'c': ['b'],
 
221
                       'd': ['c'],
 
222
                       'e': ['d'],
 
223
                       'f': ['a', 'd', 'g'],
 
224
                       'g': [NULL_REVISION],
 
225
                      }
 
226
 
123
227
#  NULL_REVISION
124
228
#       |
125
229
#       f
140
244
        self.calls = []
141
245
        self._real_parents_provider = parents_provider
142
246
 
143
 
    def get_parents(self, nodes):
 
247
    def get_parent_map(self, nodes):
144
248
        self.calls.extend(nodes)
145
 
        return self._real_parents_provider.get_parents(nodes)
 
249
        return self._real_parents_provider.get_parent_map(nodes)
146
250
 
147
251
 
148
252
class TestGraph(TestCaseWithMemoryTransport):
149
253
 
150
254
    def make_graph(self, ancestors):
151
 
        tree = self.prepare_memory_tree('.')
152
 
        self.build_ancestry(tree, ancestors)
153
 
        self.addCleanup(tree.unlock)
154
 
        return tree.branch.repository.get_graph()
 
255
        return _mod_graph.Graph(_mod_graph.DictParentsProvider(ancestors))
155
256
 
156
257
    def prepare_memory_tree(self, location):
157
258
        tree = self.make_branch_and_memory_tree(location)
236
337
                         graph.find_unique_lca(NULL_REVISION, 'rev1'))
237
338
        self.assertEqual('rev1', graph.find_unique_lca('rev1', 'rev1'))
238
339
        self.assertEqual('rev1', graph.find_unique_lca('rev2a', 'rev2b'))
 
340
        self.assertEqual(('rev1', 1,),
 
341
                         graph.find_unique_lca('rev2a', 'rev2b',
 
342
                         count_steps=True))
239
343
 
240
344
    def test_unique_lca_criss_cross(self):
241
345
        """Ensure we don't pick non-unique lcas in a criss-cross"""
242
346
        graph = self.make_graph(criss_cross)
243
347
        self.assertEqual('rev1', graph.find_unique_lca('rev3a', 'rev3b'))
 
348
        lca, steps = graph.find_unique_lca('rev3a', 'rev3b', count_steps=True)
 
349
        self.assertEqual('rev1', lca)
 
350
        self.assertEqual(2, steps)
244
351
 
245
352
    def test_unique_lca_null_revision(self):
246
353
        """Ensure we pick NULL_REVISION when necessary"""
255
362
        self.assertEqual(NULL_REVISION,
256
363
                         graph.find_unique_lca('rev4a', 'rev1b'))
257
364
 
 
365
    def test_lca_double_shortcut(self):
 
366
        graph = self.make_graph(double_shortcut)
 
367
        self.assertEqual('c', graph.find_unique_lca('f', 'g'))
 
368
 
258
369
    def test_common_ancestor_two_repos(self):
259
370
        """Ensure we do unique_lca using data from two repos"""
260
371
        mainline_tree = self.prepare_memory_tree('mainline')
283
394
        self.assertEqual((set(['rev4', 'rev3', 'rev2a']), set()),
284
395
                         graph.find_difference('rev4', 'rev2b'))
285
396
 
 
397
    def test_graph_difference_separate_ancestry(self):
 
398
        graph = self.make_graph(ancestry_2)
 
399
        self.assertEqual((set(['rev1a']), set(['rev1b'])),
 
400
                         graph.find_difference('rev1a', 'rev1b'))
 
401
        self.assertEqual((set(['rev1a', 'rev2a', 'rev3a', 'rev4a']),
 
402
                          set(['rev1b'])),
 
403
                         graph.find_difference('rev4a', 'rev1b'))
 
404
 
286
405
    def test_graph_difference_criss_cross(self):
287
406
        graph = self.make_graph(criss_cross)
288
407
        self.assertEqual((set(['rev3a']), set(['rev3b'])),
290
409
        self.assertEqual((set([]), set(['rev3b', 'rev2b'])),
291
410
                         graph.find_difference('rev2a', 'rev3b'))
292
411
 
 
412
    def test_graph_difference_extended_history(self):
 
413
        graph = self.make_graph(extended_history_shortcut)
 
414
        self.expectFailure('find_difference cannot handle shortcuts',
 
415
            self.assertEqual, (set(['e']), set(['f'])),
 
416
                graph.find_difference('e', 'f'))
 
417
        self.assertEqual((set(['e']), set(['f'])),
 
418
                         graph.find_difference('e', 'f'))
 
419
        self.assertEqual((set(['f']), set(['e'])),
 
420
                         graph.find_difference('f', 'e'))
 
421
 
 
422
    def test_graph_difference_double_shortcut(self):
 
423
        graph = self.make_graph(double_shortcut)
 
424
        self.assertEqual((set(['d', 'f']), set(['e', 'g'])),
 
425
                         graph.find_difference('f', 'g'))
 
426
 
 
427
    def test_graph_difference_complex_shortcut(self):
 
428
        graph = self.make_graph(complex_shortcut)
 
429
        self.expectFailure('find_difference cannot handle shortcuts',
 
430
            self.assertEqual, (set(['m']), set(['h', 'n'])),
 
431
                graph.find_difference('m', 'n'))
 
432
        self.assertEqual((set(['m']), set(['h', 'n'])),
 
433
                         graph.find_difference('m', 'n'))
 
434
 
 
435
    def test_graph_difference_shortcut_extra_root(self):
 
436
        graph = self.make_graph(shortcut_extra_root)
 
437
        self.expectFailure('find_difference cannot handle shortcuts',
 
438
            self.assertEqual, (set(['e']), set(['f', 'g'])),
 
439
                graph.find_difference('e', 'f'))
 
440
        self.assertEqual((set(['e']), set(['f', 'g'])),
 
441
                         graph.find_difference('e', 'f'))
 
442
 
293
443
    def test_stacked_parents_provider(self):
294
 
 
295
444
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
296
445
        parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
297
446
        stacked = _mod_graph._StackedParentsProvider([parents1, parents2])
298
 
        self.assertEqual([['rev4',], ['rev3']],
299
 
                         stacked.get_parents(['rev1', 'rev2']))
300
 
        self.assertEqual([['rev3',], ['rev4']],
301
 
                         stacked.get_parents(['rev2', 'rev1']))
302
 
        self.assertEqual([['rev3',], ['rev3']],
303
 
                         stacked.get_parents(['rev2', 'rev2']))
304
 
        self.assertEqual([['rev4',], ['rev4']],
305
 
                         stacked.get_parents(['rev1', 'rev1']))
 
447
        self.assertEqual({'rev1':['rev4'], 'rev2':['rev3']},
 
448
                         stacked.get_parent_map(['rev1', 'rev2']))
 
449
        self.assertEqual({'rev2':['rev3'], 'rev1':['rev4']},
 
450
                         stacked.get_parent_map(['rev2', 'rev1']))
 
451
        self.assertEqual({'rev2':['rev3']},
 
452
                         stacked.get_parent_map(['rev2', 'rev2']))
 
453
        self.assertEqual({'rev1':['rev4']},
 
454
                         stacked.get_parent_map(['rev1', 'rev1']))
306
455
 
307
456
    def test_iter_topo_order(self):
308
457
        graph = self.make_graph(ancestry_1)
373
522
        self.assertEqual(set(['rev1']), graph.heads(('rev1', 'null:')))
374
523
 
375
524
    def test_heads_one(self):
376
 
        # A single node will alwaya be a head
 
525
        # A single node will always be a head
377
526
        graph = self.make_graph(ancestry_1)
378
527
        self.assertEqual(set(['null:']), graph.heads(['null:']))
379
528
        self.assertEqual(set(['rev1']), graph.heads(['rev1']))
450
599
        """
451
600
        class stub(object):
452
601
            pass
453
 
        def get_parents(keys):
454
 
            result = []
 
602
        def get_parent_map(keys):
 
603
            result = {}
455
604
            for key in keys:
456
605
                if key == 'deeper':
457
606
                    self.fail('key deeper was accessed')
458
 
                result.append(graph_dict[key])
 
607
                result[key] = graph_dict[key]
459
608
            return result
460
609
        an_obj = stub()
461
 
        an_obj.get_parents = get_parents
 
610
        an_obj.get_parent_map = get_parent_map
462
611
        graph = _mod_graph.Graph(an_obj)
463
612
        return graph.heads(search)
464
613
 
496
645
        }
497
646
        self.assertEqual(set(['h1', 'h2']),
498
647
            self._run_heads_break_deeper(graph_dict, ['h1', 'h2']))
 
648
 
 
649
    def test_breadth_first_search_start_ghosts(self):
 
650
        graph = self.make_graph({})
 
651
        # with_ghosts reports the ghosts
 
652
        search = graph._make_breadth_first_searcher(['a-ghost'])
 
653
        self.assertEqual((set(), set(['a-ghost'])), search.next_with_ghosts())
 
654
        self.assertRaises(StopIteration, search.next_with_ghosts)
 
655
        # next includes them
 
656
        search = graph._make_breadth_first_searcher(['a-ghost'])
 
657
        self.assertEqual(set(['a-ghost']), search.next())
 
658
        self.assertRaises(StopIteration, search.next)
 
659
 
 
660
    def test_breadth_first_search_deep_ghosts(self):
 
661
        graph = self.make_graph({
 
662
            'head':['present'],
 
663
            'present':['child', 'ghost'],
 
664
            'child':[],
 
665
            })
 
666
        # with_ghosts reports the ghosts
 
667
        search = graph._make_breadth_first_searcher(['head'])
 
668
        self.assertEqual((set(['head']), set()), search.next_with_ghosts())
 
669
        self.assertEqual((set(['present']), set()), search.next_with_ghosts())
 
670
        self.assertEqual((set(['child']), set(['ghost'])),
 
671
            search.next_with_ghosts())
 
672
        self.assertRaises(StopIteration, search.next_with_ghosts)
 
673
        # next includes them
 
674
        search = graph._make_breadth_first_searcher(['head'])
 
675
        self.assertEqual(set(['head']), search.next())
 
676
        self.assertEqual(set(['present']), search.next())
 
677
        self.assertEqual(set(['child', 'ghost']),
 
678
            search.next())
 
679
        self.assertRaises(StopIteration, search.next)
 
680
 
 
681
    def test_breadth_first_search_change_next_to_next_with_ghosts(self):
 
682
        # To make the API robust, we allow calling both next() and
 
683
        # next_with_ghosts() on the same searcher.
 
684
        graph = self.make_graph({
 
685
            'head':['present'],
 
686
            'present':['child', 'ghost'],
 
687
            'child':[],
 
688
            })
 
689
        # start with next_with_ghosts
 
690
        search = graph._make_breadth_first_searcher(['head'])
 
691
        self.assertEqual((set(['head']), set()), search.next_with_ghosts())
 
692
        self.assertEqual(set(['present']), search.next())
 
693
        self.assertEqual((set(['child']), set(['ghost'])),
 
694
            search.next_with_ghosts())
 
695
        self.assertRaises(StopIteration, search.next)
 
696
        # start with next
 
697
        search = graph._make_breadth_first_searcher(['head'])
 
698
        self.assertEqual(set(['head']), search.next())
 
699
        self.assertEqual((set(['present']), set()), search.next_with_ghosts())
 
700
        self.assertEqual(set(['child', 'ghost']),
 
701
            search.next())
 
702
        self.assertRaises(StopIteration, search.next_with_ghosts)
 
703
 
 
704
    def test_breadth_first_change_search(self):
 
705
        # Changing the search should work with both next and next_with_ghosts.
 
706
        graph = self.make_graph({
 
707
            'head':['present'],
 
708
            'present':['stopped'],
 
709
            'other':['other_2'],
 
710
            'other_2':[],
 
711
            })
 
712
        search = graph._make_breadth_first_searcher(['head'])
 
713
        self.assertEqual((set(['head']), set()), search.next_with_ghosts())
 
714
        self.assertEqual((set(['present']), set()), search.next_with_ghosts())
 
715
        self.assertEqual(set(['present']),
 
716
            search.stop_searching_any(['present']))
 
717
        self.assertEqual((set(['other']), set(['other_ghost'])),
 
718
            search.start_searching(['other', 'other_ghost']))
 
719
        self.assertEqual((set(['other_2']), set()), search.next_with_ghosts())
 
720
        self.assertRaises(StopIteration, search.next_with_ghosts)
 
721
        # next includes them
 
722
        search = graph._make_breadth_first_searcher(['head'])
 
723
        self.assertEqual(set(['head']), search.next())
 
724
        self.assertEqual(set(['present']), search.next())
 
725
        self.assertEqual(set(['present']),
 
726
            search.stop_searching_any(['present']))
 
727
        search.start_searching(['other', 'other_ghost'])
 
728
        self.assertEqual(set(['other_2']), search.next())
 
729
        self.assertRaises(StopIteration, search.next)
 
730
 
 
731
    def assertSeenAndResult(self, instructions, search, next):
 
732
        """Check the results of .seen and get_result() for a seach.
 
733
 
 
734
        :param instructions: A list of tuples:
 
735
            (seen, recipe, included_keys, starts, stops).
 
736
            seen, recipe and included_keys are results to check on the search
 
737
            and the searches get_result(). starts and stops are parameters to
 
738
            pass to start_searching and stop_searching_any during each
 
739
            iteration, if they are not None.
 
740
        :param search: The search to use.
 
741
        :param next: A callable to advance the search.
 
742
        """
 
743
        for seen, recipe, included_keys, starts, stops in instructions:
 
744
            next()
 
745
            if starts is not None:
 
746
                search.start_searching(starts)
 
747
            if stops is not None:
 
748
                search.stop_searching_any(stops)
 
749
            result = search.get_result()
 
750
            self.assertEqual(recipe, result.get_recipe())
 
751
            self.assertEqual(set(included_keys), result.get_keys())
 
752
            self.assertEqual(seen, search.seen)
 
753
 
 
754
    def test_breadth_first_get_result_excludes_current_pending(self):
 
755
        graph = self.make_graph({
 
756
            'head':['child'],
 
757
            'child':[NULL_REVISION],
 
758
            NULL_REVISION:[],
 
759
            })
 
760
        search = graph._make_breadth_first_searcher(['head'])
 
761
        # At the start, nothing has been seen, to its all excluded:
 
762
        result = search.get_result()
 
763
        self.assertEqual((set(['head']), set(['head']), 0),
 
764
            result.get_recipe())
 
765
        self.assertEqual(set(), result.get_keys())
 
766
        self.assertEqual(set(), search.seen)
 
767
        # using next:
 
768
        expected = [
 
769
            (set(['head']), (set(['head']), set(['child']), 1),
 
770
             ['head'], None, None),
 
771
            (set(['head', 'child']), (set(['head']), set([NULL_REVISION]), 2),
 
772
             ['head', 'child'], None, None),
 
773
            (set(['head', 'child', NULL_REVISION]), (set(['head']), set(), 3),
 
774
             ['head', 'child', NULL_REVISION], None, None),
 
775
            ]
 
776
        self.assertSeenAndResult(expected, search, search.next)
 
777
        # using next_with_ghosts:
 
778
        search = graph._make_breadth_first_searcher(['head'])
 
779
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
 
780
 
 
781
    def test_breadth_first_get_result_starts_stops(self):
 
782
        graph = self.make_graph({
 
783
            'head':['child'],
 
784
            'child':[NULL_REVISION],
 
785
            'otherhead':['otherchild'],
 
786
            'otherchild':['excluded'],
 
787
            'excluded':[NULL_REVISION],
 
788
            NULL_REVISION:[]
 
789
            })
 
790
        search = graph._make_breadth_first_searcher([])
 
791
        # Starting with nothing and adding a search works:
 
792
        search.start_searching(['head'])
 
793
        # head has been seen:
 
794
        result = search.get_result()
 
795
        self.assertEqual((set(['head']), set(['child']), 1),
 
796
            result.get_recipe())
 
797
        self.assertEqual(set(['head']), result.get_keys())
 
798
        self.assertEqual(set(['head']), search.seen)
 
799
        # using next:
 
800
        expected = [
 
801
            # stop at child, and start a new search at otherhead:
 
802
            # - otherhead counts as seen immediately when start_searching is
 
803
            # called.
 
804
            (set(['head', 'child', 'otherhead']),
 
805
             (set(['head', 'otherhead']), set(['child', 'otherchild']), 2),
 
806
             ['head', 'otherhead'], ['otherhead'], ['child']),
 
807
            (set(['head', 'child', 'otherhead', 'otherchild']),
 
808
             (set(['head', 'otherhead']), set(['child', 'excluded']), 3),
 
809
             ['head', 'otherhead', 'otherchild'], None, None),
 
810
            # stop searching excluded now
 
811
            (set(['head', 'child', 'otherhead', 'otherchild', 'excluded']),
 
812
             (set(['head', 'otherhead']), set(['child', 'excluded']), 3),
 
813
             ['head', 'otherhead', 'otherchild'], None, ['excluded']),
 
814
            ]
 
815
        self.assertSeenAndResult(expected, search, search.next)
 
816
        # using next_with_ghosts:
 
817
        search = graph._make_breadth_first_searcher([])
 
818
        search.start_searching(['head'])
 
819
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
 
820
 
 
821
    def test_breadth_first_stop_searching_not_queried(self):
 
822
        # A client should be able to say 'stop node X' even if X has not been
 
823
        # returned to the client.
 
824
        graph = self.make_graph({
 
825
            'head':['child', 'ghost1'],
 
826
            'child':[NULL_REVISION],
 
827
            NULL_REVISION:[],
 
828
            })
 
829
        search = graph._make_breadth_first_searcher(['head'])
 
830
        expected = [
 
831
            # NULL_REVISION and ghost1 have not been returned
 
832
            (set(['head']), (set(['head']), set(['child', 'ghost1']), 1),
 
833
             ['head'], None, [NULL_REVISION, 'ghost1']),
 
834
            # ghost1 has been returned, NULL_REVISION is to be returned in the
 
835
            # next iteration.
 
836
            (set(['head', 'child', 'ghost1']),
 
837
             (set(['head']), set(['ghost1', NULL_REVISION]), 2),
 
838
             ['head', 'child'], None, [NULL_REVISION, 'ghost1']),
 
839
            ]
 
840
        self.assertSeenAndResult(expected, search, search.next)
 
841
        # using next_with_ghosts:
 
842
        search = graph._make_breadth_first_searcher(['head'])
 
843
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
 
844
 
 
845
    def test_breadth_first_get_result_ghosts_are_excluded(self):
 
846
        graph = self.make_graph({
 
847
            'head':['child', 'ghost'],
 
848
            'child':[NULL_REVISION],
 
849
            NULL_REVISION:[],
 
850
            })
 
851
        search = graph._make_breadth_first_searcher(['head'])
 
852
        # using next:
 
853
        expected = [
 
854
            (set(['head']),
 
855
             (set(['head']), set(['ghost', 'child']), 1),
 
856
             ['head'], None, None),
 
857
            (set(['head', 'child', 'ghost']),
 
858
             (set(['head']), set([NULL_REVISION, 'ghost']), 2),
 
859
             ['head', 'child'], None, None),
 
860
            ]
 
861
        self.assertSeenAndResult(expected, search, search.next)
 
862
        # using next_with_ghosts:
 
863
        search = graph._make_breadth_first_searcher(['head'])
 
864
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
 
865
 
 
866
    def test_breadth_first_get_result_starting_a_ghost_ghost_is_excluded(self):
 
867
        graph = self.make_graph({
 
868
            'head':['child'],
 
869
            'child':[NULL_REVISION],
 
870
            NULL_REVISION:[],
 
871
            })
 
872
        search = graph._make_breadth_first_searcher(['head'])
 
873
        # using next:
 
874
        expected = [
 
875
            (set(['head', 'ghost']),
 
876
             (set(['head', 'ghost']), set(['child', 'ghost']), 1),
 
877
             ['head'], ['ghost'], None),
 
878
            (set(['head', 'child', 'ghost']),
 
879
             (set(['head', 'ghost']), set([NULL_REVISION, 'ghost']), 2),
 
880
             ['head', 'child'], None, None),
 
881
            ]
 
882
        self.assertSeenAndResult(expected, search, search.next)
 
883
        # using next_with_ghosts:
 
884
        search = graph._make_breadth_first_searcher(['head'])
 
885
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
 
886
 
 
887
    def test_breadth_first_revision_count_includes_NULL_REVISION(self):
 
888
        graph = self.make_graph({
 
889
            'head':[NULL_REVISION],
 
890
            NULL_REVISION:[],
 
891
            })
 
892
        search = graph._make_breadth_first_searcher(['head'])
 
893
        # using next:
 
894
        expected = [
 
895
            (set(['head']),
 
896
             (set(['head']), set([NULL_REVISION]), 1),
 
897
             ['head'], None, None),
 
898
            (set(['head', NULL_REVISION]),
 
899
             (set(['head']), set([]), 2),
 
900
             ['head', NULL_REVISION], None, None),
 
901
            ]
 
902
        self.assertSeenAndResult(expected, search, search.next)
 
903
        # using next_with_ghosts:
 
904
        search = graph._make_breadth_first_searcher(['head'])
 
905
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
 
906
 
 
907
    def test_breadth_first_search_get_result_after_StopIteration(self):
 
908
        # StopIteration should not invalid anything..
 
909
        graph = self.make_graph({
 
910
            'head':[NULL_REVISION],
 
911
            NULL_REVISION:[],
 
912
            })
 
913
        search = graph._make_breadth_first_searcher(['head'])
 
914
        # using next:
 
915
        expected = [
 
916
            (set(['head']),
 
917
             (set(['head']), set([NULL_REVISION]), 1),
 
918
             ['head'], None, None),
 
919
            (set(['head', 'ghost', NULL_REVISION]),
 
920
             (set(['head', 'ghost']), set(['ghost']), 2),
 
921
             ['head', NULL_REVISION], ['ghost'], None),
 
922
            ]
 
923
        self.assertSeenAndResult(expected, search, search.next)
 
924
        self.assertRaises(StopIteration, search.next)
 
925
        self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
 
926
        result = search.get_result()
 
927
        self.assertEqual((set(['ghost', 'head']), set(['ghost']), 2),
 
928
            result.get_recipe())
 
929
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
 
930
        # using next_with_ghosts:
 
931
        search = graph._make_breadth_first_searcher(['head'])
 
932
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
 
933
        self.assertRaises(StopIteration, search.next)
 
934
        self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
 
935
        result = search.get_result()
 
936
        self.assertEqual((set(['ghost', 'head']), set(['ghost']), 2),
 
937
            result.get_recipe())
 
938
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
 
939
 
 
940
 
 
941
class TestCachingParentsProvider(tests.TestCase):
 
942
 
 
943
    def setUp(self):
 
944
        super(TestCachingParentsProvider, self).setUp()
 
945
        dict_pp = _mod_graph.DictParentsProvider({'a':('b',)})
 
946
        self.inst_pp = InstrumentedParentsProvider(dict_pp)
 
947
        self.caching_pp = _mod_graph.CachingParentsProvider(self.inst_pp)
 
948
 
 
949
    def test_get_parent_map(self):
 
950
        """Requesting the same revision should be returned from cache"""
 
951
        self.assertEqual({}, self.caching_pp._cache)
 
952
        self.assertEqual({'a':('b',)}, self.caching_pp.get_parent_map(['a']))
 
953
        self.assertEqual(['a'], self.inst_pp.calls)
 
954
        self.assertEqual({'a':('b',)}, self.caching_pp.get_parent_map(['a']))
 
955
        # No new call, as it should have been returned from the cache
 
956
        self.assertEqual(['a'], self.inst_pp.calls)
 
957
        self.assertEqual({'a':('b',)}, self.caching_pp._cache)
 
958
 
 
959
    def test_get_parent_map_not_present(self):
 
960
        """The cache should also track when a revision doesn't exist"""
 
961
        self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
 
962
        self.assertEqual(['b'], self.inst_pp.calls)
 
963
        self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
 
964
        # No new calls
 
965
        self.assertEqual(['b'], self.inst_pp.calls)
 
966
        self.assertEqual({'b':None}, self.caching_pp._cache)
 
967
 
 
968
    def test_get_parent_map_mixed(self):
 
969
        """Anything that can be returned from cache, should be"""
 
970
        self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
 
971
        self.assertEqual(['b'], self.inst_pp.calls)
 
972
        self.assertEqual({'a':('b',)},
 
973
                         self.caching_pp.get_parent_map(['a', 'b']))
 
974
        self.assertEqual(['b', 'a'], self.inst_pp.calls)
 
975
 
 
976
    def test_get_parent_map_repeated(self):
 
977
        """Asking for the same parent 2x will only forward 1 request."""
 
978
        self.assertEqual({'a':('b',)},
 
979
                         self.caching_pp.get_parent_map(['b', 'a', 'b']))
 
980
        # Use sorted because we don't care about the order, just that each is
 
981
        # only present 1 time.
 
982
        self.assertEqual(['a', 'b'], sorted(self.inst_pp.calls))