diff --git a/cachecontrol/caches/file_cache.py b/cachecontrol/caches/file_cache.py index 43393b8f..38fa7734 100644 --- a/cachecontrol/caches/file_cache.py +++ b/cachecontrol/caches/file_cache.py @@ -67,6 +67,7 @@ def __init__( dirmode=0o0700, use_dir_lock=None, lock_class=None, + lock_timeout=30, ): if use_dir_lock is not None and lock_class is not None: @@ -97,6 +98,7 @@ def __init__( self.filemode = filemode self.dirmode = dirmode self.lock_class = lock_class + self.lock_timeout = lock_timeout @staticmethod def encode(x): @@ -127,7 +129,7 @@ def set(self, key, value): except (IOError, OSError): pass - with self.lock_class(name) as lock: + with self.lock_class(name, timeout=self.lock_timeout) as lock: # Write our actual file with _secure_open_write(lock.path, self.filemode) as fh: fh.write(value) diff --git a/tests/test_storage_filecache.py b/tests/test_storage_filecache.py index 4ac8a4f0..c09dd1be 100644 --- a/tests/test_storage_filecache.py +++ b/tests/test_storage_filecache.py @@ -12,6 +12,7 @@ import pytest import requests +from mock import ANY, Mock from cachecontrol import CacheControl from cachecontrol.caches import FileCache from lockfile import LockFile @@ -115,6 +116,24 @@ def test_lock_class(self, tmpdir): assert cache.lock_class is lock_class cache.close() + def test_default_lock_timeout_is_passed_to_lock(self, tmpdir): + lock = Mock(__enter__=Mock(), __exit__=Mock()) + lock_class = Mock(return_value=lock) + + self.cache = FileCache(str(tmpdir), lock_class=lock_class) + self.cache.set('key', b'value') + lock_class.assert_called_once_with(ANY, timeout=30) + + def test_lock_timeout_of_none_is_passed_to_lock(self, tmpdir): + lock = Mock(__enter__=Mock(), __exit__=Mock()) + lock_class = Mock(return_value=lock) + + self.cache = FileCache(str(tmpdir), lock_class=lock_class, + lock_timeout=None) + self.cache.set('key', b'value') + lock_class.assert_called_once_with(ANY, timeout=None) + cache.close() + def test_filecache_with_delete_request(self, tmpdir, sess): # verifies issue #155 url = self.url + "".join(sample(string.ascii_lowercase, randint(2, 4)))