From 3545ef8bb49d9ad02e85bd13f478072f8059c582 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 21 Apr 2016 12:38:22 +0000 Subject: [PATCH] tests/thread: Remove need to sleep to wait for completion in some tests. Use a lock and a counter instead, and busy wait for all threads to complete. This makes test run faster and they no longer rely on the time module. --- tests/thread/thread_exc1.py | 18 ++++++++++++------ tests/thread/thread_ident1.py | 10 +++++----- tests/thread/thread_shared1.py | 23 +++++++++++++++-------- tests/thread/thread_shared2.py | 23 +++++++++++++++-------- tests/thread/thread_stacksize1.py | 17 +++++++++++------ tests/thread/thread_stress_recurse.py | 12 +++++++----- 6 files changed, 65 insertions(+), 38 deletions(-) diff --git a/tests/thread/thread_exc1.py b/tests/thread/thread_exc1.py index 9ef3d2a15..10fb94b4f 100644 --- a/tests/thread/thread_exc1.py +++ b/tests/thread/thread_exc1.py @@ -2,10 +2,6 @@ # # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd -try: - import utime as time -except ImportError: - import time import _thread def foo(): @@ -16,9 +12,19 @@ def thread_entry(): foo() except ValueError: pass + with lock: + global n_finished + n_finished += 1 -for i in range(4): +lock = _thread.allocate_lock() +n_thread = 4 +n_finished = 0 + +# spawn threads +for i in range(n_thread): _thread.start_new_thread(thread_entry, ()) -time.sleep(0.2) +# busy wait for threads to finish +while n_finished < n_thread: + pass print('done') diff --git a/tests/thread/thread_ident1.py b/tests/thread/thread_ident1.py index f32956676..217fce73b 100644 --- a/tests/thread/thread_ident1.py +++ b/tests/thread/thread_ident1.py @@ -2,20 +2,20 @@ # # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd -try: - import utime as time -except ImportError: - import time import _thread def thread_entry(): tid = _thread.get_ident() print('thread', type(tid) == int, tid != 0, tid != tid_main) + global finished + finished = True tid_main = _thread.get_ident() print('main', type(tid_main) == int, tid_main != 0) +finished = False _thread.start_new_thread(thread_entry, ()) -time.sleep(0.2) +while not finished: + pass print('done') diff --git a/tests/thread/thread_shared1.py b/tests/thread/thread_shared1.py index 6ef4b654f..13c6651cc 100644 --- a/tests/thread/thread_shared1.py +++ b/tests/thread/thread_shared1.py @@ -2,10 +2,6 @@ # # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd -try: - import utime as time -except ImportError: - import time import _thread def foo(i): @@ -14,11 +10,22 @@ def foo(i): def thread_entry(n, tup): for i in tup: foo(i) + with lock: + global n_finished + n_finished += 1 +lock = _thread.allocate_lock() +n_thread = 2 +n_finished = 0 + +# the shared data structure tup = (1, 2, 3, 4) -_thread.start_new_thread(thread_entry, (100, tup)) -_thread.start_new_thread(thread_entry, (100, tup)) -# wait for threads to finish -time.sleep(0.2) +# spawn threads +for i in range(n_thread): + _thread.start_new_thread(thread_entry, (100, tup)) + +# busy wait for threads to finish +while n_finished < n_thread: + pass print(tup) diff --git a/tests/thread/thread_shared2.py b/tests/thread/thread_shared2.py index 3e3e2dc33..e4bfe7802 100644 --- a/tests/thread/thread_shared2.py +++ b/tests/thread/thread_shared2.py @@ -3,10 +3,6 @@ # # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd -try: - import utime as time -except ImportError: - import time import _thread def foo(lst, i): @@ -15,11 +11,22 @@ def foo(lst, i): def thread_entry(n, lst, idx): for i in range(n): foo(lst, idx) + with lock: + global n_finished + n_finished += 1 +lock = _thread.allocate_lock() +n_thread = 2 +n_finished = 0 + +# the shared data structure lst = [0, 0] -_thread.start_new_thread(thread_entry, (10, lst, 0)) -_thread.start_new_thread(thread_entry, (20, lst, 1)) -# wait for threads to finish -time.sleep(0.2) +# spawn threads +for i in range(n_thread): + _thread.start_new_thread(thread_entry, ((i + 1) * 10, lst, i)) + +# busy wait for threads to finish +while n_finished < n_thread: + pass print(lst) diff --git a/tests/thread/thread_stacksize1.py b/tests/thread/thread_stacksize1.py index b0118843b..e3e77fada 100644 --- a/tests/thread/thread_stacksize1.py +++ b/tests/thread/thread_stacksize1.py @@ -3,10 +3,6 @@ # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd import sys -try: - import utime as time -except ImportError: - import time import _thread # different implementations have different minimum sizes @@ -20,6 +16,9 @@ def foo(): def thread_entry(): foo() + with lock: + global n_finished + n_finished += 1 # test set/get of stack size print(_thread.stack_size()) @@ -27,10 +26,16 @@ print(_thread.stack_size(sz)) print(_thread.stack_size() == sz) print(_thread.stack_size()) +lock = _thread.allocate_lock() +n_thread = 2 +n_finished = 0 + # set stack size and spawn a few threads _thread.stack_size(sz) -for i in range(2): +for i in range(n_thread): _thread.start_new_thread(thread_entry, ()) -time.sleep(0.2) +# busy wait for threads to finish +while n_finished < n_thread: + pass print('done') diff --git a/tests/thread/thread_stress_recurse.py b/tests/thread/thread_stress_recurse.py index eb9c136f2..68367c4dd 100644 --- a/tests/thread/thread_stress_recurse.py +++ b/tests/thread/thread_stress_recurse.py @@ -2,10 +2,6 @@ # # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd -try: - import utime as time -except ImportError: - import time import _thread def foo(): @@ -16,8 +12,14 @@ def thread_entry(): foo() except RuntimeError: print('RuntimeError') + global finished + finished = True + +finished = False _thread.start_new_thread(thread_entry, ()) -time.sleep(0.2) +# busy wait for thread to finish +while not finished: + pass print('done')