~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_branch/test_revision_history.py

  • Committer: Ross Lagerwall
  • Date: 2012-08-07 06:32:51 UTC
  • mto: (6437.63.5 2.5)
  • mto: This revision was merged to the branch mainline in revision 6558.
  • Revision ID: rosslagerwall@gmail.com-20120807063251-x9p03ghg2ws8oqjc
Add bzrlib/locale to .bzrignore

bzrlib/locale is generated with ./setup.py build_mo which is in turn called
by ./setup.py build

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2010 Canonical Ltd
 
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests for Branch.revision_history and last_revision."""
 
18
 
 
19
from bzrlib import (
 
20
    branch,
 
21
    errors,
 
22
    revision as _mod_revision,
 
23
    )
 
24
from bzrlib.symbol_versioning import deprecated_in
 
25
from bzrlib.tests import (
 
26
    per_branch,
 
27
    TestNotApplicable,
 
28
    )
 
29
 
 
30
 
 
31
class TestLastRevision(per_branch.TestCaseWithBranch):
 
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(
 
46
            self.applyDeprecated(deprecated_in((2, 5, 0)), br.revision_history),
 
47
            ["rev1",])
 
48
        br.set_last_revision_info(3, 'rev3')
 
49
        self.assertEquals(
 
50
            self.applyDeprecated(deprecated_in((2, 5, 0)), br.revision_history),
 
51
            ["rev1", "rev2", "rev3"])
 
52
        # append_revision specifically prohibits some ids;
 
53
        # set_last_revision_info currently does not
 
54
        ## self.assertRaises(errors.ReservedId,
 
55
        ##         br.set_last_revision_info, 4, 'current:')
 
56
 
 
57
 
 
58
class TestRevisionHistoryCaching(per_branch.TestCaseWithBranch):
 
59
    """Tests for the caching of branches' revision_history.
 
60
 
 
61
    When locked, branches should avoid regenerating or rereading
 
62
    revision_history by caching the last value of it.  This is safe because
 
63
    the branch is locked, so nothing can change the revision_history
 
64
    unexpectedly.
 
65
 
 
66
    When not locked, obviously the revision_history will need to be regenerated
 
67
    or reread each time.
 
68
 
 
69
    We test if revision_history is using the cache by instrumenting the branch's
 
70
    _gen_revision_history method, which is called by Branch.revision_history if
 
71
    the branch does not have a cache of the revision history.
 
72
    """
 
73
 
 
74
    def get_instrumented_branch(self):
 
75
        """Get a branch and monkey patch it to log calls to
 
76
        _gen_revision_history.
 
77
 
 
78
        :returns: a tuple of (the branch, list that calls will be logged to)
 
79
        """
 
80
        branch = self.get_branch()
 
81
        calls = []
 
82
        real_gen_revision_history = branch._gen_revision_history
 
83
        def fake_gen_revision_history():
 
84
            calls.append('_gen_revision_history')
 
85
            return real_gen_revision_history()
 
86
        branch._gen_revision_history = fake_gen_revision_history
 
87
        return branch, calls
 
88
 
 
89
    def test_revision_history_when_unlocked(self):
 
90
        """Repeated calls to revision history will call _gen_revision_history
 
91
        each time when the branch is not locked.
 
92
        """
 
93
        branch, calls = self.get_instrumented_branch()
 
94
        # Repeatedly call revision_history.
 
95
        self.applyDeprecated(deprecated_in((2, 5, 0)), branch.revision_history)
 
96
        self.applyDeprecated(deprecated_in((2, 5, 0)), branch.revision_history)
 
97
        self.assertEqual(
 
98
            ['_gen_revision_history', '_gen_revision_history'], calls)
 
99
 
 
100
    def test_revision_history_when_locked(self):
 
101
        """Repeated calls to revision history will only call
 
102
        _gen_revision_history once while the branch is locked.
 
103
        """
 
104
        branch, calls = self.get_instrumented_branch()
 
105
        # Lock the branch, then repeatedly call revision_history.
 
106
        branch.lock_read()
 
107
        try:
 
108
            self.applyDeprecated(deprecated_in((2, 5, 0)),
 
109
                branch.revision_history)
 
110
            self.applyDeprecated(deprecated_in((2, 5, 0)),
 
111
                branch.revision_history)
 
112
            self.assertEqual(['_gen_revision_history'], calls)
 
113
        finally:
 
114
            branch.unlock()
 
115
 
 
116
    def test_set_revision_history_when_locked(self):
 
117
        """When the branch is locked, calling set_revision_history should cache
 
118
        the revision history so that a later call to revision_history will not
 
119
        need to call _gen_revision_history.
 
120
        """
 
121
        branch, calls = self.get_instrumented_branch()
 
122
        # Lock the branch, set the revision history, then repeatedly call
 
123
        # revision_history.
 
124
        branch.lock_write()
 
125
        self.applyDeprecated(deprecated_in((2, 4, 0)),
 
126
            branch.set_revision_history, [])
 
127
        try:
 
128
            self.applyDeprecated(deprecated_in((2, 5, 0)),
 
129
                branch.revision_history)
 
130
            self.assertEqual([], calls)
 
131
        finally:
 
132
            branch.unlock()
 
133
 
 
134
    def test_set_revision_history_when_unlocked(self):
 
135
        """When the branch is not locked, calling set_revision_history will not
 
