Skip to content

Commit a003ac2

Browse files
committed
tests/thread: Add a test for accuracy of sleep within a thread.
The existing thread_sleep1.py test only tests execution, not accuracy, of time.sleep. Also the existing test only tests sleep(0) on targets like rp2 that can only create a single thread. The new test in this commit checks for timing accuracy on the main thread and one other thread when they run at the same time. Signed-off-by: Damien George <[email protected]>
1 parent 2265d70 commit a003ac2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tests/thread/thread_sleep2.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Test accuracy of sleep within a thread.
2+
3+
import time
4+
import _thread
5+
6+
7+
def sleep(t, valid_range):
8+
t0 = time.time_ns()
9+
time.sleep(t)
10+
dt_ms = (time.time_ns() - t0) // 1_000_000
11+
if dt_ms in valid_range:
12+
print("dt in range", t)
13+
else:
14+
print("dt not in range:", dt_ms)
15+
16+
17+
def thread_entry():
18+
lock.acquire()
19+
print("thread start")
20+
sleep(0.2, range(180, 400))
21+
print("thread end")
22+
23+
24+
lock = _thread.allocate_lock()
25+
lock.acquire()
26+
_thread.start_new_thread(thread_entry, ())
27+
28+
print("main start")
29+
lock.release()
30+
sleep(0.5, range(480, 800))
31+
print("main end")

0 commit comments

Comments
 (0)