summaryrefslogtreecommitdiff
path: root/tests/thread/thread_lock1.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-04-19 17:03:26 +0000
committerDamien George <damien.p.george@gmail.com>2016-06-28 11:09:31 +0100
commited36632c6cc227206994cf68b4a4afec2b694e49 (patch)
treebc7b3c4f7e5882dada6bce99e8dd54517c1f8284 /tests/thread/thread_lock1.py
parentf2892bd77da5f7adbad3d83bb6e0898af224975b (diff)
tests: Add tests for _thread module.
Includes functionality and stress tests.
Diffstat (limited to 'tests/thread/thread_lock1.py')
-rw-r--r--tests/thread/thread_lock1.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/thread/thread_lock1.py b/tests/thread/thread_lock1.py
new file mode 100644
index 000000000..ca585ffbb
--- /dev/null
+++ b/tests/thread/thread_lock1.py
@@ -0,0 +1,40 @@
+# 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())