diff --git a/tempest/common/tempest_fixtures.py b/tempest/common/tempest_fixtures.py index 081b271ae0..ebc9ad332e 100644 --- a/tempest/common/tempest_fixtures.py +++ b/tempest/common/tempest_fixtures.py @@ -15,16 +15,9 @@ # License for the specific language governing permissions and limitations # under the License. -import fixtures - from tempest.openstack.common import lockutils -class LockFixture(fixtures.Fixture): +class LockFixture(lockutils.LockFixture): def __init__(self, name): - self.mgr = lockutils.lock(name, 'tempest-', True) - - def setUp(self): - super(LockFixture, self).setUp() - self.addCleanup(self.mgr.__exit__, None, None, None) - self.mgr.__enter__() + super(LockFixture, self).__init__(name, 'tempest-') diff --git a/tempest/openstack/common/lockutils.py b/tempest/openstack/common/lockutils.py index 0abd1a7713..a55fd94047 100644 --- a/tempest/openstack/common/lockutils.py +++ b/tempest/openstack/common/lockutils.py @@ -24,6 +24,7 @@ import threading import time import weakref +import fixtures from oslo.config import cfg 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) + + +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__()