Update lockutils from oslo-incubator

This commit updates lockutils from oslo incubator which adds a
LockFixture class. This also converts the tempest LockFixture to wrap
the fixture from oslo.

Change-Id: I4f5007109fa6ceff868c23d55d9962f9d703bb1f
This commit is contained in:
Matthew Treinish
2013-10-09 18:20:57 -04:00
parent b3e9c228b5
commit 968a317e6a
2 changed files with 36 additions and 9 deletions

View File

@@ -15,16 +15,9 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import fixtures
from tempest.openstack.common import lockutils from tempest.openstack.common import lockutils
class LockFixture(fixtures.Fixture): class LockFixture(lockutils.LockFixture):
def __init__(self, name): def __init__(self, name):
self.mgr = lockutils.lock(name, 'tempest-', True) super(LockFixture, self).__init__(name, 'tempest-')
def setUp(self):
super(LockFixture, self).setUp()
self.addCleanup(self.mgr.__exit__, None, None, None)
self.mgr.__enter__()

View File

@@ -24,6 +24,7 @@ import threading
import time import time
import weakref import weakref
import fixtures
from oslo.config import cfg from oslo.config import cfg
from tempest.openstack.common import fileutils from tempest.openstack.common import fileutils
@@ -275,3 +276,36 @@ def synchronized_with_prefix(lock_file_prefix):
""" """
return functools.partial(synchronized, lock_file_prefix=lock_file_prefix) return functools.partial(synchronized, lock_file_prefix=lock_file_prefix)
class LockFixture(fixtures.Fixture):
"""External locking fixture.
This fixture is basically an alternative to the synchronized decorator with
the external flag so that tearDowns and addCleanups will be included in
the lock context for locking between tests. The fixture is recommended to
be the first line in a test method, like so::
def test_method(self):
self.useFixture(LockFixture)
...
or the first line in setUp if all the test methods in the class are
required to be serialized. Something like::
class TestCase(testtools.testcase):
def setUp(self):
self.useFixture(LockFixture)
super(TestCase, self).setUp()
...
This is because addCleanups are put on a LIFO queue that gets run after the
test method exits. (either by completing or raising an exception)
"""
def __init__(self, name, lock_file_prefix=None):
self.mgr = lock(name, lock_file_prefix, True)
def setUp(self):
super(LockFixture, self).setUp()
self.addCleanup(self.mgr.__exit__, None, None, None)
self.mgr.__enter__()