~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_branch/test_locking.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
    lock_helpers,
26
26
    per_branch,
27
27
    )
 
28
from bzrlib.tests.matchers import *
28
29
 
29
30
 
30
31
class TestBranchLocking(per_branch.TestCaseWithBranch):
291
292
 
292
293
    def test_lock_write_returns_None_refuses_token(self):
293
294
        branch = self.make_branch('b')
294
 
        token = branch.lock_write()
 
295
        token = branch.lock_write().branch_token
295
296
        try:
296
297
            if token is not None:
297
298
                # This test does not apply, because this lockable supports
304
305
 
305
306
    def test_reentering_lock_write_raises_on_token_mismatch(self):
306
307
        branch = self.make_branch('b')
307
 
        token = branch.lock_write()
 
308
        token = branch.lock_write().branch_token
308
309
        try:
309
310
            if token is None:
310
311
                # This test does not apply, because this lockable refuses
321
322
 
322
323
    def test_lock_write_with_nonmatching_token(self):
323
324
        branch = self.make_branch('b')
324
 
        token = branch.lock_write()
 
325
        token = branch.lock_write().branch_token
325
326
        try:
326
327
            if token is None:
327
328
                # This test does not apply, because this branch refuses
344
345
        """Test that a branch can be locked with a token, if it is already
345
346
        locked by that token."""
346
347
        branch = self.make_branch('b')
347
 
        token = branch.lock_write()
 
348
        token = branch.lock_write().branch_token
348
349
        try:
349
350
            if token is None:
350
351
                # This test does not apply, because this branch refuses tokens.
368
369
        # If lock_write did not physically acquire the lock (because it was
369
370
        # passed some tokens), then unlock should not physically release it.
370
371
        branch = self.make_branch('b')
371
 
        token = branch.lock_write()
 
372
        token = branch.lock_write().branch_token
372
373
        try:
373
374
            if token is None:
374
375
                # This test does not apply, because this lockable refuses
390
391
        # to lock with a token that is no longer valid (because the original
391
392
        # lock was released).
392
393
        branch = self.make_branch('b')
393
 
        token = branch.lock_write()
 
394
        token = branch.lock_write().branch_token
394
395
        branch.unlock()
395
396
        if token is None:
396
397
            # This test does not apply, because this lockable refuses
397
398
            # tokens.
398
399
            return
399
 
 
400
 
        self.assertRaises(errors.TokenMismatch,
401
 
                          branch.lock_write, token=token)
 
400
        self.assertRaises(errors.TokenMismatch, branch.lock_write, token=token)
402
401
 
403
402
    def test_lock_write_reenter_with_token(self):
404
403
        branch = self.make_branch('b')
405
 
        token = branch.lock_write()
 
404
        token = branch.lock_write().branch_token
406
405
        try:
407
406
            if token is None:
408
407
                # This test does not apply, because this lockable refuses
420
419
        new_branch.lock_write()
421
420
        new_branch.unlock()
422
421
 
 
422
    def test_lock_write_returns_unlockable(self):
 
423
        branch = self.make_branch('b')
 
424
        self.assertThat(branch.lock_write, ReturnsUnlockable(branch))
 
425
 
423
426
    def test_leave_lock_in_place(self):
424
427
        branch = self.make_branch('b')
425
428
        # Lock the branch, then use leave_lock_in_place so that when we
426
429
        # unlock the branch the lock is still held on disk.
427
 
        token = branch.lock_write()
 
430
        token = branch.lock_write().branch_token
428
431
        try:
429
432
            if token is None:
430
433
                # This test does not apply, because this repository refuses lock
445
448
    def test_dont_leave_lock_in_place(self):
446
449
        branch = self.make_branch('b')
447
450
        # Create a lock on disk.
448
 
        token = branch.lock_write()
 
451
        token = branch.lock_write().branch_token
449
452
        try:
450
453
            if token is None:
451
454
                # This test does not apply, because this branch refuses lock
497
500
        branch.lock_read()
498
501
        branch.unlock()
499
502
 
 
503
    def test_lock_read_returns_unlockable(self):
 
504
        branch = self.make_branch('b')
 
505
        self.assertThat(branch.lock_read, ReturnsUnlockable(branch))
 
506
 
500
507
    def test_lock_write_locks_repo_too(self):
501
508
        if isinstance(self.branch_format, _mod_branch.BzrBranchFormat4):
502
509
            # Branch format 4 is combined with the repository, so this test
523
530
        finally:
524
531
            branch.unlock()
525
532
 
 
533
    def test_lock_write_returns_unlockable(self):
 
534
        branch = self.make_branch('b')
 
535
        self.assertThat(branch.lock_write, ReturnsUnlockable(branch))
 
536
 
526
537
    def test_lock_and_unlock_leaves_repo_unlocked(self):
527
538
        branch = self.make_branch('b')
528
539
        branch.lock_write()