Commit 0cc00929 authored by Guido Trotter's avatar Guido Trotter
Browse files

LockSet: make acquire() return the set of names

In a LockSet acquire() returned True on success. This code changes that to
return a set containing the names of the elements acquired. This is still a
true value if we acquired any lock but is slightly more useful (because if
needed one has access to this data without querying for it). The only change
happens if acquiring no locks, which though is a usage which should not
normally happen because it has no practical use.

The patch also changes a some tests to check that the new format is respected.

Reviewed-by: imsnah
parent 8b68f394
......@@ -389,6 +389,8 @@ class LockSet:
except (KeyError):
raise errors.LockError('non-existing lock in set (%s)' % lname)
# This will hold the locknames we effectively acquired.
acquired = set()
# Now acquire_list contains a sorted list of resources and locks we want.
# In order to get them we loop on this (private) list and acquire() them.
# We gave no real guarantee they will still exist till this is done but
......@@ -399,6 +401,7 @@ class LockSet:
try:
# now the lock cannot be deleted, we have it!
self._add_owned(lname)
acquired.add(lname)
except:
# We shouldn't have problems adding the lock to the owners list, but
# if we did we'll try to release this lock and re-raise exception.
......@@ -413,7 +416,7 @@ class LockSet:
self._del_owned(lname)
raise errors.LockError('non-existing lock in set (%s)' % name_fail)
return True
return acquired
def release(self, names=None):
"""Release a set of resource locks, at the same level.
......
......@@ -245,11 +245,11 @@ class TestLockSet(unittest.TestCase):
self.assertEquals(newls._names(), set())
def testAcquireRelease(self):
self.ls.acquire('one')
self.assert_(self.ls.acquire('one'))
self.assertEquals(self.ls._list_owned(), set(['one']))
self.ls.release()
self.assertEquals(self.ls._list_owned(), set())
self.ls.acquire(['one'])
self.assertEquals(self.ls.acquire(['one']), set(['one']))
self.assertEquals(self.ls._list_owned(), set(['one']))
self.ls.release()
self.assertEquals(self.ls._list_owned(), set())
......@@ -261,7 +261,7 @@ class TestLockSet(unittest.TestCase):
self.assertEquals(self.ls._list_owned(), set(['two']))
self.ls.release()
self.assertEquals(self.ls._list_owned(), set())
self.ls.acquire(['one', 'three'])
self.assertEquals(self.ls.acquire(['one', 'three']), set(['one', 'three']))
self.assertEquals(self.ls._list_owned(), set(['one', 'three']))
self.ls.release()
self.assertEquals(self.ls._list_owned(), set())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment