diff --git a/swift/common/utils.py b/swift/common/utils.py index e49053338f..839789ef78 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -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) diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index d436e2c02c..76e881e102 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -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]