2022-06-24 07:39:45 +01:00
|
|
|
# test that the following do not use the heap:
|
|
|
|
# - basic scheduling of tasks
|
2023-06-08 07:01:38 +01:00
|
|
|
# - asyncio.sleep_ms
|
2022-06-24 07:39:45 +01:00
|
|
|
# - StreamWriter.write, stream is blocked and data to write is a bytes object
|
|
|
|
# - StreamWriter.write, when stream is not blocked
|
2019-11-13 10:08:22 +00:00
|
|
|
|
|
|
|
import micropython
|
|
|
|
|
|
|
|
# strict stackless builds can't call functions without allocating a frame on the heap
|
|
|
|
try:
|
2022-09-13 07:40:34 +01:00
|
|
|
# force bytecode (in case we're running with emit=native) and verify
|
|
|
|
# that bytecode-calling-bytecode doesn't allocate
|
|
|
|
@micropython.bytecode
|
|
|
|
def f(x):
|
|
|
|
x and f(x - 1)
|
|
|
|
|
2019-11-13 10:08:22 +00:00
|
|
|
micropython.heap_lock()
|
2022-09-13 07:40:34 +01:00
|
|
|
f(1)
|
2019-11-13 10:08:22 +00:00
|
|
|
micropython.heap_unlock()
|
|
|
|
except RuntimeError:
|
2022-09-13 07:40:34 +01:00
|
|
|
# RuntimeError (max recursion depth) not MemoryError because effectively
|
|
|
|
# the recursion depth is at the limit while the heap is locked with
|
|
|
|
# stackless
|
2019-11-13 10:08:22 +00:00
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
try:
|
2023-06-08 07:01:38 +01:00
|
|
|
import asyncio
|
2019-11-13 10:08:22 +00:00
|
|
|
except ImportError:
|
2023-06-08 07:01:38 +01:00
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
2019-11-13 10:08:22 +00:00
|
|
|
|
|
|
|
|
2022-06-24 07:39:45 +01:00
|
|
|
class TestStream:
|
|
|
|
def __init__(self, blocked):
|
|
|
|
self.blocked = blocked
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
print("TestStream.write", data)
|
|
|
|
if self.blocked:
|
|
|
|
return None
|
|
|
|
return len(data)
|
|
|
|
|
|
|
|
|
2019-11-13 10:08:22 +00:00
|
|
|
async def task(id, n, t):
|
|
|
|
for i in range(n):
|
|
|
|
print(id, i)
|
|
|
|
await asyncio.sleep_ms(t)
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
2022-05-03 13:39:58 +01:00
|
|
|
t1 = asyncio.create_task(task(1, 4, 100))
|
|
|
|
t2 = asyncio.create_task(task(2, 2, 250))
|
2019-11-13 10:08:22 +00:00
|
|
|
|
2022-06-24 07:39:45 +01:00
|
|
|
# test scheduling tasks, and calling sleep_ms
|
2019-11-13 10:08:22 +00:00
|
|
|
micropython.heap_lock()
|
|
|
|
print("start")
|
2022-05-03 13:39:58 +01:00
|
|
|
await asyncio.sleep_ms(5)
|
2019-11-13 10:08:22 +00:00
|
|
|
print("sleep")
|
2022-05-03 13:39:58 +01:00
|
|
|
await asyncio.sleep_ms(350)
|
2019-11-13 10:08:22 +00:00
|
|
|
print("finish")
|
2022-06-24 07:39:45 +01:00
|
|
|
micropython.heap_unlock()
|
2019-11-13 10:08:22 +00:00
|
|
|
|
2022-06-24 07:39:45 +01:00
|
|
|
# test writing to a stream, when the underlying stream is blocked
|
|
|
|
s = asyncio.StreamWriter(TestStream(True), None)
|
|
|
|
micropython.heap_lock()
|
|
|
|
s.write(b"12")
|
|
|
|
micropython.heap_unlock()
|
|
|
|
|
|
|
|
# test writing to a stream, when the underlying stream is not blocked
|
|
|
|
buf = bytearray(b"56")
|
|
|
|
s = asyncio.StreamWriter(TestStream(False), None)
|
|
|
|
micropython.heap_lock()
|
|
|
|
s.write(b"34")
|
|
|
|
s.write(buf)
|
2019-11-13 10:08:22 +00:00
|
|
|
micropython.heap_unlock()
|
|
|
|
|
|
|
|
|
|
|
|
asyncio.run(main())
|