From 71513727c558211c53a08f86e8fe001bb2527d88 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 23 Aug 2021 17:35:03 +0100 Subject: [PATCH] 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. --- tests/test_sync_lock.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/test_sync_lock.py b/tests/test_sync_lock.py index 0b77a70..53541b7 100644 --- a/tests/test_sync_lock.py +++ b/tests/test_sync_lock.py @@ -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() \ No newline at end of file + pass + assert not this_lock.locked() \ No newline at end of file