circuitpython/tests/micropython/schedule.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
1.2 KiB
Python
Raw Normal View History

# test micropython.schedule() function
import micropython
try:
micropython.schedule
except AttributeError:
2021-03-15 09:57:36 -04:00
print("SKIP")
raise SystemExit
# Basic test of scheduling a function.
2021-03-15 09:57:36 -04:00
def callback(arg):
global done
print(arg)
done = True
2021-03-15 09:57:36 -04:00
done = False
micropython.schedule(callback, 1)
while not done:
pass
# Test that callbacks can be scheduled from within a callback, but
# that they don't execute until the outer callback is finished.
2021-03-15 09:57:36 -04:00
def callback_inner(arg):
global done
2021-03-15 09:57:36 -04:00
print("inner")
done += 1
2021-03-15 09:57:36 -04:00
def callback_outer(arg):
global done
micropython.schedule(callback_inner, 0)
# need a loop so that the VM can check for pending events
for i in range(2):
pass
2021-03-15 09:57:36 -04:00
print("outer")
done += 1
2021-03-15 09:57:36 -04:00
done = 0
micropython.schedule(callback_outer, 0)
while done != 2:
pass
# Test that scheduling too many callbacks leads to an exception. To do this we
# must schedule from within a callback to guarantee that the scheduler is locked.
2021-03-15 09:57:36 -04:00
def callback(arg):
global done
try:
for i in range(100):
2021-03-15 09:57:36 -04:00
micropython.schedule(lambda x: x, None)
except RuntimeError:
2021-03-15 09:57:36 -04:00
print("RuntimeError")
done = True
2021-03-15 09:57:36 -04:00
done = False
micropython.schedule(callback, None)
while not done:
pass