Rev 2289: Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens). (copied from hpss branch) in sftp://bazaar.launchpad.net/%7Ebzr/bzr/bzr.dev.hpss.api.changes/

Andrew Bennetts andrew.bennetts at canonical.com
Thu Apr 12 08:44:29 BST 2007


At sftp://bazaar.launchpad.net/%7Ebzr/bzr/bzr.dev.hpss.api.changes/

------------------------------------------------------------
revno: 2289
revision-id: andrew.bennetts at canonical.com-20070412074331-otphanb3q0tx6rfz
parent: andrew.bennetts at canonical.com-20070412073725-pzd8641vf9oh6v5g
committer: Andrew Bennetts <andrew.bennetts at canonical.com>
branch nick: bzr.dev.hpss.api.changes
timestamp: Thu 2007-04-12 17:43:31 +1000
message:
  Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).  (copied from hpss branch)
modified:
  bzrlib/branch.py               branch.py-20050309040759-e4baf4e0d046576e
  bzrlib/tests/branch_implementations/test_locking.py test_locking.py-20060707151933-tav3o2hpibwi53u4-4
=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2007-04-11 13:35:32 +0000
+++ b/bzrlib/branch.py	2007-04-12 07:43:31 +0000
@@ -1239,26 +1239,14 @@
     def is_locked(self):
         return self.control_files.is_locked()
 
-    def lock_write(self, tokens=None):
-        if tokens is not None:
-            branch_token, repo_token = tokens
-        else:
-            branch_token = repo_token = None
-        repo_token = self.repository.lock_write(token=repo_token)
+    def lock_write(self, token=None):
+        repo_token = self.repository.lock_write()
         try:
-            branch_token = self.control_files.lock_write(token=branch_token)
+            token = self.control_files.lock_write(token=token)
         except:
             self.repository.unlock()
             raise
-        else:
-            tokens = (branch_token, repo_token)
-            assert tokens == (None, None) or None not in tokens, (
-                'Both branch and repository locks must return tokens, or else '
-                'neither must return tokens.  Got %r.' % (tokens,))
-            if tokens == (None, None):
-                return None
-            else:
-                return tokens
+        return token
 
     def lock_read(self):
         self.repository.lock_read()

=== modified file 'bzrlib/tests/branch_implementations/test_locking.py'
--- a/bzrlib/tests/branch_implementations/test_locking.py	2007-04-11 13:35:32 +0000
+++ b/bzrlib/tests/branch_implementations/test_locking.py	2007-04-12 07:43:31 +0000
@@ -227,53 +227,45 @@
                           ('rc', 'ul', True),
                          ], self.locks)
 
-    def test_lock_write_returns_None_refuses_tokens(self):
+    def test_lock_write_returns_None_refuses_token(self):
         branch = self.make_branch('b')
-        tokens = branch.lock_write()
+        token = branch.lock_write()
         try:
-            if tokens is not None:
+            if token is not None:
                 # This test does not apply, because this lockable supports
                 # tokens.
                 return
             self.assertRaises(errors.TokenLockingNotSupported,
-                              branch.lock_write, tokens=('token','token'))
+                              branch.lock_write, token='token')
         finally:
             branch.unlock()
 
     def test_reentering_lock_write_raises_on_token_mismatch(self):
         branch = self.make_branch('b')
-        tokens = branch.lock_write()
+        token = branch.lock_write()
         try:
-            if tokens is None:
+            if token is None:
                 # This test does not apply, because this lockable refuses
                 # tokens.
                 return
-            branch_token, repo_token = tokens
-            different_branch_token = branch_token + 'xxx'
-            different_repo_token = repo_token + 'xxx'
+            different_branch_token = token + 'xxx'
             # Re-using the same lockable instance with a different branch token
             # will raise TokenMismatch.
             self.assertRaises(errors.TokenMismatch,
                               branch.lock_write,
-                              tokens=(different_branch_token, repo_token))
-            # Similarly for a different repository token.
-            self.assertRaises(errors.TokenMismatch,
-                              branch.lock_write,
-                              tokens=(branch_token, different_repo_token))
+                              token=different_branch_token)
         finally:
             branch.unlock()
 
     def test_lock_write_with_nonmatching_token(self):
         branch = self.make_branch('b')
-        tokens = branch.lock_write()
+        token = branch.lock_write()
         try:
-            if tokens is None:
+            if token is None:
                 # This test does not apply, because this branch refuses
                 # tokens.
                 return
-            branch_token, repo_token = tokens
-            different_branch_token = branch_token + 'xxx'
-            different_repo_token = repo_token + 'xxx'
+            different_branch_token = token + 'xxx'
 
             new_branch = branch.bzrdir.open_branch()
             # We only want to test the relocking abilities of branch, so use the
@@ -281,10 +273,7 @@
             new_branch.repository = branch.repository
             self.assertRaises(errors.TokenMismatch,
                               new_branch.lock_write,
-                              tokens=(different_branch_token, repo_token))
-            self.assertRaises(errors.TokenMismatch,
-                              new_branch.lock_write,
-                              tokens=(branch_token, different_repo_token))
+                              token=different_branch_token)
         finally:
             branch.unlock()
 
