~bzr-pqm/bzr/bzr.dev

1711.8.5 by John Arbash Meinel
Move the new locking tests into their own files, and move the helper functions into a test helper.
1
# Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Test locks across all branch implemenations"""
18
2381.1.1 by Robert Collins
Split out hpss test fixes which dont depend on new or altered API's.
19
from bzrlib import errors
20
from bzrlib.branch import BzrBranchFormat4
21
from bzrlib.tests import TestSkipped
1711.8.5 by John Arbash Meinel
Move the new locking tests into their own files, and move the helper functions into a test helper.
22
from bzrlib.tests.branch_implementations.test_branch import TestCaseWithBranch
1711.8.7 by John Arbash Meinel
Renaming LockHelpers.py to lock_helpers.py
23
from bzrlib.tests.lock_helpers import TestPreventLocking, LockWrapper
1711.8.5 by John Arbash Meinel
Move the new locking tests into their own files, and move the helper functions into a test helper.
24
25
26
class TestBranchLocking(TestCaseWithBranch):
27
2381.1.1 by Robert Collins
Split out hpss test fixes which dont depend on new or altered API's.
28
    def setUp(self):
29
        TestCaseWithBranch.setUp(self)
30
        self.reduceLockdirTimeout()
31
1711.8.5 by John Arbash Meinel
Move the new locking tests into their own files, and move the helper functions into a test helper.
32
    def get_instrumented_branch(self):
33
        """Get a Branch object which has been instrumented"""
34
        # TODO: jam 20060630 It may be that not all formats have a 
35
        # 'control_files' member. So we should fail gracefully if
36
        # not there. But assuming it has them lets us test the exact 
37
        # lock/unlock order.
38
        self.locks = []
39
        b = LockWrapper(self.locks, self.get_branch(), 'b')
40
        b.repository = LockWrapper(self.locks, b.repository, 'r')
41
        bcf = b.control_files
42
        rcf = b.repository.control_files
43
44
        # Look out for branch types that reuse their control files
45
        self.combined_control = bcf is rcf
46
47
        b.control_files = LockWrapper(self.locks, b.control_files, 'bc')
48
        b.repository.control_files = \
49
            LockWrapper(self.locks, b.repository.control_files, 'rc')
50
        return b
51
52
    def test_01_lock_read(self):
53
        # Test that locking occurs in the correct order
54
        b = self.get_instrumented_branch()
55
56
        self.assertFalse(b.is_locked())
57
        self.assertFalse(b.repository.is_locked())
58
        b.lock_read()
59
        try:
60
            self.assertTrue(b.is_locked())
61
            self.assertTrue(b.repository.is_locked())
62
        finally:
63
            b.unlock()
64
        self.assertFalse(b.is_locked())
65
        self.assertFalse(b.repository.is_locked())
66
67
        self.assertEqual([('b', 'lr', True),
68
                          ('r', 'lr', True),
69
                          ('rc', 'lr', True),
70
                          ('bc', 'lr', True),
71
                          ('b', 'ul', True),
72
                          ('bc', 'ul', True),
73
                          ('r', 'ul', True),
74
                          ('rc', 'ul', True),
75
                         ], self.locks)
76
77
    def test_02_lock_write(self):
78
        # Test that locking occurs in the correct order
79
        b = self.get_instrumented_branch()
80
81
        self.assertFalse(b.is_locked())
82
        self.assertFalse(b.repository.is_locked())
83
        b.lock_write()
84
        try:
85
            self.assertTrue(b.is_locked())
86
            self.assertTrue(b.repository.is_locked())
87
        finally:
88
            b.unlock()
89
        self.assertFalse(b.is_locked())
90
        self.assertFalse(b.repository.is_locked())
91
92
        self.assertEqual([('b', 'lw', True),
93
                          ('r', 'lw', True),
94
                          ('rc', 'lw', True),
95
                          ('bc', 'lw', True),
96
                          ('b', 'ul', True),
97
                          ('bc', 'ul', True),
98
                          ('r', 'ul', True),
99
                          ('rc', 'ul', True),
100
                         ], self.locks)
101
102
    def test_03_lock_fail_unlock_repo(self):
103
        # Make sure branch.unlock() is called, even if there is a
104
        # failure while unlocking the repository.
105
        b = self.get_instrumented_branch()
106
        b.repository.disable_unlock()
107
108
        self.assertFalse(b.is_locked())
109
        self.assertFalse(b.repository.is_locked())
