Skip to content

Commit e323769

Browse files
committed
cantest.py Add test6.
1 parent 36846cc commit e323769

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

PRIMITIVES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ The following modules are provided:
8888
* `asyn.py` The main library.
8989
* `asyntest.py` Test/demo programs for the primitives.
9090
* `asyn_demos.py` Minimal "get started" task cancellation demos.
91-
* `cantest.py` Task cancellation tests.
91+
* `cantest.py` Task cancellation tests. Examples of intercepting `StopTask`.
92+
Intended to verify the library against future `uasyncio` changes.
9293

93-
Import the test or demo module for a list of available tests.
94+
Import `asyn_demos.py` or `cantest.py` for a list of available tests.
9495

9596
###### [Contents](./PRIMITIVES.md#contents)
9697

cantest.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import uasyncio as asyncio
2727
import asyn
28+
import utime as time
2829

2930
def print_tests():
3031
st = '''Available functions:
@@ -33,6 +34,7 @@ def print_tests():
3334
test3() Cancellation of a NamedTask which has run to completion.
3435
test4() Test of Cancellable class.
3536
test5() Cancellable and NamedTask instances as bound methods.
37+
test6() Test of NamedTask.is_running() and awaiting NamedTask cancellation.
3638
Recommended to issue ctrl-D after running each test.
3739
'''
3840
print('\x1b[32m')
@@ -327,3 +329,96 @@ def test5():
327329
cantest = CanTest()
328330
loop = asyncio.get_event_loop()
329331
loop.run_until_complete(cantest.start(loop))
332+
333+
# test 6: test NamedTask.is_running()
334+
@asyn.cancellable
335+
async def cant60(task_id, name):
336+
task_no = task_id()
337+
print('Task cant60 no. {} name \"{}\" running.'.format(task_no, name))
338+
try:
339+
for _ in range(5):
340+
await asyncio.sleep(2) # 2 secs latency.
341+
except asyn.StopTask:
342+
print('Task cant60 no. {} name \"{}\" was cancelled.'.format(task_no, name))
343+
return
344+
else:
345+
print('Task cant60 no. {} name \"{}\" ended.'.format(task_no, name))
346+
347+
@asyn.cancellable
348+
async def cant61(task_id):
349+
try:
350+
while True:
351+
for name in ('complete', 'cancel me'):
352+
res = asyn.NamedTask.is_running(name)
353+
print('Task \"{}\" running: {}'.format(name, res))
354+
await asyncio.sleep(1)
355+
except asyn.StopTask:
356+
print('Task cant61 cancelled.')
357+
358+
async def run_cancel_test6(loop):
359+
for name in ('complete', 'cancel me'):
360+
loop.create_task(asyn.NamedTask(name, cant60, name)())
361+
loop.create_task(asyn.Cancellable(cant61)())
362+
await asyncio.sleep(4.5)
363+
print('Cancelling task \"{}\". 1.5 secs latency.'.format(name))
364+
await asyn.NamedTask.cancel(name)
365+
await asyncio.sleep(7)
366+
name = 'cancel wait'
367+
loop.create_task(asyn.NamedTask(name, cant60, name)())
368+
await asyncio.sleep(0.5)
369+
print('Cancelling task \"{}\". 1.5 secs latency.'.format(name))
370+
t = time.ticks_ms()
371+
await asyn.NamedTask.cancel('cancel wait', nowait=False)
372+
print('Was cancelled in {} ms'.format(time.ticks_diff(time.ticks_ms(), t)))
373+
print('Cancelling cant61')
374+
await asyn.Cancellable.cancel_all()
375+
print('Done')
376+
377+
378+
def test6():
379+
printexp('''Task cant60 no. 0 name "complete" running.
380+
Task cant60 no. 1 name "cancel me" running.
381+
Task "complete" running: True
382+
Task "cancel me" running: True
383+
Task "complete" running: True
384+
Task "cancel me" running: True
385+
Task "complete" running: True
386+
Task "cancel me" running: True
387+
Task "complete" running: True
388+
Task "cancel me" running: True
389+
Task "complete" running: True
390+
Task "cancel me" running: True
391+
Cancelling task "cancel me". 1.5 secs latency.
392+
Task "complete" running: True
393+
Task "cancel me" running: True
394+
Task cant60 no. 1 name "cancel me" was cancelled.
395+
Task "complete" running: True
396+
Task "cancel me" running: False
397+
Task "complete" running: True
398+
Task "cancel me" running: False
399+
Task "complete" running: True
400+
Task "cancel me" running: False
401+
Task "complete" running: True
402+
Task "cancel me" running: False
403+
Task cant60 no. 0 name "complete" ended.
404+
Task "complete" running: False
405+
Task "cancel me" running: False
406+
Task "complete" running: False
407+
Task "cancel me" running: False
408+
Task cant60 no. 3 name "cancel wait" running.
409+
Cancelling task "cancel wait". 1.5 secs latency.
410+
Task "complete" running: False
411+
Task "cancel me" running: False
412+
Task "complete" running: False
413+
Task "cancel me" running: False
414+
Task cant60 no. 3 name "cancel wait" was cancelled.
415+
Was cancelled in 1503 ms
416+
Cancelling cant61
417+
Task cant61 cancelled.
418+
Done
419+
420+
421+
[Duration of cancel wait may vary depending on platform 1500 <= range <= 1600ms]
422+
''', 14)
423+
loop = asyncio.get_event_loop()
424+
loop.run_until_complete(run_cancel_test6(loop))

0 commit comments

Comments
 (0)