8000 tests/ports/rp2: Add a test case for light sleeping from CPU1. · micropython/micropython@977fd94 · GitHub
[go: up one dir, main page]

Skip to content

Commit 977fd94

Browse files
projectgusdpgeorge
authored andcommitted
tests/ports/rp2: Add a test case for light sleeping from CPU1.
Not currently passing. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 03da155 commit 977fd94

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Verify that a thread running on CPU1 can go to lightsleep
2+
# and wake up in the expected timeframe
3+
import _thread
4+
import time
5+
import unittest
6+
from machine import lightsleep
7+
8+
N_SLEEPS = 5
9+
SLEEP_MS = 250
10+
11+
IDEAL_RUNTIME = N_SLEEPS * SLEEP_MS
12+
MAX_RUNTIME = (N_SLEEPS + 1) * SLEEP_MS
13+
MAX_DELTA = 20
14 8000 +
15+
16+
class LightSleepInThread(unittest.TestCase):
17+
def thread_entry(self, is_thread=True):
18+
for _ in range(N_SLEEPS):
19+
lightsleep(SLEEP_MS)
20+
if is_thread:
21+
self.thread_done = True
22+
23+
def elapsed_ms(self):
24+
return time.ticks_diff(time.ticks_ms(), self.t0)
25+
26+
def setUp(self):
27+
self.thread_done = False
28+
self.t0 = time.ticks_ms()
29+
30+
def test_cpu0_busy(self):
31+
_thread.start_new_thread(self.thread_entry, ())
32+
# CPU0 is busy-waiting not asleep itself
33+
while not self.thread_done:
34+
self.assertLessEqual(self.elapsed_ms(), MAX_RUNTIME)
35+
self.assertAlmostEqual(self.elapsed_ms(), IDEAL_RUNTIME, delta=MAX_DELTA)
36+
37+
def test_cpu0_sleeping(self):
38+
_thread.start_new_thread(self.thread_entry, ())
39+
time.sleep_ms(MAX_RUNTIME)
40+
self.assertTrue(self.thread_done)
41+
self.assertAlmostEqual(self.elapsed_ms(), MAX_RUNTIME, delta=MAX_DELTA)
42+
43+
def test_cpu0_also_lightsleep(self):
44+
_thread.start_new_thread(self.thread_entry, ())
45+
time.sleep(0.050) # account for any delay in starting the thread
46+
self.thread_entry(False) # does the same lightsleep loop, doesn't set the done flag
47+
self.assertTrue(self.thread_done)
48+
# only one thread can actually be in lightsleep at a time to avoid races, so the total
49+
# runtime is doubled by doing it on both CPUs
50+
self.assertAlmostEqual(self.elapsed_ms(), IDEAL_RUNTIME * 2, delta=IDEAL_RUNTIME)
51+
52+
53+
if __name__ == "__main__":
54+
unittest.main()

0 commit comments

Comments
 (0)
0