Skip to content

Commit

Permalink
Merge pull request #24 from luizalabs/ro-wrong_lock_release-fix
Browse files Browse the repository at this point in the history
Fix wrong lock release when CacheLock is used with raise_exception=False
  • Loading branch information
jairhenrique committed Apr 7, 2017
2 parents 767aa20 + e1b2ba5 commit 157ad65
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion django_toolkit/concurrent/locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@ def __enter__(self):
return self

def __exit__(self, *args, **kwargs):
if self.active:
self.cache.delete(self._key)

self.active = False
self.cache.delete(self._key)
23 changes: 23 additions & 0 deletions tests/concurrent/test_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,26 @@ def test_should_use_default_cache_timeout_when_expire_is_not_given(self):
def test_should_expire_immediately_when_expire_is_zero(self):
with CacheLock(key='test', expire=0) as lock:
assert not lock.cache.get(lock._key)

def test_should_not_release_when_lock_is_already_acquired(self):
"""
It should release the lock only if it was acquired successfully.
A lock that do not acquired a lock should not release it.
"""
with CacheLock(key='test', raise_exception=False) as lock:
with CacheLock(key='test', raise_exception=False):
pass

assert lock.cache.get(lock._key)

assert not lock.cache.get(lock._key)

def test_should_not_release_when_lock_when_lock_active_error_is_raised(
self
):
with CacheLock(key='test') as lock:
with pytest.raises(LockActiveError):
with CacheLock(key='test'):
pass

assert lock.cache.get(lock._key)

0 comments on commit 157ad65

Please sign in to comment.