231
231
('rc', 'ul', True),
234
def test_lock_write_returns_None_refuses_tokens(self):
234
def test_lock_write_returns_None_refuses_token(self):
235
235
branch = self.make_branch('b')
236
tokens = branch.lock_write()
236
token = branch.lock_write()
238
if tokens is not None:
238
if token is not None:
239
239
# This test does not apply, because this lockable supports
242
242
self.assertRaises(errors.TokenLockingNotSupported,
243
branch.lock_write, tokens=('token','token'))
243
branch.lock_write, token='token')
247
247
def test_reentering_lock_write_raises_on_token_mismatch(self):
248
248
branch = self.make_branch('b')
249
tokens = branch.lock_write()
249
token = branch.lock_write()
252
252
# This test does not apply, because this lockable refuses
255
branch_token, repo_token = tokens
256
different_branch_token = branch_token + 'xxx'
257
different_repo_token = repo_token + 'xxx'
255
different_branch_token = token + 'xxx'
258
256
# Re-using the same lockable instance with a different branch token
259
257
# will raise TokenMismatch.
260
258
self.assertRaises(errors.TokenMismatch,
261
259
branch.lock_write,
262
tokens=(different_branch_token, repo_token))
263
# Similarly for a different repository token.
264
self.assertRaises(errors.TokenMismatch,
266
tokens=(branch_token, different_repo_token))
260
token=different_branch_token)
270
264
def test_lock_write_with_nonmatching_token(self):
271
265
branch = self.make_branch('b')
272
tokens = branch.lock_write()
266
token = branch.lock_write()
275
269
# This test does not apply, because this branch refuses
278
branch_token, repo_token = tokens
279
different_branch_token = branch_token + 'xxx'
280
different_repo_token = repo_token + 'xxx'
272
different_branch_token = token + 'xxx'
282
274
new_branch = branch.bzrdir.open_branch()
283
275
# We only want to test the relocking abilities of branch, so use the
285
277
new_branch.repository = branch.repository
286
278
self.assertRaises(errors.TokenMismatch,
287
279
new_branch.lock_write,
288
tokens=(different_branch_token, repo_token))
289
self.assertRaises(errors.TokenMismatch,
290
new_branch.lock_write,
291
tokens=(branch_token, different_repo_token))
280
token=different_branch_token)
297
286
"""Test that a branch can be locked with a token, if it is already
298
287
locked by that token."""
299
288
branch = self.make_branch('b')
300
tokens = branch.lock_write()
289
token = branch.lock_write()
303
292
# This test does not apply, because this branch refuses tokens.
305
294
# The same instance will accept a second lock_write if the specified
307
branch.lock_write(tokens=tokens)
296
branch.lock_write(token=token)
309
298
# Calling lock_write on a new instance for the same lockable will
312
301
# We only want to test the relocking abilities of branch, so use the
313
302
# existing repository object which is already locked.
314
303
new_branch.repository = branch.repository
315
new_branch.lock_write(tokens=tokens)
304
new_branch.lock_write(token=token)
316
305
new_branch.unlock()
320
def test_unlock_after_lock_write_with_tokens(self):
309
def test_unlock_after_lock_write_with_token(self):
321
310
# If lock_write did not physically acquire the lock (because it was
322
311
# passed some tokens), then unlock should not physically release it.
323
312
branch = self.make_branch('b')
324
tokens = branch.lock_write()
313
token = branch.lock_write()
327
316
# This test does not apply, because this lockable refuses
331
320
# We only want to test the relocking abilities of branch, so use the
332
321
# existing repository object which is already locked.
333
322
new_branch.repository = branch.repository
334
new_branch.lock_write(tokens=tokens)
323
new_branch.lock_write(token=token)
335
324
new_branch.unlock()
336
325
self.assertTrue(branch.get_physical_lock_status()) #XXX
340
def test_lock_write_with_tokens_fails_when_unlocked(self):
341
# Lock and unlock to get superficially valid tokens. This mimics a
342
# likely programming error, where a caller accidentally tries to lock
343
# with tokens that are no longer valid (because the original lock was
329
def test_lock_write_with_token_fails_when_unlocked(self):
330
# First, lock and then unlock to get superficially valid tokens. This
331
# mimics a likely programming error, where a caller accidentally tries
332
# to lock with a token that is no longer valid (because the original
333
# lock was released).
345
334
branch = self.make_branch('b')
346
tokens = branch.lock_write()
335
token = branch.lock_write()
349
338
# This test does not apply, because this lockable refuses
353
342
self.assertRaises(errors.TokenMismatch,
354
branch.lock_write, tokens=tokens)
343
branch.lock_write, token=token)
356
def test_lock_write_reenter_with_tokens(self):
345
def test_lock_write_reenter_with_token(self):
357
346
branch = self.make_branch('b')
358
tokens = branch.lock_write()
347
token = branch.lock_write()
361
350
# This test does not apply, because this lockable refuses
364
353
# Relock with a token.
365
branch.lock_write(tokens=tokens)
354
branch.lock_write(token=token)
377
366
branch = self.make_branch('b')
378
367
# Lock the branch, then use leave_lock_in_place so that when we
379
368
# unlock the branch the lock is still held on disk.
380
tokens = branch.lock_write()
369
token = branch.lock_write()
383
372
# This test does not apply, because this repository refuses lock
385
self.assertRaises(NotImplementedError, branch.leave_lock_in_place)
374
self.assertRaises(NotImplementedError,
375
branch.leave_lock_in_place)
387
377
branch.leave_lock_in_place()
393
383
def test_dont_leave_lock_in_place(self):
394
384
branch = self.make_branch('b')
395
385
# Create a lock on disk.
396
tokens = branch.lock_write()
386
token = branch.lock_write()
399
389
# This test does not apply, because this branch refuses lock
401
391
self.assertRaises(NotImplementedError,
407
397
# This branch doesn't support this API.
409
399
branch.repository.leave_lock_in_place()
400
repo_token = branch.repository.lock_write()
401
branch.repository.unlock()
412
404
# Reacquire the lock (with a different branch object) by using the
414
406
new_branch = branch.bzrdir.open_branch()
415
new_branch.lock_write(tokens=tokens)
407
# We have to explicitly lock the repository first.
408
new_branch.repository.lock_write(token=repo_token)
409
new_branch.lock_write(token=token)
410
# Now we don't need our own repository lock anymore (the branch is
411
# holding it for us).
412
new_branch.repository.unlock()
416
413
# Call dont_leave_lock_in_place, so that the lock will be released by
417
414
# this instance, even though the lock wasn't originally acquired by it.
418
415
new_branch.dont_leave_lock_in_place()