~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
5010.2.19 by Vincent Ladeuil
Fix imports in per_branch/test_revision_history.py.
25
from bzrlib.tests import per_branch
26
27
28
class TestLastRevision(per_branch.TestCaseWithBranch):
2697.2.2 by Martin Pool
deprecate Branch.append_revision
29
    """Tests for the last_revision property of the branch.
30
    """
31
32
    def test_set_last_revision_info(self):
33
        # based on TestBranch.test_append_revisions, which uses the old
34
        # append_revision api
35
        wt = self.make_branch_and_tree('tree')
36
        wt.commit('f', rev_id='rev1')
37
        wt.commit('f', rev_id='rev2')
38
        wt.commit('f', rev_id='rev3')
39
        br = self.get_branch()
40
        br.fetch(wt.branch)
41
        br.set_last_revision_info(1, 'rev1')
42
        self.assertEquals(br.revision_history(), ["rev1",])
43
        br.set_last_revision_info(3, 'rev3')
44
        self.assertEquals(br.revision_history(), ["rev1", "rev2", "rev3"])
45
        # append_revision specifically prohibits some ids;
46
        # set_last_revision_info currently does not
47
        ## self.assertRaises(errors.ReservedId,
48
        ##         br.set_last_revision_info, 4, 'current:')
49
50
5010.2.19 by Vincent Ladeuil
Fix imports in per_branch/test_revision_history.py.
51
class TestRevisionHistoryCaching(per_branch.TestCaseWithBranch):
2375.1.5 by Andrew Bennetts
Deal with review comments from Robert:
52
    """Tests for the caching of branches' revision_history.
53
54
    When locked, branches should avoid regenerating or rereading
55
    revision_history by caching the last value of it.  This is safe because
56
    the branch is locked, so nothing can change the revision_history
57
    unexpectedly.
58
59
    When not locked, obviously the revision_history will need to be regenerated
60
    or reread each time.
61
62
    We test if revision_history is using the cache by instrumenting the branch's
63
    _gen_revision_history method, which is called by Branch.revision_history if
64
    the branch does not have a cache of the revision history.
65
    """
66
67
    def get_instrumented_branch(self):
68
        """Get a branch and monkey patch it to log calls to
69
        _gen_revision_history.
70
71
        :returns: a tuple of (the branch, list that calls will be logged to)
72
        """
73
        branch = self.get_branch()
74
        calls = []
75
        real_gen_revision_history = branch._gen_revision_history
76
        def fake_gen_revision_history():
77
            calls.append('_gen_revision_history')
78
            return real_gen_revision_history()
79
        branch._gen_revision_history = fake_gen_revision_history
80
        return branch, calls
81
82
    def test_revision_history_when_unlocked(self):
83
        """Repeated calls to revision history will call _gen_revision_history
84
        each time when the branch is not locked.
85
        """
86
        branch, calls = self.get_instrumented_branch()
87
        # Repeatedly call revision_history.
88
        branch.revision_history()
89
        branch.revision_history()
90
        self.assertEqual(
91
            ['_gen_revision_history', '_gen_revision_history'], calls)
92
93
    def test_revision_history_when_locked(self):
94
        """Repeated calls to revision history will only call
95
        _gen_revision_history once while the branch is locked.
96
        """
97
        branch, calls = self.get_instrumented_branch()
98
        # Lock the branch, then repeatedly call revision_history.
99
        branch.lock_read()
100
        try:
101
            branch.revision_history()
102
            branch.revision_history()
103
            self.assertEqual(['_gen_revision_history'], calls)
104
        finally:
105
            branch.unlock()
106
107
    def test_set_revision_history_when_locked(self):
108
        """When the branch is locked, calling set_revision_history should cache
109
        the revision history so that a later call to revision_history will not
110
        need to call _gen_revision_history.
111
        """
112
        branch, calls = self.get_instrumented_branch()
113
        # Lock the branch, set the revision history, then repeatedly call
114
        # revision_history.
115
        branch.lock_write()
5718.7.4 by Jelmer Vernooij
Branch.set_revision_history.
116
        self.applyDeprecated(deprecated_in((2, 4, 0)),
117
            branch.set_revision_history, [])
2375.1.5 by Andrew Bennetts
Deal with review comments from Robert:
118
        try:
119
            branch.revision_history()
120
            self.assertEqual([], calls)
121
        finally:
122
            branch.unlock()
123
124
    def test_set_revision_history_when_unlocked(self):
125
        """When the branch is not locked, calling set_revision_history will not
126
        cause the revision history to be cached.
127
        """
128
        branch, calls = self.get_instrumented_branch()
