Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bound how long to wait for lock files in FileCache. #101

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion cachecontrol/caches/file_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_storage_filecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
Expand Down