110
        b.lock_write()
111
        try:
112
            self.assertTrue(b.is_locked())
113
            self.assertTrue(b.repository.is_locked())
114
            self.assertRaises(TestPreventLocking, b.unlock)
115
            if self.combined_control:
116
                self.assertTrue(b.is_locked())
117
            else:
118
                self.assertFalse(b.is_locked())
119
            self.assertTrue(b.repository.is_locked())
120
121
            # We unlock the branch control files, even if 
122
            # we fail to unlock the repository
123
            self.assertEqual([('b', 'lw', True),
124
                              ('r', 'lw', True),
125
                              ('rc', 'lw', True),
126
                              ('bc', 'lw', True),
127
                              ('b', 'ul', True),
128
                              ('bc', 'ul', True),
129
                              ('r', 'ul', False), 
130
                             ], self.locks)
131
132
        finally:
133
            # For cleanup purposes, make sure we are unlocked
134
            b.repository._other.unlock()
135
136
    def test_04_lock_fail_unlock_control(self):
137
        # Make sure repository.unlock() is called, if we fail to unlock self
138
        b = self.get_instrumented_branch()
139
        b.control_files.disable_unlock()
140
141
        self.assertFalse(b.is_locked())
142
        self.assertFalse(b.repository.is_locked())
143
        b.lock_write()
144
        try:
145
            self.assertTrue(b.is_locked())
146
            self.assertTrue(b.repository.is_locked())
147
            self.assertRaises(TestPreventLocking, b.unlock)
148
            self.assertTrue(b.is_locked())
149
            if self.combined_control:
150
                self.assertTrue(b.repository.is_locked())
151
            else:
152
                self.assertFalse(b.repository.is_locked())
153
154
            # We unlock the repository even if 
155
            # we fail to unlock the control files
156
            self.assertEqual([('b', 'lw', True),
157
                              ('r', 'lw', True),
158
                              ('rc', 'lw', True),
159
                              ('bc', 'lw', True),
160
                              ('b', 'ul', True),
161
                              ('bc', 'ul', False),
162
                              ('r', 'ul', True), 
163
                              ('rc', 'ul', True), 
164
                             ], self.locks)
165
166
        finally:
167
            # For cleanup purposes, make sure we are unlocked
168
            b.control_files._other.unlock()
169
170
    def test_05_lock_read_fail_repo(self):
171
        # Test that the branch is not locked if it cannot lock the repository
172
        b = self.get_instrumented_branch()
173
        b.repository.disable_lock_read()
174
175
        self.assertRaises(TestPreventLocking, b.lock_read)
176
        self.assertFalse(b.is_locked())
177
        self.assertFalse(b.repository.is_locked())
178
179
        self.assertEqual([('b', 'lr', True),
180
                          ('r', 'lr', False), 
181
                         ], self.locks)
182
183
    def test_06_lock_write_fail_repo(self):
184
        # Test that the branch is not locked if it cannot lock the repository
185
        b = self.get_instrumented_branch()
186
        b.repository.disable_lock_write()
187
188
        self.assertRaises(TestPreventLocking, b.lock_write)
189
        self.assertFalse(b.is_locked())
190
        self.assertFalse(b.repository.is_locked())
191
192
        self.assertEqual([('b', 'lw', True),
193
                          ('r', 'lw', False), 
194
                         ], self.locks)
195
196
    def test_07_lock_read_fail_control(self):
197
        # Test the repository is unlocked if we can't lock self
198
        b = self.get_instrumented_branch()
199
        b.control_files.disable_lock_read()
200
201
        self.assertRaises(TestPreventLocking, b.lock_read)
202
        self.assertFalse(b.is_locked())
203
        self.assertFalse(b.repository.is_locked())
204
205
        self.assertEqual([('b', 'lr', True),
206
                          ('r', 'lr', True),
207
                          ('rc', 'lr', True),
208
                          ('bc', 'lr', False),
209
                          ('r', 'ul', True),
210
                          ('rc', 'ul', True),
211
                         ], self.locks)
212
213
    def test_08_lock_write_fail_control(self):
214
        # Test the repository is unlocked if we can't lock self
215
        b = self.get_instrumented_branch()
216
        b.control_files.disable_lock_write()
217
218
        self.assertRaises(TestPreventLocking, b.lock_write)
219
        self.assertFalse(b.is_locked())
220
        self.assertFalse(b.repository.is_locked())