5718.7.4 by Jelmer Vernooij
Branch.set_revision_history.
129
        self.applyDeprecated(deprecated_in((2, 4, 0)),
130
            branch.set_revision_history, [])
2375.1.5 by Andrew Bennetts
Deal with review comments from Robert:
131
        branch.revision_history()
132
        self.assertEqual(['_gen_revision_history'], calls)
133
134
    def test_set_last_revision_info_when_locked(self):
135
        """When the branch is locked, calling set_last_revision_info should
136
        cache the last revision info so that a later call to last_revision_info
137
        will not need the revision_history.  Thus the branch will not to call
138
        _gen_revision_history in this situation.
139
        """
140
        a_branch, calls = self.get_instrumented_branch()
141
        # Lock the branch, set the last revision info, then call
142
        # last_revision_info.
143
        a_branch.lock_write()
3240.1.7 by Aaron Bentley
Update from review comments
144
        a_branch.set_last_revision_info(0, _mod_revision.NULL_REVISION)
2375.1.5 by Andrew Bennetts
Deal with review comments from Robert:
145
        del calls[:]
146
        try:
147
            a_branch.last_revision_info()
148
            self.assertEqual([], calls)
149
        finally:
150
            a_branch.unlock()
151
3240.1.7 by Aaron Bentley
Update from review comments
152
    def test_set_last_revision_info_none(self):
5803.1.1 by Jelmer Vernooij
Raise InvalidRevisionId on Branch.set_last_revision_info.
153
        """Passing None to set_last_revision_info raises an exception."""
3240.1.7 by Aaron Bentley
Update from review comments
154
        a_branch = self.get_branch()
155
        # Lock the branch, set the last revision info, then call
156
        # last_revision_info.
157
        a_branch.lock_write()
158
        self.addCleanup(a_branch.unlock)
5803.1.1 by Jelmer Vernooij
Raise InvalidRevisionId on Branch.set_last_revision_info.
159
        self.assertRaises(errors.InvalidRevisionId,
3240.1.7 by Aaron Bentley
Update from review comments
160
            a_branch.set_last_revision_info, 0, None)
161
2375.1.5 by Andrew Bennetts
Deal with review comments from Robert:
162
    def test_set_last_revision_info_uncaches_revision_history_for_format6(self):
163
        """On format 6 branches, set_last_revision_info invalidates the revision
164
        history cache.
165
        """
166
        if not isinstance(self.branch_format, branch.BzrBranchFormat6):
167
            return
168
        a_branch, calls = self.get_instrumented_branch()
169
        # Lock the branch, cache the revision history.
170
        a_branch.lock_write()
171
        a_branch.revision_history()
172
        # Set the last revision info, clearing the cache.
3240.1.7 by Aaron Bentley
Update from review comments
173
        a_branch.set_last_revision_info(0, _mod_revision.NULL_REVISION)
2375.1.5 by Andrew Bennetts
Deal with review comments from Robert:
174
        del calls[:]
175
        try:
176
            a_branch.revision_history()
177
            self.assertEqual(['_gen_revision_history'], calls)
178
        finally:
179
            a_branch.unlock()
180
181
    def test_cached_revision_history_not_accidentally_mutable(self):
182
        """When there's a cached version of the history, revision_history
183
        returns a copy of the cached data so that callers cannot accidentally
184
        corrupt the cache.
185
        """
186
        branch = self.get_branch()
187
        # Lock the branch, then repeatedly call revision_history, mutating the
188
        # results.
189
        branch.lock_read()
190
        try:
191
            # The first time the data returned will not be in the cache.
192
            history = branch.revision_history()
193
            history.append('one')
194
            # The second time the data comes from the cache.
195
            history = branch.revision_history()
196
            history.append('two')
197
            # The revision_history() should still be unchanged, even though
198
            # we've mutated the return values from earlier calls.
199
            self.assertEqual([], branch.revision_history())
200
        finally:
201
            branch.unlock()
202
203
5010.2.19 by Vincent Ladeuil
Fix imports in per_branch/test_revision_history.py.
204
class TestRevisionHistory(per_branch.TestCaseWithBranch):
2375.1.5 by Andrew Bennetts
Deal with review comments from Robert:
205
3495.2.1 by Aaron Bentley
Tolerate ghosts in mainline (#235055)
206
    def test_parent_ghost(self):
207
        tree = self.make_branch_and_tree('tree')
208
        tree.add_parent_tree_id('ghost-revision',
209
                                allow_leftmost_as_ghost=True)
210
        tree.commit('first non-ghost commit', rev_id='non-ghost-revision')
211
        self.assertEqual(['non-ghost-revision'],
212
                         tree.branch.revision_history())