~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 01:41:52 UTC
  • mto: This revision was merged to the branch mainline in revision 2376.
  • Revision ID: andrew.bennetts@canonical.com-20070326014152-yym3dec6270pxjc3
More tests for Branch revision history caching.

Show diffs side-by-side

added added

removed removed

Lines of Context:
616
616
 
617
617
 
618
618
class TestRevisionHistoryCaching(TestCaseWithBranch):
 
619
    """Tests for the caching of branch revision_history.
 
620
 
 
621
    When locked, branches should avoid regenerating or rereading
 
622
    revision_history by caching the last value of it.  This is safe because
 
623
    the branch is locked, so nothing can change the revision_history
 
624
    unexpectedly.
 
625
 
 
626
    When not locked, obviously the revision_history will need to be regenerated
 
627
    or reread each time.
 
628
 
 
629
    We test if revision_history is using the cache by instrumenting the branch's
 
630
    _gen_revision_history method, which is called by Branch.revision_history if
 
631
    the branch does not have a cache of the revision history.
 
632
    """
619
633
 
620
634
    def get_instrumented_branch(self):
621
 
        # Get a branch and monkey patch it to log calls to _gen_revision_history
 
635
        """Get a branch and monkey patch it to log calls to
 
636
        _gen_revision_history.
 
637
 
 
638
        :returns: a tuple of (the branch, list that calls will be logged to)
 
639
        """
622
640
        branch = self.get_branch()
623
641
        calls = []
624
642
        real_gen_revision_history = branch._gen_revision_history
634
652
        """
635
653
        branch, calls = self.get_instrumented_branch()
636
654
        # Repeatedly call revision_history.
637
 
        # _gen_revision_history will called each time, because the branch is not
638
 
        # locked, so the history might have changed between calls.
639
655
        branch.revision_history()
640
656
        branch.revision_history()
641
657
        self.assertEqual(
647
663
        """
648
664
        branch, calls = self.get_instrumented_branch()
649
665
        # Lock the branch, then repeatedly call revision_history.
650
 
        # _gen_revision_history will only be called once.
651
666
        branch.lock_read()
652
667
        try:
653
668
            branch.revision_history()
656
671
        finally:
657
672
            branch.unlock()
658
673
 
 
674
    def test_set_revision_history_when_locked(self):
 
675
        """When the branch is locked, calling set_revision_history should cache
 
676
        the revision history so that a later call to revision_history will not
 
677
        need to call _gen_revision_history.
 
678
        """
 
679
        branch, calls = self.get_instrumented_branch()
 
680
        # Lock the branch, set the revision history, then repeatedly call
 
681
        # revision_history.
 
682
        branch.lock_write()
 
683
        branch.set_revision_history([])
 
684
        try:
 
685
            branch.revision_history()
 
686
            self.assertEqual([], calls)
 
687
        finally:
 
688
            branch.unlock()
 
689
 
 
690
    def test_set_revision_history_when_unlocked(self):
 
691
        """When the branch is not locked, calling set_revision_history will not
 
692
        cause the revision history to be cached.
 
693
        """
 
694
        branch, calls = self.get_instrumented_branch()
 
695
        # Lock the branch, set the revision history, then repeatedly call
 
696
        # revision_history.
 
697
        branch.set_revision_history([])
 
698
        branch.revision_history()
 
699
        self.assertEqual(['_gen_revision_history'], calls)
 
700
 
 
701
    def test_set_last_revision_info_when_locked(self):
 
702
        """When the branch is locked, calling set_last_revision_info should
 
703
        cache the last revision info so that a later call to last_revision_info
 
704
        will not need the revision_history.  Thus the branch will not to call
 
705
        _gen_revision_history in this situation.
 
706
        """
 
707
        branch, calls = self.get_instrumented_branch()
 
708
        # Lock the branch, set the revision history, then repeatedly call
 
709
        # revision_history.
 
710
        branch.lock_write()
 
711
        branch.set_last_revision_info(0, None)
 
712
        del calls[:]
 
713
        try:
 
714
            branch.last_revision_info()
 
715
            self.assertEqual([], calls)
 
716
        finally:
 
717
            branch.unlock()
 
718
 
659
719
 
660
720
class TestBranchPushLocations(TestCaseWithBranch):
661
721