~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/branch_implementations/test_branch.py

  • Committer: Andrew Bennetts
  • Date: 2007-03-26 06:24:01 UTC
  • mto: This revision was merged to the branch mainline in revision 2376.
  • Revision ID: andrew.bennetts@canonical.com-20070326062401-k3nbefzje5332jaf
Deal with review comments from Robert:

  * Add my name to the NEWS file
  * Move the test case to a new module in branch_implementations
  * Remove revision_history cruft from identitymap and test_identitymap
  * Improve some docstrings

Also, this fixes a bug where revision_history was not returning a copy of the
cached data, allowing the cache to be corrupted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
566
566
        self.assertEqual(['lw', 'ul'], branch._calls)
567
567
 
568
568
 
569
 
class TestRevisionHistoryCaching(TestCaseWithBranch):
570
 
    """Tests for the caching of branch revision_history.
571
 
 
572
 
    When locked, branches should avoid regenerating or rereading
573
 
    revision_history by caching the last value of it.  This is safe because
574
 
    the branch is locked, so nothing can change the revision_history
575
 
    unexpectedly.
576
 
 
577
 
    When not locked, obviously the revision_history will need to be regenerated
578
 
    or reread each time.
579
 
 
580
 
    We test if revision_history is using the cache by instrumenting the branch's
581
 
    _gen_revision_history method, which is called by Branch.revision_history if
582
 
    the branch does not have a cache of the revision history.
583
 
    """
584
 
 
585
 
    def get_instrumented_branch(self):
586
 
        """Get a branch and monkey patch it to log calls to
587
 
        _gen_revision_history.
588
 
 
589
 
        :returns: a tuple of (the branch, list that calls will be logged to)
590
 
        """
591
 
        branch = self.get_branch()
592
 
        calls = []
593
 
        real_gen_revision_history = branch._gen_revision_history
594
 
        def fake_gen_revision_history():
595
 
            calls.append('_gen_revision_history')
596
 
            return real_gen_revision_history()
597
 
        branch._gen_revision_history = fake_gen_revision_history
598
 
        return branch, calls
599
 
 
600
 
    def test_revision_history_when_unlocked(self):
601
 
        """Repeated calls to revision history will call _gen_revision_history
602
 
        each time when the branch is not locked.
603
 
        """
604
 
        branch, calls = self.get_instrumented_branch()
605
 
        # Repeatedly call revision_history.
606
 
        branch.revision_history()
607
 
        branch.revision_history()
608
 
        self.assertEqual(
609
 
            ['_gen_revision_history', '_gen_revision_history'], calls)
610
 
 
611
 
    def test_revision_history_when_locked(self):
612
 
        """Repeated calls to revision history will only call
613
 
        _gen_revision_history once while the branch is locked.
614
 
        """
615
 
        branch, calls = self.get_instrumented_branch()
616
 
        # Lock the branch, then repeatedly call revision_history.
617
 
        branch.lock_read()
618
 
        try:
619
 
            branch.revision_history()
620
 
            branch.revision_history()
621
 
            self.assertEqual(['_gen_revision_history'], calls)
622
 
        finally:
623
 
            branch.unlock()
624
 
 
625
 
    def test_set_revision_history_when_locked(self):
626
 
        """When the branch is locked, calling set_revision_history should cache
627
 
        the revision history so that a later call to revision_history will not
628
 
        need to call _gen_revision_history.
629
 
        """
630
 
        branch, calls = self.get_instrumented_branch()
631
 
        # Lock the branch, set the revision history, then repeatedly call
632
 
        # revision_history.
633
 
        branch.lock_write()
634
 
        branch.set_revision_history([])
635
 
        try:
636
 
            branch.revision_history()
637
 
            self.assertEqual([], calls)
638
 
        finally:
639
 
            branch.unlock()
640
 
 
641
 
    def test_set_revision_history_when_unlocked(self):
642
 
        """When the branch is not locked, calling set_revision_history will not
643
 
        cause the revision history to be cached.
644
 
        """
645
 
        branch, calls = self.get_instrumented_branch()
646
 
        # Lock the branch, set the revision history, then repeatedly call
647
 
        # revision_history.
648
 
        branch.set_revision_history([])
649
 
        branch.revision_history()
650
 
        self.assertEqual(['_gen_revision_history'], calls)
651
 
 
652
 
    def test_set_last_revision_info_when_locked(self):
653
 
        """When the branch is locked, calling set_last_revision_info should
654
 
        cache the last revision info so that a later call to last_revision_info
655
 
        will not need the revision_history.  Thus the branch will not to call
656
 
        _gen_revision_history in this situation.
657
 
        """
658
 
        a_branch, calls = self.get_instrumented_branch()
659
 
        # Lock the branch, set the last revision info, then call
660
 
        # last_revision_info.
661
 
        a_branch.lock_write()
662
 
        a_branch.set_last_revision_info(0, None)
663
 
        del calls[:]
664
 
        try:
665
 
            a_branch.last_revision_info()
666
 
            self.assertEqual([], calls)
667
 
        finally:
668
 
            a_branch.unlock()
669
 
 
670
 
    def test_set_last_revision_info_uncaches_revision_history_for_format6(self):
671
 
        if not isinstance(self.branch_format, branch.BzrBranchFormat6):
672
 
            return
673
 
        a_branch, calls = self.get_instrumented_branch()
674
 
        # Lock the branch, cache the revision history.
675
 
        a_branch.lock_write()
676
 
        a_branch.revision_history()
677
 
        # Set the last revision info, clearing the cache.
678
 
        a_branch.set_last_revision_info(0, None)
679
 
        del calls[:]
680
 
        try:
681
 
            a_branch.revision_history()
682
 
            self.assertEqual(['_gen_revision_history'], calls)
683
 
        finally:
684
 
            a_branch.unlock()
685
 
 
686
 
        
687
 
 
688
 
 
689
569
class TestBranchPushLocations(TestCaseWithBranch):
690
570
 
691
571
    def test_get_push_location_unset(self):