Skip to content

Commit

Permalink
uasyncio.core: test_cancel_sleep: Test for cancelling uasyncio.sleep(…
Browse files Browse the repository at this point in the history
…) call.
  • Loading branch information
pfalcon committed Feb 2, 2019
1 parent 342ad69 commit 08aca0d
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions uasyncio.core/test_cancel_sleep.py
@@ -0,0 +1,33 @@
import utime
try:
import uasyncio.core as asyncio
except ImportError:
import asyncio


requested = None

async def coro():
try:
await asyncio.sleep(1)
assert False, "sleep wasn't cancelled"
except asyncio.CancelledError:
now = utime.ticks_ms()
diff = utime.ticks_diff(now, requested)
print("cancelled at: %s, delay: %s" % (now, diff))
assert diff < 10


async def canceller(task):
global requested
asyncio.cancel(task)
requested = utime.ticks_ms()
print("requested cancel at:", requested)
await asyncio.sleep(1)


loop = asyncio.get_event_loop()

task = coro()
loop.create_task(task)
loop.run_until_complete(canceller(task))

0 comments on commit 08aca0d

Please sign in to comment.