1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# Copyright (C) 2007-2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Tests for Branch.revision_history and last_revision."""
from bzrlib import (
branch,
errors,
revision as _mod_revision,
)
from bzrlib.symbol_versioning import deprecated_in
from bzrlib.tests import per_branch
class TestLastRevision(per_branch.TestCaseWithBranch):
"""Tests for the last_revision property of the branch.
"""
def test_set_last_revision_info(self):
# based on TestBranch.test_append_revisions, which uses the old
# append_revision api
wt = self.make_branch_and_tree('tree')
wt.commit('f', rev_id='rev1')
wt.commit('f', rev_id='rev2')
wt.commit('f', rev_id='rev3')
br = self.get_branch()
br.fetch(wt.branch)
br.set_last_revision_info(1, 'rev1')
self.assertEquals(br.revision_history(), ["rev1",])
br.set_last_revision_info(3, 'rev3')
self.assertEquals(br.revision_history(), ["rev1", "rev2", "rev3"])
# append_revision specifically prohibits some ids;
# set_last_revision_info currently does not
## self.assertRaises(errors.ReservedId,
## br.set_last_revision_info, 4, 'current:')
class TestRevisionHistoryCaching(per_branch.TestCaseWithBranch):
"""Tests for the caching of branches' revision_history.
When locked, branches should avoid regenerating or rereading
revision_history by caching the last value of it. This is safe because
the branch is locked, so nothing can change the revision_history
unexpectedly.
When not locked, obviously the revision_history will need to be regenerated
or reread each time.
We test if revision_history is using the cache by instrumenting the branch's
_gen_revision_history method, which is called by Branch.revision_history if
the branch does not have a cache of the revision history.
"""
def get_instrumented_branch(self):
"""Get a branch and monkey patch it to log calls to
_gen_revision_history.
:returns: a tuple of (the branch, list that calls will be logged to)
"""
branch = self.get_branch()
calls = []
real_gen_revision_history = branch._gen_revision_history
def fake_gen_revision_history():
calls.append('_gen_revision_history')
return real_gen_revision_history()
branch._gen_revision_history = fake_gen_revision_history
return branch, calls
def test_revision_history_when_unlocked(self):
"""Repeated calls to revision history will call _gen_revision_history
each time when the branch is not locked.
"""
branch, calls = self.get_instrumented_branch()
# Repeatedly call revision_history.
branch.revision_history()
branch.revision_history()
self.assertEqual(
['_gen_revision_history', '_gen_revision_history'], calls)
def test_revision_history_when_locked(self):
"""Repeated calls to revision history will only call
_gen_revision_history once while the branch is locked.
"""
branch, calls = self.get_instrumented_branch()
# Lock the branch, then repeatedly call revision_history.
branch.lock_read()
try:
branch.revision_history()
branch.revision_history()
self.assertEqual(['_gen_revision_history'], calls)
finally:
branch.unlock()
def test_set_revision_history_when_locked(self):
"""When the branch is locked, calling set_revision_history should cache
the revision history so that a later call to revision_history will not
need to call _gen_revision_history.
"""
branch, calls = self.get_instrumented_branch()
# Lock the branch, set the revision history, then repeatedly call
# revision_history.
branch.lock_write()
self.applyDeprecated(deprecated_in((2, 4, 0)),
branch.set_revision_history, [])
try:
branch.revision_history()
self.assertEqual([], calls)
finally:
branch.unlock()
def test_set_revision_history_when_unlocked(self):
"""When the branch is not locked, calling set_revision_history will not
cause the revision history to be cached.
"""
branch, calls = self.get_instrumented_branch()
self.applyDeprecated(deprecated_in((2, 4, 0)),
branch.set_revision_history, [])
branch.revision_history()
self.assertEqual(['_gen_revision_history'], calls)
def test_set_last_revision_info_when_locked(self):
"""When the branch is locked, calling set_last_revision_info should
cache the last revision info so that a later call to last_revision_info
will not need the revision_history. Thus the branch will not to call
_gen_revision_history in this situation.
"""
a_branch, calls = self.get_instrumented_branch()
# Lock the branch, set the last revision info, then call
# last_revision_info.
a_branch.lock_write()
a_branch.set_last_revision_info(0, _mod_revision.NULL_REVISION)
del calls[:]
try:
a_branch.last_revision_info()
self.assertEqual([], calls)
finally:
a_branch.unlock()
def test_set_last_revision_info_none(self):
"""Passing None to set_last_revision_info raises an exception."""
a_branch = self.get_branch()
# Lock the branch, set the last revision info, then call
# last_revision_info.
a_branch.lock_write()
self.addCleanup(a_branch.unlock)
self.assertRaises(errors.InvalidRevisionId,
a_branch.set_last_revision_info, 0, None)
def test_set_last_revision_info_uncaches_revision_history_for_format6(self):
"""On format 6 branches, set_last_revision_info invalidates the revision
history cache.
"""
if not isinstance(self.branch_format, branch.BzrBranchFormat6):
return
a_branch, calls = self.get_instrumented_branch()
# Lock the branch, cache the revision history.
a_branch.lock_write()
a_branch.revision_history()
# Set the last revision info, clearing the cache.
a_branch.set_last_revision_info(0, _mod_revision.NULL_REVISION)
del calls[:]
try:
a_branch.revision_history()
self.assertEqual(['_gen_revision_history'], calls)
finally:
a_branch.unlock()
def test_cached_revision_history_not_accidentally_mutable(self):
"""When there's a cached version of the history, revision_history
returns a copy of the cached data so that callers cannot accidentally
corrupt the cache.
"""
branch = self.get_branch()
# Lock the branch, then repeatedly call revision_history, mutating the
# results.
branch.lock_read()
try:
# The first time the data returned will not be in the cache.
history = branch.revision_history()
history.append('one')
# The second time the data comes from the cache.
history = branch.revision_history()
history.append('two')
# The revision_history() should still be unchanged, even though
# we've mutated the return values from earlier calls.
self.assertEqual([], branch.revision_history())
finally:
branch.unlock()
class TestRevisionHistory(per_branch.TestCaseWithBranch):
def test_parent_ghost(self):
tree = self.make_branch_and_tree('tree')
tree.add_parent_tree_id('ghost-revision',
allow_leftmost_as_ghost=True)
tree.commit('first non-ghost commit', rev_id='non-ghost-revision')
self.assertEqual(['non-ghost-revision'],
tree.branch.revision_history())
|