tests/extmod: Add heap-lock test for stream writing.
Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
parent
c21452a1d2
commit
2a2589738c
@ -1,4 +1,8 @@
|
|||||||
# test that basic scheduling of tasks, and uasyncio.sleep_ms, does not use the heap
|
# test that the following do not use the heap:
|
||||||
|
# - basic scheduling of tasks
|
||||||
|
# - uasyncio.sleep_ms
|
||||||
|
# - StreamWriter.write, stream is blocked and data to write is a bytes object
|
||||||
|
# - StreamWriter.write, when stream is not blocked
|
||||||
|
|
||||||
import micropython
|
import micropython
|
||||||
|
|
||||||
@ -22,6 +26,17 @@ except ImportError:
|
|||||||
raise SystemExit
|
raise SystemExit
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
async def task(id, n, t):
|
async def task(id, n, t):
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
print(id, i)
|
print(id, i)
|
||||||
@ -32,14 +47,27 @@ async def main():
|
|||||||
t1 = asyncio.create_task(task(1, 4, 100))
|
t1 = asyncio.create_task(task(1, 4, 100))
|
||||||
t2 = asyncio.create_task(task(2, 2, 250))
|
t2 = asyncio.create_task(task(2, 2, 250))
|
||||||
|
|
||||||
|
# test scheduling tasks, and calling sleep_ms
|
||||||
micropython.heap_lock()
|
micropython.heap_lock()
|
||||||
|
|
||||||
print("start")
|
print("start")
|
||||||
await asyncio.sleep_ms(5)
|
await asyncio.sleep_ms(5)
|
||||||
print("sleep")
|
print("sleep")
|
||||||
await asyncio.sleep_ms(350)
|
await asyncio.sleep_ms(350)
|
||||||
print("finish")
|
print("finish")
|
||||||
|
micropython.heap_unlock()
|
||||||
|
|
||||||
|
# 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)
|
||||||
micropython.heap_unlock()
|
micropython.heap_unlock()
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,3 +7,6 @@ sleep
|
|||||||
2 1
|
2 1
|
||||||
1 3
|
1 3
|
||||||
finish
|
finish
|
||||||
|
TestStream.write b'12'
|
||||||
|
TestStream.write b'34'
|
||||||
|
TestStream.write bytearray(b'56')
|
||||||
|
Loading…
Reference in New Issue
Block a user