oslo_concurrency.fixture.lockutils

class oslo_concurrency.fixture.lockutils.ExternalLockFixture

Bases: fixtures.fixture.Fixture

Configure lock_path so external locks can be used in unit tests.

Creates a temporary directory to hold file locks and sets the oslo.config lock_path opt to use it. This can be used to enable external locking on a per-test basis, rather than globally with the OSLO_LOCK_PATH environment variable.

Example:

def test_method(self):
    self.useFixture(ExternalLockFixture())
    something_that_needs_external_locks()

Alternatively, the useFixture call could be placed in a test class’s setUp method to provide this functionality to all tests in the class.

New in version 0.3.

setUp()

Prepare the Fixture for use.

This should not be overridden. Concrete fixtures should implement _setUp. Overriding of setUp is still supported, just not recommended.

After setUp has completed, the fixture will have one or more attributes which can be used (these depend totally on the concrete subclass).

Raises

MultipleExceptions if _setUp fails. The last exception captured within the MultipleExceptions will be a SetupError exception.

Returns

None.

Changed in 1.3

The recommendation to override setUp has been reversed - before 1.3, setUp() should be overridden, now it should not be.

Changed in 1.3.1

BaseException is now caught, and only subclasses of Exception are wrapped in MultipleExceptions.

class oslo_concurrency.fixture.lockutils.LockFixture(name, lock_file_prefix=None)

Bases: fixtures.fixture.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('lock_name'))
        ...

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('lock_name'))
        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)

setUp()

Prepare the Fixture for use.

This should not be overridden. Concrete fixtures should implement _setUp. Overriding of setUp is still supported, just not recommended.

After setUp has completed, the fixture will have one or more attributes which can be used (these depend totally on the concrete subclass).

Raises

MultipleExceptions if _setUp fails. The last exception captured within the MultipleExceptions will be a SetupError exception.

Returns

None.

Changed in 1.3

The recommendation to override setUp has been reversed - before 1.3, setUp() should be overridden, now it should not be.

Changed in 1.3.1

BaseException is now caught, and only subclasses of Exception are wrapped in MultipleExceptions.