Skip to content

Commit

Permalink
Test lock releases with/without arguments
Browse files Browse the repository at this point in the history
StrictLock and CompositeLock can be used as context managers in 2 ways.
They are context manager classes (__enter__ and __exit__ methods) but
the __call__ method is a generator context manager too - allowing
arguments.

This code now tests both forms.
  • Loading branch information
rwb27 committed Aug 23, 2021
1 parent 8bdc522 commit 7151372
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions tests/test_sync_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,29 @@ def g():
class DummyException(Exception):
pass

def test_rlock_released_after_error(this_lock):
def test_rlock_released_after_error_args(this_lock):
"""If an exception occurs in a with block, the lock should release.
NB there are two sets of code that do this - one if arguments are
given (i.e. the __call__ method of the lock class) and one without
arguments (i.e. the __enter__ and __exit__ methods).
See the following function for the no-arguments version.
"""
try:
with this_lock():
assert this_lock.locked()
raise DummyException()
except DummyException:
pass
assert not this_lock.locked()

def test_rlock_released_after_error_noargs(this_lock):
"""If an exception occurs in a with block, the lock should release."""
try:
with this_lock:
assert this_lock.locked()
raise DummyException()
except DummyException:
assert not this_lock.locked()
pass
assert not this_lock.locked()

0 comments on commit 7151372

Please sign in to comment.