~bzr-pqm/bzr/bzr.dev

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