227
227
('rc', 'ul', True),
230
def test_lock_write_returns_None_refuses_tokens(self):
231
branch = self.make_branch('b')
232
tokens = branch.lock_write()
234
if tokens is not None:
235
# This test does not apply, because this lockable supports
238
self.assertRaises(errors.TokenLockingNotSupported,
239
branch.lock_write, tokens=('token','token'))
243
def test_reentering_lock_write_raises_on_token_mismatch(self):
244
branch = self.make_branch('b')
245
tokens = branch.lock_write()
248
# This test does not apply, because this lockable refuses
251
branch_token, repo_token = tokens
252
different_branch_token = branch_token + 'xxx'
253
different_repo_token = repo_token + 'xxx'
254
# Re-using the same lockable instance with a different branch token
255
# will raise TokenMismatch.
256
self.assertRaises(errors.TokenMismatch,
258
tokens=(different_branch_token, repo_token))
259
# Similarly for a different repository token.
260
self.assertRaises(errors.TokenMismatch,
262
tokens=(branch_token, different_repo_token))
266
def test_lock_write_with_nonmatching_token(self):
267
branch = self.make_branch('b')
268
tokens = branch.lock_write()
271
# This test does not apply, because this branch refuses
274
branch_token, repo_token = tokens
275
different_branch_token = branch_token + 'xxx'
276
different_repo_token = repo_token + 'xxx'
278
new_branch = branch.bzrdir.open_branch()
279
# We only want to test the relocking abilities of branch, so use the
280
# existing repository object which is already locked.
281
new_branch.repository = branch.repository
282
self.assertRaises(errors.TokenMismatch,
283
new_branch.lock_write,
284
tokens=(different_branch_token, repo_token))
285
self.assertRaises(errors.TokenMismatch,
286
new_branch.lock_write,
287
tokens=(branch_token, different_repo_token))
292
def test_lock_write_with_matching_token(self):
293
"""Test that a branch can be locked with a token, if it is already
294
locked by that token."""
295
branch = self.make_branch('b')
296
tokens = branch.lock_write()
299
# This test does not apply, because this branch refuses tokens.
301
# The same instance will accept a second lock_write if the specified
303
branch.lock_write(tokens=tokens)
305
# Calling lock_write on a new instance for the same lockable will
307
new_branch = branch.bzrdir.open_branch()
308
# We only want to test the relocking abilities of branch, so use the
309
# existing repository object which is already locked.
310
new_branch.repository = branch.repository
311
new_branch.lock_write(tokens=tokens)
316
def test_unlock_after_lock_write_with_tokens(self):
317
# If lock_write did not physically acquire the lock (because it was
318
# passed some tokens), then unlock should not physically release it.
319
branch = self.make_branch('b')
320
tokens = branch.lock_write()
323
# This test does not apply, because this lockable refuses
326
new_branch = branch.bzrdir.open_branch()
327
# We only want to test the relocking abilities of branch, so use the
328
# existing repository object which is already locked.
329
new_branch.repository = branch.repository
330
new_branch.lock_write(tokens=tokens)
332
self.assertTrue(branch.get_physical_lock_status()) #XXX
336
def test_lock_write_with_tokens_fails_when_unlocked(self):
337
# Lock and unlock to get superficially valid tokens. This mimics a
338
# likely programming error, where a caller accidentally tries to lock
339
# with tokens that are no longer valid (because the original lock was
341
branch = self.make_branch('b')
342
tokens = branch.lock_write()
345
# This test does not apply, because this lockable refuses
349
self.assertRaises(errors.TokenMismatch,
350
branch.lock_write, tokens=tokens)
352
def test_lock_write_reenter_with_tokens(self):
353
branch = self.make_branch('b')
354
tokens = branch.lock_write()
357
# This test does not apply, because this lockable refuses
360
# Relock with a token.
361
branch.lock_write(tokens=tokens)
365
# The lock should be unlocked on disk. Verify that with a new lock
367
new_branch = branch.bzrdir.open_branch()
368
# Calling lock_write now should work, rather than raise LockContention.
369
new_branch.lock_write()
372
def test_leave_lock_in_place(self):
373
branch = self.make_branch('b')
374
# Lock the branch, then use leave_lock_in_place so that when we
375
# unlock the branch the lock is still held on disk.
376
tokens = branch.lock_write()
379
# This test does not apply, because this repository refuses lock
381
self.assertRaises(NotImplementedError, branch.leave_lock_in_place)
383
branch.leave_lock_in_place()
386
# We should be unable to relock the repo.
387
self.assertRaises(errors.LockContention, branch.lock_write)
389
def test_dont_leave_lock_in_place(self):
390
branch = self.make_branch('b')
391
# Create a lock on disk.
392
tokens = branch.lock_write()
395
# This test does not apply, because this branch refuses lock
397
self.assertRaises(NotImplementedError,
398
branch.dont_leave_lock_in_place)
401
branch.leave_lock_in_place()
402
except NotImplementedError:
403
# This branch doesn't support this API.
405
branch.repository.leave_lock_in_place()
408
# Reacquire the lock (with a different branch object) by using the
410
new_branch = branch.bzrdir.open_branch()
411
new_branch.lock_write(tokens=tokens)
412
# Call dont_leave_lock_in_place, so that the lock will be released by
413
# this instance, even though the lock wasn't originally acquired by it.
414
new_branch.dont_leave_lock_in_place()
415
new_branch.repository.dont_leave_lock_in_place()
417
# Now the branch (and repository) is unlocked. Test this by locking it
230
422
def test_lock_read_then_unlock(self):
231
423
# Calling lock_read then unlocking should work without errors.
232
424
branch = self.make_branch('b')