Merge "Require lock_path limit to be a positive int"

This commit is contained in:
Zuul 2017-11-07 19:33:02 +00:00 committed by Gerrit Code Review
commit 0da972bbb0
2 changed files with 24 additions and 0 deletions

View File

@ -2338,7 +2338,11 @@ def lock_path(directory, timeout=10, timeout_class=None, limit=1):
the same directory at the time this method is called. Note that this
limit is only applied during the current call to this method and does
not prevent subsequent calls giving a larger limit. Defaults to 1.
:raises TypeError: if limit is not an int.
:raises ValueError: if limit is less than 1.
"""
if limit < 1:
raise ValueError('limit must be greater than or equal to 1')
if timeout_class is None:
timeout_class = swift.common.exceptions.LockTimeout
mkdirs(directory)

View File

@ -963,6 +963,26 @@ class TestUtils(unittest.TestCase):
success = True
self.assertFalse(success)
@with_tempdir
def test_lock_path_invalid_limit(self, tmpdir):
success = False
with self.assertRaises(ValueError):
with utils.lock_path(tmpdir, 0.1, limit=0):
success = True
self.assertFalse(success)
with self.assertRaises(ValueError):
with utils.lock_path(tmpdir, 0.1, limit=-1):
success = True
self.assertFalse(success)
with self.assertRaises(TypeError):
with utils.lock_path(tmpdir, 0.1, limit='1'):
success = True
self.assertFalse(success)
with self.assertRaises(TypeError):
with utils.lock_path(tmpdir, 0.1, limit=1.1):
success = True
self.assertFalse(success)
@with_tempdir
def test_lock_path_num_sleeps(self, tmpdir):
num_short_calls = [0]