1# test _thread lock object using a single thread
2#
3# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
4
5import _thread
6
7# create lock
8lock = _thread.allocate_lock()
9
10print(type(lock) == _thread.LockType)
11
12# should be unlocked
13print(lock.locked())
14
15# basic acquire and release
16print(lock.acquire())
17print(lock.locked())
18lock.release()
19print(lock.locked())
20
21# try acquire twice (second should fail)
22print(lock.acquire())
23print(lock.locked())
24print(lock.acquire(0))
25print(lock.locked())
26lock.release()
27print(lock.locked())
28
29# test with capabilities of lock
30with lock:
31    print(lock.locked())
32
33# test that lock is unlocked if an error is rasied
34try:
35    with lock:
36        print(lock.locked())
37        raise KeyError
38except KeyError:
39    print("KeyError")
40    print(lock.locked())
41
42# test that we can't release an unlocked lock
43try:
44    lock.release()
45except RuntimeError:
46    print("RuntimeError")
47