summaryrefslogtreecommitdiff
path: root/tests/test_lock.py
diff options
context:
space:
mode:
authorIhor Kalnytskyi <ihor@kalnytskyi.com>2018-07-24 19:14:50 +0300
committerIhor Kalnytskyi <ihor@kalnytskyi.com>2018-12-28 22:03:15 +0200
commitff120df78ccd85d6e2e2938ee02d1eb831676724 (patch)
tree33394c68b38ed4d0431142a560fc2c97719298c9 /tests/test_lock.py
parent4ae98e7de18f16fb669b74ee09245ff4de3da0b4 (diff)
downloadredis-py-ff120df78ccd85d6e2e2938ee02d1eb831676724.tar.gz
Add `.reacquire()` method to Lock
`Lock` class provides a method called `.extend()` to manage a TTL of the acquired lock. However, the method allows only to extend a timeout of existing lock by N seconds, there's no way you can reset a TTL to the timeout value you passed to this lock. There could be multiple use cases for such behaviour. For instance, one may want to use a lock to implement active/passive behaviour where only one process owns a lock and resets its TTL all over again until it dies. This commit adds a new method called `.reacquire()` to reset a TTL of the acquired lock back to the passed timeout value.
Diffstat (limited to 'tests/test_lock.py')
-rw-r--r--tests/test_lock.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/test_lock.py b/tests/test_lock.py
index ea45379..945fe2f 100644
--- a/tests/test_lock.py
+++ b/tests/test_lock.py
@@ -125,6 +125,34 @@ class TestLock(object):
with pytest.raises(LockNotOwnedError):
lock.extend(10)
+ def test_reacquire_lock(self, r):
+ lock = self.get_lock(r, 'foo', timeout=10)
+ assert lock.acquire(blocking=False)
+ assert r.pexpire('foo', 5000)
+ assert r.pttl('foo') <= 5000
+ assert lock.reacquire()
+ assert 8000 < r.pttl('foo') <= 10000
+ lock.release()
+
+ def test_reacquiring_unlocked_lock_raises_error(self, r):
+ lock = self.get_lock(r, 'foo', timeout=10)
+ with pytest.raises(LockError):
+ lock.reacquire()
+
+ def test_reacquiring_lock_with_no_timeout_raises_error(self, r):
+ lock = self.get_lock(r, 'foo')
+ assert lock.acquire(blocking=False)
+ with pytest.raises(LockError):
+ lock.reacquire()
+ lock.release()
+
+ def test_reacquiring_lock_no_longer_owned_raises_error(self, r):
+ lock = self.get_lock(r, 'foo', timeout=10)
+ assert lock.acquire(blocking=False)
+ r.set('foo', 'a')
+ with pytest.raises(LockNotOwnedError):
+ lock.reacquire()
+
class TestLockClassSelection(object):
def test_lock_class_argument(self, r):