Skip to content

Commit c772817

Browse files
committed
tests/micropython: Add tests for micropython.schedule().
1 parent 9ee4641 commit c772817

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

tests/micropython/schedule.py

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

tests/micropython/schedule.py.exp

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ def run_tests(pyb, tests, args):
319319
skip_tests.add('misc/sys_exc_info.py') # sys.exc_info() is not supported for native
320320
skip_tests.add('micropython/heapalloc_traceback.py') # because native doesn't have proper traceback info
321321
skip_tests.add('micropython/heapalloc_iter.py') # requires generators
322+
skip_tests.add('micropython/schedule.py') # native code doesn't check pending events
322323

323324
for test_file in tests:
324325
test_file = test_file.replace('\\', '/')

0 commit comments

Comments
 (0)