136
        cause the revision history to be cached.
 
137
        """
 
138
        branch, calls = self.get_instrumented_branch()
 
139
        self.applyDeprecated(deprecated_in((2, 4, 0)),
 
140
            branch.set_revision_history, [])
 
141
        self.applyDeprecated(deprecated_in((2, 5, 0)),
 
142
            branch.revision_history)
 
143
        self.assertEqual(['_gen_revision_history'], calls)
 
144
 
 
145
    def test_set_last_revision_info_when_locked(self):
 
146
        """When the branch is locked, calling set_last_revision_info should
 
147
        cache the last revision info so that a later call to last_revision_info
 
148
        will not need the revision_history.  Thus the branch will not to call
 
149
        _gen_revision_history in this situation.
 
150
        """
 
151
        a_branch, calls = self.get_instrumented_branch()
 
152
        # Lock the branch, set the last revision info, then call
 
153
        # last_revision_info.
 
154
        a_branch.lock_write()
 
155
        a_branch.set_last_revision_info(0, _mod_revision.NULL_REVISION)
 
156
        del calls[:]
 
157
        try:
 
158
            a_branch.last_revision_info()
 
159
            self.assertEqual([], calls)
 
160
        finally:
 
161
            a_branch.unlock()
 
162
 
 
163
    def test_set_last_revision_info_none(self):
 
164
        """Passing None to set_last_revision_info raises an exception."""
 
165
        a_branch = self.get_branch()
 
166
        # Lock the branch, set the last revision info, then call
 
167
        # last_revision_info.
 
168
        a_branch.lock_write()
 
169
        self.addCleanup(a_branch.unlock)
 
170
        self.assertRaises(errors.InvalidRevisionId,
 
171
            a_branch.set_last_revision_info, 0, None)
 
172
 
 
173
    def test_set_last_revision_info_uncaches_revision_history_for_format6(self):
 
174
        """On format 6 branches, set_last_revision_info invalidates the revision
 
175
        history cache.
 
176
        """
 
177
        if not isinstance(self.branch_format, branch.BzrBranchFormat6):
 
178
            return
 
179
        a_branch, calls = self.get_instrumented_branch()
 
180
        # Lock the branch, cache the revision history.
 
181
        a_branch.lock_write()
 
182
        self.applyDeprecated(deprecated_in((2, 5, 0)),
 
183
            a_branch.revision_history)
 
184
        # Set the last revision info, clearing the cache.
 
185
        a_branch.set_last_revision_info(0, _mod_revision.NULL_REVISION)
 
186
        del calls[:]
 
187
        try:
 
188
            self.applyDeprecated(deprecated_in((2, 5, 0)),
 
189
                a_branch.revision_history)
 
190
            self.assertEqual(['_gen_revision_history'], calls)
 
191
        finally:
 
192
            a_branch.unlock()
 
193
 
 
194
    def test_cached_revision_history_not_accidentally_mutable(self):
 
195
        """When there's a cached version of the history, revision_history
 
196
        returns a copy of the cached data so that callers cannot accidentally
 
197
        corrupt the cache.
 
198
        """
 
199
        branch = self.get_branch()
 
200
        # Lock the branch, then repeatedly call revision_history, mutating the
 
201
        # results.
 
202
        branch.lock_read()
 
203
        try:
 
204
            # The first time the data returned will not be in the cache.
 
205
            history = self.applyDeprecated(deprecated_in((2, 5, 0)),
 
206
                branch.revision_history)
 
207
            history.append('one')
 
208
            # The second time the data comes from the cache.
 
209
            history = self.applyDeprecated(deprecated_in((2, 5, 0)),
 
210
                branch.revision_history)
 
211
            history.append('two')
 
212
            # The revision_history() should still be unchanged, even though
 
213
            # we've mutated the return values from earlier calls.
 
214
            self.assertEqual([], self.applyDeprecated(
 
215
                deprecated_in((2, 5, 0)), branch.revision_history))
 
216
        finally:
 
217
            branch.unlock()
 
218
 
 
219
 
 
220
class TestRevisionHistory(per_branch.TestCaseWithBranch):
 
221
 
 
222
    def test_parent_ghost(self):
 
223
        tree = self.make_branch_and_tree('tree')
 
224
        if not tree.branch.repository._format.supports_ghosts:
 
225
            raise TestNotApplicable("repository format does not support ghosts")
 
226
        tree.add_parent_tree_id('ghost-revision',
 
227
                                allow_leftmost_as_ghost=True)
 
228
        tree.commit('first non-ghost commit', rev_id='non-ghost-revision')
 
229
        self.assertEqual(['non-ghost-revision'],
 
230
                         self.applyDeprecated(deprecated_in((2, 5, 0)),
 
231
                            tree.branch.revision_history))