@@ -293,14 +282,14 @@
         """Test that a branch can be locked with a token, if it is already
         locked by that token."""
         branch = self.make_branch('b')
-        tokens = branch.lock_write()
+        token = branch.lock_write()
         try:
-            if tokens is None:
+            if token is None:
                 # This test does not apply, because this branch refuses tokens.
                 return
             # The same instance will accept a second lock_write if the specified
             # token matches.
-            branch.lock_write(tokens=tokens)
+            branch.lock_write(token=token)
             branch.unlock()
             # Calling lock_write on a new instance for the same lockable will
             # also succeed.
@@ -308,18 +297,18 @@
             # We only want to test the relocking abilities of branch, so use the
             # existing repository object which is already locked.
             new_branch.repository = branch.repository
-            new_branch.lock_write(tokens=tokens)
+            new_branch.lock_write(token=token)
             new_branch.unlock()
         finally:
             branch.unlock()
 
-    def test_unlock_after_lock_write_with_tokens(self):
+    def test_unlock_after_lock_write_with_token(self):
         # If lock_write did not physically acquire the lock (because it was
         # passed some tokens), then unlock should not physically release it.
         branch = self.make_branch('b')
-        tokens = branch.lock_write()
+        token = branch.lock_write()
         try:
-            if tokens is None:
+            if token is None:
                 # This test does not apply, because this lockable refuses
                 # tokens.
                 return
@@ -327,38 +316,38 @@
             # We only want to test the relocking abilities of branch, so use the
             # existing repository object which is already locked.
             new_branch.repository = branch.repository
-            new_branch.lock_write(tokens=tokens)
+            new_branch.lock_write(token=token)
             new_branch.unlock()
             self.assertTrue(branch.get_physical_lock_status()) #XXX
         finally:
             branch.unlock()
 
-    def test_lock_write_with_tokens_fails_when_unlocked(self):
-        # Lock and unlock to get superficially valid tokens.  This mimics a
-        # likely programming error, where a caller accidentally tries to lock
-        # with tokens that are no longer valid (because the original lock was
-        # released).
+    def test_lock_write_with_token_fails_when_unlocked(self):
+        # First, lock and then unlock to get superficially valid tokens.  This
+        # mimics a likely programming error, where a caller accidentally tries
+        # to lock with a token that is no longer valid (because the original
+        # lock was released).
         branch = self.make_branch('b')
-        tokens = branch.lock_write()
+        token = branch.lock_write()
         branch.unlock()
-        if tokens is None:
+        if token is None:
             # This test does not apply, because this lockable refuses
             # tokens.
             return
 
         self.assertRaises(errors.TokenMismatch,
-                          branch.lock_write, tokens=tokens)
+                          branch.lock_write, token=token)
 
-    def test_lock_write_reenter_with_tokens(self):
+    def test_lock_write_reenter_with_token(self):
         branch = self.make_branch('b')
-        tokens = branch.lock_write()
+        token = branch.lock_write()
         try:
-            if tokens is None:
+            if token is None:
                 # This test does not apply, because this lockable refuses
                 # tokens.
                 return
             # Relock with a token.
-            branch.lock_write(tokens=tokens)
+            branch.lock_write(token=token)
             branch.unlock()
         finally:
             branch.unlock()
@@ -373,12 +362,13 @@
         branch = self.make_branch('b')
         # Lock the branch, then use leave_lock_in_place so that when we
         # unlock the branch the lock is still held on disk.
-        tokens = branch.lock_write()
+        token = branch.lock_write()
         try:
-            if tokens is None:
+            if token is None:
                 # This test does not apply, because this repository refuses lock
                 # tokens.
-                self.assertRaises(NotImplementedError, branch.leave_lock_in_place)
+                self.assertRaises(NotImplementedError,
+                                  branch.leave_lock_in_place)
                 return
             branch.leave_lock_in_place()
         finally:
@@ -389,9 +379,9 @@
     def test_dont_leave_lock_in_place(self):
         branch = self.make_branch('b')
         # Create a lock on disk.
-        tokens = branch.lock_write()
+        token = branch.lock_write()
         try:
-            if tokens is None:
+            if token is None:
                 # This test does not apply, because this branch refuses lock
                 # tokens.
                 self.assertRaises(NotImplementedError,
@@ -403,12 +393,19 @@
                 # This branch doesn't support this API.
                 return
             branch.repository.leave_lock_in_place()
+            repo_token = branch.repository.lock_write()
+            branch.repository.unlock()
         finally:
             branch.unlock()
         # Reacquire the lock (with a different branch object) by using the
         # tokens.
         new_branch = branch.bzrdir.open_branch()
-        new_branch.lock_write(tokens=tokens)
+        # We have to explicitly lock the repository first.
+        new_branch.repository.lock_write(token=repo_token)
+        new_branch.lock_write(token=token)
+        # Now we don't need our own repository lock anymore (the branch is
+        # holding it for us).
+        new_branch.repository.unlock()
         # Call dont_leave_lock_in_place, so that the lock will be released by
         # this instance, even though the lock wasn't originally acquired by it.
         new_branch.dont_leave_lock_in_place()




More information about the bazaar-commits mailing list