227
227
('rc', 'ul', True),
230
def test_lock_write_returns_None_refuses_token(self):
231
branch = self.make_branch('b')
232
token = branch.lock_write()
234
if token is not None:
235
# This test does not apply, because this lockable supports
238
self.assertRaises(errors.TokenLockingNotSupported,
239
branch.lock_write, token='token')
243
def test_reentering_lock_write_raises_on_token_mismatch(self):
244
branch = self.make_branch('b')
245
token = branch.lock_write()
248
# This test does not apply, because this lockable refuses
251
different_branch_token = token + 'xxx'
252
# Re-using the same lockable instance with a different branch token
253
# will raise TokenMismatch.
254
self.assertRaises(errors.TokenMismatch,
256
token=different_branch_token)
260
def test_lock_write_with_nonmatching_token(self):
261
branch = self.make_branch('b')
262
token = branch.lock_write()
265
# This test does not apply, because this branch refuses
268
different_branch_token = token + 'xxx'
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,
276
token=different_branch_token)
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')
285
token = branch.lock_write()
288
# This test does not apply, because this branch refuses tokens.
290
# The same instance will accept a second lock_write if the specified
292
branch.lock_write(token=token)
294
# Calling lock_write on a new instance for the same lockable will
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
300
new_branch.lock_write(token=token)
305
def test_unlock_after_lock_write_with_token(self):
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')
309
token = branch.lock_write()
312
# This test does not apply, because this lockable refuses
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
319
new_branch.lock_write(token=token)
321
self.assertTrue(branch.get_physical_lock_status()) #XXX
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).
330
branch = self.make_branch('b')
331
token = branch.lock_write()
334
# This test does not apply, because this lockable refuses
338
self.assertRaises(errors.TokenMismatch,
339
branch.lock_write, token=token)
341
def test_lock_write_reenter_with_token(self):
342
branch = self.make_branch('b')
343
token = branch.lock_write()
346
# This test does not apply, because this lockable refuses
349
# Relock with a token.
350
branch.lock_write(token=token)
354
# The lock should be unlocked on disk. Verify that with a new lock
356
new_branch = branch.bzrdir.open_branch()
357
# Calling lock_write now should work, rather than raise LockContention.
358
new_branch.lock_write()
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.
365
token = branch.lock_write()
368
# This test does not apply, because this repository refuses lock
370
self.assertRaises(NotImplementedError,
371
branch.leave_lock_in_place)
373
branch.leave_lock_in_place()
376
# We should be unable to relock the repo.
377
self.assertRaises(errors.LockContention, branch.lock_write)
379
def test_dont_leave_lock_in_place(self):
380
branch = self.make_branch('b')
381
# Create a lock on disk.
382
token = branch.lock_write()
385
# This test does not apply, because this branch refuses lock
387
self.assertRaises(NotImplementedError,
388
branch.dont_leave_lock_in_place)
391
branch.leave_lock_in_place()
392
except NotImplementedError:
393
# This branch doesn't support this API.
395
branch.repository.leave_lock_in_place()
396
repo_token = branch.repository.lock_write()
397
branch.repository.unlock()
400
# Reacquire the lock (with a different branch object) by using the
402
new_branch = branch.bzrdir.open_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()
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()
414
# Now the branch (and repository) is unlocked. Test this by locking it
230
419
def test_lock_read_then_unlock(self):
231
420
# Calling lock_read then unlocking should work without errors.
232
421
branch = self.make_branch('b')