618
618
class TestRevisionHistoryCaching(TestCaseWithBranch):
619
"""Tests for the caching of branch revision_history.
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
626
When not locked, obviously the revision_history will need to be regenerated
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.
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.
638
:returns: a tuple of (the branch, list that calls will be logged to)
622
640
branch = self.get_branch()
624
642
real_gen_revision_history = branch._gen_revision_history
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(
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()
653
668
branch.revision_history()
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.
679
branch, calls = self.get_instrumented_branch()
680
# Lock the branch, set the revision history, then repeatedly call
683
branch.set_revision_history([])
685
branch.revision_history()
686
self.assertEqual([], calls)
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.
694
branch, calls = self.get_instrumented_branch()
695
# Lock the branch, set the revision history, then repeatedly call
697
branch.set_revision_history([])
698
branch.revision_history()
699
self.assertEqual(['_gen_revision_history'], calls)
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.
707
branch, calls = self.get_instrumented_branch()
708
# Lock the branch, set the revision history, then repeatedly call
711
branch.set_last_revision_info(0, None)
714
branch.last_revision_info()
715
self.assertEqual([], calls)
660
720
class TestBranchPushLocations(TestCaseWithBranch):