221
222
        self.assertEqual([('b', 'lw', True),
223
                          ('r', 'lw', True),
224
                          ('rc', 'lw', True),
225
                          ('bc', 'lw', False),
226
                          ('r', 'ul', True),
227
                          ('rc', 'ul', True),
228
                         ], self.locks)
229
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
230
    def test_lock_write_returns_None_refuses_token(self):
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
231
        branch = self.make_branch('b')
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
232
        token = branch.lock_write()
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
233
        try:
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
234
            if token is not None:
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
235
                # This test does not apply, because this lockable supports
236
                # tokens.
237
                return
238
            self.assertRaises(errors.TokenLockingNotSupported,
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
239
                              branch.lock_write, token='token')
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
240
        finally:
241
            branch.unlock()
242
243
    def test_reentering_lock_write_raises_on_token_mismatch(self):
244
        branch = self.make_branch('b')
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
245
        token = branch.lock_write()
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
246
        try:
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
247
            if token is None:
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
248
                # This test does not apply, because this lockable refuses
249
                # tokens.
250
                return
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
251
            different_branch_token = token + 'xxx'
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
252
            # Re-using the same lockable instance with a different branch token
253
            # will raise TokenMismatch.
254
            self.assertRaises(errors.TokenMismatch,
255
                              branch.lock_write,
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
256
                              token=different_branch_token)
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
257
        finally:
258
            branch.unlock()
259
260
    def test_lock_write_with_nonmatching_token(self):
261
        branch = self.make_branch('b')
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
262
        token = branch.lock_write()
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
263
        try:
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
264
            if token is None:
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
265
                # This test does not apply, because this branch refuses
266
                # tokens.
267
                return
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
268
            different_branch_token = token + 'xxx'
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
269
270
            new_branch = branch.bzrdir.open_branch()
271
            # We only want to test the relocking abilities of branch, so use the
272
            # existing repository object which is already locked.
273
            new_branch.repository = branch.repository
274
            self.assertRaises(errors.TokenMismatch,
275
                              new_branch.lock_write,
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
276
                              token=different_branch_token)
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
277
        finally:
278
            branch.unlock()
279
280
281
    def test_lock_write_with_matching_token(self):
282
        """Test that a branch can be locked with a token, if it is already
283
        locked by that token."""
284
        branch = self.make_branch('b')
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
285
        token = branch.lock_write()
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
286
        try:
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
287
            if token is None:
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
288
                # This test does not apply, because this branch refuses tokens.
289
                return
290
            # The same instance will accept a second lock_write if the specified
291
            # token matches.
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
292
            branch.lock_write(token=token)
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
293
            branch.unlock()
294
            # Calling lock_write on a new instance for the same lockable will
295
            # also succeed.
296
            new_branch = branch.bzrdir.open_branch()
297
            # We only want to test the relocking abilities of branch, so use the
298
            # existing repository object which is already locked.
299
            new_branch.repository = branch.repository
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
300
            new_branch.lock_write(token=token)
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
301
            new_branch.unlock()
302
        finally:
303
            branch.unlock()
304
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
305
    def test_unlock_after_lock_write_with_token(self):
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
306
        # If lock_write did not physically acquire the lock (because it was
307
        # passed some tokens), then unlock should not physically release it.
308
        branch = self.make_branch('b')
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
309
        token = branch.lock_write()
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
310
        try:
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
311
            if token is None:
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
312
                # This test does not apply, because this lockable refuses
313
                # tokens.
314
                return
315
            new_branch = branch.bzrdir.open_branch()
316
            # We only want to test the relocking abilities of branch, so use the
317
            # existing repository object which is already locked.
318
            new_branch.repository = branch.repository
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
319
            new_branch.lock_write(token=token)
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
320
            new_branch.unlock()
321
            self.assertTrue(branch.get_physical_lock_status()) #XXX
322
        finally:
323
            branch.unlock()
324
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
325
    def test_lock_write_with_token_fails_when_unlocked(self):
326
        # First, lock and then unlock to get superficially valid tokens.  This
327
        # mimics a likely programming error, where a caller accidentally tries
328
        # to lock with a token that is no longer valid (because the original
329
        # lock was released).
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
330
        branch = self.make_branch('b')
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
331
        token = branch.lock_write()
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
332
        branch.unlock()
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
333
        if token is None:
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
334
            # This test does not apply, because this lockable refuses
335
            # tokens.
336
            return
