Skip to content

Commit f387484

Browse files
jeplerdpgeorge
authored andcommitted
tests/micropython: Add new schedule_sleep.py test.
This variant of `schedule.py` explicitly calls a zero sleep. The existing variant is kept to ensure the scheduler is called between bytecodes. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
1 parent 2782d45 commit f387484

4 files changed

Lines changed: 79 additions & 1 deletion

File tree

tests/micropython/schedule.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# test micropython.schedule() function
2+
# this test should be manually kept in synch with
3+
# tests/micrpython/schedule_sleep.py.
24

35
try:
46
import micropython
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# test micropython.schedule() function
2+
# this is the same as tests/micropython/schedule.py but the busy loops are
3+
# replaced with sleep/sleep_ms which allows the test to succeed when run under
4+
# the native emitter.
5+
6+
try:
7+
import micropython
8+
import time
9+
10+
micropython.schedule
11+
except (ImportError, AttributeError):
12+
print("SKIP")
13+
raise SystemExit
14+
15+
16+
# Basic test of scheduling a function.
17+
18+
19+
def callback(arg):
20+
global done
21+
print(arg)
22+
done = True
23+
24+
25+
done = False
26+
micropython.schedule(callback, 1)
27+
while not done:
28+
time.sleep(0)
29+
30+
# Test that callbacks can be scheduled from within a callback, but
31+
# that they don't execute until the outer callback is finished.
32+
33+
34+
def callback_inner(arg):
35+
global done
36+
print("inner")
37+
done += 1
38+
39+
40+
def callback_outer(arg):
41+
global done
42+
micropython.schedule(callback_inner, 0)
43+
# need a loop so that the VM can check for pending events
44+
for i in range(2):
45+
pass
46+
print("outer")
47+
done += 1
48+
49+
50+
done = 0
51+
micropython.schedule(callback_outer, 0)
52+
while done != 2:
53+
time.sleep(0)
54+
55+
# Test that scheduling too many callbacks leads to an exception. To do this we
56+
# must schedule from within a callback to guarantee that the scheduler is locked.
57+
58+
59+
def callback(arg):
60+
global done
61+
try:
62+
for i in range(100):
63+
micropython.schedule(lambda x: x, None)
64+
except RuntimeError:
65+
print("RuntimeError")
66+
done = True
67+
68+
69+
done = False
70+
micropython.schedule(callback, None)
71+
while not done:
72+
time.sleep_ms(0)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1
2+
outer
3+
inner
4+
RuntimeError

tests/run-tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def open(self, path, mode):
164164
"basics/exception_chain.py",
165165
# These require stack-allocated slice optimisation.
166166
"micropython/heapalloc_slice.py",
167-
# These require running the scheduler.
167+
# These require implicitly running the scheduler between bytecodes.
168168
"micropython/schedule.py",
169169
"extmod/asyncio_event_queue.py",
170170
"extmod/asyncio_iterator_event.py",

0 commit comments

Comments
 (0)