micropython/tests/thread/thread_lock1.py
David Lechner 3dc324d3f1 tests: Format all Python code with black, except tests in basics subdir.
This adds the Python files in the tests/ directory to be formatted with
./tools/codeformat.py.  The basics/ subdirectory is excluded for now so we
aren't changing too much at once.

In a few places `# fmt: off`/`# fmt: on` was used where the code had
special formatting for readability or where the test was actually testing
the specific formatting.
2020-03-30 13:21:58 +11:00

47 lines
918 B
Python

# test _thread lock object using a single thread
#
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import _thread
# create lock
lock = _thread.allocate_lock()
print(type(lock) == _thread.LockType)
# should be unlocked
print(lock.locked())
# basic acquire and release
print(lock.acquire())
print(lock.locked())
lock.release()
print(lock.locked())
# try acquire twice (second should fail)
print(lock.acquire())
print(lock.locked())
print(lock.acquire(0))
print(lock.locked())
lock.release()
print(lock.locked())
# test with capabilities of lock
with lock:
print(lock.locked())
# test that lock is unlocked if an error is rasied
try:
with lock:
print(lock.locked())
raise KeyError
except KeyError:
print("KeyError")
print(lock.locked())
# test that we can't release an unlocked lock
try:
lock.release()
except RuntimeError:
print("RuntimeError")