summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--redis/lock.py5
-rw-r--r--tests/test_lock.py6
2 files changed, 9 insertions, 2 deletions
diff --git a/redis/lock.py b/redis/lock.py
index fb73ce2..05d4153 100644
--- a/redis/lock.py
+++ b/redis/lock.py
@@ -125,8 +125,9 @@ class Lock(object):
def __enter__(self):
# force blocking, as otherwise the user would have to check whether
# the lock was actually acquired or not.
- self.acquire(blocking=True)
- return self
+ if self.acquire(blocking=True):
+ return self
+ raise LockError("Unable to acquire lock within the time specified")
def __exit__(self, exc_type, exc_value, traceback):
self.release()
diff --git a/tests/test_lock.py b/tests/test_lock.py
index 9cbc2f8..a9ecf54 100644
--- a/tests/test_lock.py
+++ b/tests/test_lock.py
@@ -56,6 +56,12 @@ class TestLock(object):
assert r.get('foo') == lock.local.token
assert r.get('foo') is None
+ def test_context_manager_raises_when_locked_not_acquired(self, r):
+ r.set('foo', 'bar')
+ with pytest.raises(LockError):
+ with self.get_lock(r, 'foo', blocking_timeout=0.1):
+ pass
+
def test_high_sleep_raises_error(self, r):
"If sleep is higher than timeout, it should raise an error"
with pytest.raises(LockError):