220
233
('rc', 'ul', True),
236
def test_lock_write_returns_None_refuses_token(self):
237
branch = self.make_branch('b')
238
token = branch.lock_write()
240
if token is not None:
241
# This test does not apply, because this lockable supports
244
self.assertRaises(errors.TokenLockingNotSupported,
245
branch.lock_write, token='token')
249
def test_reentering_lock_write_raises_on_token_mismatch(self):
250
branch = self.make_branch('b')
251
token = branch.lock_write()
254
# This test does not apply, because this lockable refuses
257
different_branch_token = token + 'xxx'
258
# Re-using the same lockable instance with a different branch token
259
# will raise TokenMismatch.
260
self.assertRaises(errors.TokenMismatch,
262
token=different_branch_token)
266
def test_lock_write_with_nonmatching_token(self):
267
branch = self.make_branch('b')
268
token = branch.lock_write()
271
# This test does not apply, because this branch refuses
274
different_branch_token = token + 'xxx'
276
new_branch = branch.bzrdir.open_branch()
277
# We only want to test the relocking abilities of branch, so use the
278
# existing repository object which is already locked.
279
new_branch.repository = branch.repository
280
self.assertRaises(errors.TokenMismatch,
281
new_branch.lock_write,
282
token=different_branch_token)
287
def test_lock_write_with_matching_token(self):
288
"""Test that a branch can be locked with a token, if it is already
289
locked by that token."""
290
branch = self.make_branch('b')
291
token = branch.lock_write()
294
# This test does not apply, because this branch refuses tokens.
296
# The same instance will accept a second lock_write if the specified
298
branch.lock_write(token=token)
300
# Calling lock_write on a new instance for the same lockable will
302
new_branch = branch.bzrdir.open_branch()
303
# We only want to test the relocking abilities of branch, so use the
304
# existing repository object which is already locked.
305
new_branch.repository = branch.repository
306
new_branch.lock_write(token=token)
311
def test_unlock_after_lock_write_with_token(self):
312
# If lock_write did not physically acquire the lock (because it was
313
# passed some tokens), then unlock should not physically release it.
314
branch = self.make_branch('b')
315
token = branch.lock_write()
318
# This test does not apply, because this lockable refuses
321
new_branch = branch.bzrdir.open_branch()
322
# We only want to test the relocking abilities of branch, so use the
323
# existing repository object which is already locked.
324
new_branch.repository = branch.repository
325
new_branch.lock_write(token=token)
327
self.assertTrue(branch.get_physical_lock_status()) #XXX
331
def test_lock_write_with_token_fails_when_unlocked(self):
332
# First, lock and then unlock to get superficially valid tokens. This
333
# mimics a likely programming error, where a caller accidentally tries
334
# to lock with a token that is no longer valid (because the original
335
# lock was released).
336
branch = self.make_branch('b')
337
token = branch.lock_write()
340
# This test does not apply, because this lockable refuses
344
self.assertRaises(errors.TokenMismatch,
345
branch.lock_write, token=token)
347
def test_lock_write_reenter_with_token(self):
348
branch = self.make_branch('b')
349
token = branch.lock_write()
352
# This test does not apply, because this lockable refuses
355
# Relock with a token.
356
branch.lock_write(token=token)
360
# The lock should be unlocked on disk. Verify that with a new lock
362
new_branch = branch.bzrdir.open_branch()
363
# Calling lock_write now should work, rather than raise LockContention.
364
new_branch.lock_write()
367
def test_leave_lock_in_place(self):
368
branch = self.make_branch('b')
369
# Lock the branch, then use leave_lock_in_place so that when we
370
# unlock the branch the lock is still held on disk.
371
token = branch.lock_write()
374
# This test does not apply, because this repository refuses lock
376
self.assertRaises(NotImplementedError,
377
branch.leave_lock_in_place)
379
branch.leave_lock_in_place()
382
# We should be unable to relock the repo.
383
self.assertRaises(errors.LockContention, branch.lock_write)
385
def test_dont_leave_lock_in_place(self):
386
branch = self.make_branch('b')
387
# Create a lock on disk.
388
token = branch.lock_write()
391
# This test does not apply, because this branch refuses lock
393
self.assertRaises(NotImplementedError,
394
branch.dont_leave_lock_in_place)
397
branch.leave_lock_in_place()
398
except NotImplementedError:
399
# This branch doesn't support this API.
401
branch.repository.leave_lock_in_place()
402
repo_token = branch.repository.lock_write()
403
branch.repository.unlock()
406
# Reacquire the lock (with a different branch object) by using the
408
new_branch = branch.bzrdir.open_branch()
409
# We have to explicitly lock the repository first.
410
new_branch.repository.lock_write(token=repo_token)
411
new_branch.lock_write(token=token)
412
# Now we don't need our own repository lock anymore (the branch is
413
# holding it for us).
414
new_branch.repository.unlock()
415
# Call dont_leave_lock_in_place, so that the lock will be released by
416
# this instance, even though the lock wasn't originally acquired by it.
417
new_branch.dont_leave_lock_in_place()
418
new_branch.repository.dont_leave_lock_in_place()
420
# Now the branch (and repository) is unlocked. Test this by locking it
425
def test_lock_read_then_unlock(self):
426
# Calling lock_read then unlocking should work without errors.
427
branch = self.make_branch('b')
431
def test_lock_write_locks_repo_too(self):
432
if isinstance(self.branch_format, BzrBranchFormat4):
433
# Branch format 4 is combined with the repository, so this test
436
branch = self.make_branch('b')
437
branch = branch.bzrdir.open_branch()
440
# Now the branch.repository is locked, so we can't lock it with a new
441
# repository without a token.
442
new_repo = branch.bzrdir.open_repository()
443
self.assertRaises(errors.LockContention, new_repo.lock_write)
444
# We can call lock_write on the original repository object though,
445
# because it is already locked.
446
branch.repository.lock_write()
447
branch.repository.unlock()