Skip to content

Commit 7151372

Browse files
committed
Test lock releases with/without arguments
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.
1 parent 8bdc522 commit 7151372

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

tests/test_sync_lock.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,29 @@ def g():
112112
class DummyException(Exception):
113113
pass
114114

115-
def test_rlock_released_after_error(this_lock):
115+
def test_rlock_released_after_error_args(this_lock):
116+
"""If an exception occurs in a with block, the lock should release.
117+
118+
NB there are two sets of code that do this - one if arguments are
119+
given (i.e. the __call__ method of the lock class) and one without
120+
arguments (i.e. the __enter__ and __exit__ methods).
121+
122+
See the following function for the no-arguments version.
123+
"""
124+
try:
125+
with this_lock():
126+
assert this_lock.locked()
127+
raise DummyException()
128+
except DummyException:
129+
pass
130+
assert not this_lock.locked()
131+
132+
def test_rlock_released_after_error_noargs(this_lock):
133+
"""If an exception occurs in a with block, the lock should release."""
116134
try:
117135
with this_lock:
136+
assert this_lock.locked()
118137
raise DummyException()
119138
except DummyException:
120-
assert not this_lock.locked()
139+
pass
140+
assert not this_lock.locked()

0 commit comments

Comments
 (0)