566
566
self.assertEqual(['lw', 'ul'], branch._calls)
569
class TestRevisionHistoryCaching(TestCaseWithBranch):
570
"""Tests for the caching of branch revision_history.
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
577
When not locked, obviously the revision_history will need to be regenerated
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.
585
def get_instrumented_branch(self):
586
"""Get a branch and monkey patch it to log calls to
587
_gen_revision_history.
589
:returns: a tuple of (the branch, list that calls will be logged to)
591
branch = self.get_branch()
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
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.
604
branch, calls = self.get_instrumented_branch()
605
# Repeatedly call revision_history.
606
branch.revision_history()
607
branch.revision_history()
609
['_gen_revision_history', '_gen_revision_history'], calls)
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.
615
branch, calls = self.get_instrumented_branch()
616
# Lock the branch, then repeatedly call revision_history.
619
branch.revision_history()
620
branch.revision_history()
621
self.assertEqual(['_gen_revision_history'], calls)
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.
630
branch, calls = self.get_instrumented_branch()
631
# Lock the branch, set the revision history, then repeatedly call
634
branch.set_revision_history([])
636
branch.revision_history()
637
self.assertEqual([], calls)
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.
645
branch, calls = self.get_instrumented_branch()
646
# Lock the branch, set the revision history, then repeatedly call
648
branch.set_revision_history([])
649
branch.revision_history()
650
self.assertEqual(['_gen_revision_history'], calls)
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.
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)
665
a_branch.last_revision_info()
666
self.assertEqual([], calls)
670
def test_set_last_revision_info_uncaches_revision_history_for_format6(self):
671
if not isinstance(self.branch_format, branch.BzrBranchFormat6):
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)
681
a_branch.revision_history()
682
self.assertEqual(['_gen_revision_history'], calls)
689
569
class TestBranchPushLocations(TestCaseWithBranch):
691
571
def test_get_push_location_unset(self):