337
338
        self.assertRaises(errors.TokenMismatch,
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
339
                          branch.lock_write, token=token)
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
340
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
341
    def test_lock_write_reenter_with_token(self):
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
342
        branch = self.make_branch('b')
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
343
        token = branch.lock_write()
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
344
        try:
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
345
            if token is None:
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
346
                # This test does not apply, because this lockable refuses
347
                # tokens.
348
                return
349
            # Relock with a token.
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
350
            branch.lock_write(token=token)
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
351
            branch.unlock()
352
        finally:
353
            branch.unlock()
354
        # The lock should be unlocked on disk.  Verify that with a new lock
355
        # instance.
356
        new_branch = branch.bzrdir.open_branch()
357
        # Calling lock_write now should work, rather than raise LockContention.
358
        new_branch.lock_write()
359
        new_branch.unlock()
360
361
    def test_leave_lock_in_place(self):
362
        branch = self.make_branch('b')
363
        # Lock the branch, then use leave_lock_in_place so that when we
364
        # unlock the branch the lock is still held on disk.
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
365
        token = branch.lock_write()
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
366
        try:
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
367
            if token is None:
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
368
                # This test does not apply, because this repository refuses lock
369
                # tokens.
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
370
                self.assertRaises(NotImplementedError,
371
                                  branch.leave_lock_in_place)
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
372
                return
373
            branch.leave_lock_in_place()
374
        finally:
375
            branch.unlock()
376
        # We should be unable to relock the repo.
377
        self.assertRaises(errors.LockContention, branch.lock_write)
378
379
    def test_dont_leave_lock_in_place(self):
380
        branch = self.make_branch('b')
381
        # Create a lock on disk.
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
382
        token = branch.lock_write()
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
383
        try:
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
384
            if token is None:
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
385
                # This test does not apply, because this branch refuses lock
386
                # tokens.
387
                self.assertRaises(NotImplementedError,
388
                                  branch.dont_leave_lock_in_place)
389
                return
390
            try:
391
                branch.leave_lock_in_place()
392
            except NotImplementedError:
393
                # This branch doesn't support this API.
394
                return
395
            branch.repository.leave_lock_in_place()
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
396
            repo_token = branch.repository.lock_write()
397
            branch.repository.unlock()
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
398
        finally:
399
            branch.unlock()
400
        # Reacquire the lock (with a different branch object) by using the
401
        # tokens.
402
        new_branch = branch.bzrdir.open_branch()
2279.7.10 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch)
403
        # We have to explicitly lock the repository first.
404
        new_branch.repository.lock_write(token=repo_token)
405
        new_branch.lock_write(token=token)
406
        # Now we don't need our own repository lock anymore (the branch is
407
        # holding it for us).
408
        new_branch.repository.unlock()
2279.7.7 by Andrew Bennetts
LockDir, Repository and Branch lock token changes from the hpss branch.
409
        # Call dont_leave_lock_in_place, so that the lock will be released by
410
        # this instance, even though the lock wasn't originally acquired by it.
411
        new_branch.dont_leave_lock_in_place()
412
        new_branch.repository.dont_leave_lock_in_place()
413
        new_branch.unlock()
414
        # Now the branch (and repository) is unlocked.  Test this by locking it
415
        # without tokens.
416
        branch.lock_write()
417
        branch.unlock()
418
2381.1.1 by Robert Collins
Split out hpss test fixes which dont depend on new or altered API's.
419
    def test_lock_read_then_unlock(self):
420
        # Calling lock_read then unlocking should work without errors.
421
        branch = self.make_branch('b')
422
        branch.lock_read()
423
        branch.unlock()
424
425
    def test_lock_write_locks_repo_too(self):
426
        if isinstance(self.branch_format, BzrBranchFormat4):
427
            # Branch format 4 is combined with the repository, so this test
428
            # doesn't apply.
429
            return
430
        branch = self.make_branch('b')
431
        branch = branch.bzrdir.open_branch()
432
        branch.lock_write()
433
        try:
434
            # Now the branch.repository is locked, so we can't lock it with a new
435
            # repository without a token.
436
            new_repo = branch.bzrdir.open_repository()
437
            self.assertRaises(errors.LockContention, new_repo.lock_write)
438
            # We can call lock_write on the original repository object though,
439
            # because it is already locked.
440
            branch.repository.lock_write()
441
            branch.repository.unlock()
442
        finally:
443
            branch.unlock()