]>
vault307.fbx.one Git - ir_remote.git/blob - primitives/tests/delay_test.py
1 # delay_test.py Tests for Delay_ms class
3 # Copyright (c) 2020 Peter Hinch
4 # Released under the MIT License (MIT) - see LICENSE file
6 import uasyncio
as asyncio
8 from primitives
.delay_ms
import Delay_ms
10 micropython
.alloc_emergency_exception_buf(100)
12 def printexp(exp
, runtime
=0):
13 print('Expected output:')
18 print('Running (runtime = {}s):'.format(runtime
))
20 print('Running (runtime < 1s):')
22 async def ctor_test(): # Constructor arg
34 d
= Delay_ms(cb
, ('callback',), duration
=5000)
36 print('Trigger 5 sec delay')
38 await asyncio
.sleep(4)
39 print('Retrigger 5 sec delay')
41 await asyncio
.sleep(4)
42 print('Callback should run')
43 await asyncio
.sleep(2)
46 async def launch_test():
49 Coroutine should run: run to completion.
52 Coroutine should run: test cancellation.
54 Coroutine should run: test awaiting.
62 await asyncio
.sleep_ms(ms
)
65 d
= Delay_ms(cb
, ('coroutine', 1000))
67 print('Trigger 5 sec delay')
68 d
.trigger(5000) # Test extending time
69 await asyncio
.sleep(4)
70 print('Coroutine should run: run to completion.')
71 await asyncio
.sleep(3)
72 d
= Delay_ms(cb
, ('coroutine', 3000))
74 await asyncio
.sleep(4)
75 print('Coroutine should run: test cancellation.')
76 await asyncio
.sleep(2)
80 await asyncio
.sleep(4)
81 print('Coroutine should run: test awaiting.')
82 await asyncio
.sleep(2)
88 async def reduce_test(): # Test reducing a running delay
101 d
= Delay_ms(cb
, ('callback',))
103 print('Trigger 5 sec delay')
104 d
.trigger(5000) # Test extending time
105 await asyncio
.sleep(4)
106 print('Callback should run')
107 await asyncio
.sleep(2)
109 await asyncio
.sleep(1)
111 await asyncio
.sleep(2)
112 print('Callback should run')
113 await asyncio
.sleep(2)
117 async def stop_test(): # Test the .stop and .running methods
124 Callback should not run
132 d
= Delay_ms(cb
, ('callback',))
134 print('Trigger 5 sec delay')
135 d
.trigger(5000) # Test extending time
136 await asyncio
.sleep(4)
139 print('Callback should run')
140 await asyncio
.sleep(2)
141 print('Callback returned', d
.rvalue())
143 await asyncio
.sleep(1)
145 await asyncio
.sleep(1)
148 print('Callback should not run')
149 await asyncio
.sleep(4)
153 async def isr_test(): # Test trigger from hard ISR
154 from pyb
import Timer
156 Timer holds off cb for 5 secs
165 d
= Delay_ms(cb
, ('callback',))
169 tim
= Timer(1, freq
=10, callback
=timer_cb
)
171 print('Timer holds off cb for 5 secs')
172 await asyncio
.sleep(5)
174 print('cb should now run')
175 await asyncio
.sleep(1)
178 async def err_test(): # Test triggering de-initialised timer
180 Running (runtime = 3s):
183 Success: error was raised.
191 d
= Delay_ms(cb
, ('callback',))
193 print('Trigger 1 sec delay')
195 await asyncio
.sleep(2)
200 print("Success: error was raised.")
204 Run a test by issuing
206 where n is a test number. Avaliable tests:
208 0 Test triggering from a hard ISR (Pyboard only)
209 1 Test the .stop method and callback return value.
210 2 Test reducing the duration of a running timer
211 3 Test delay defined by constructor arg
212 4 Test triggering a Task
213 5 Attempt to trigger de-initialised instance
218 tests
= (isr_test
, stop_test
, reduce_test
, ctor_test
, launch_test
, err_test
)
221 asyncio
.run(tests
[n
]())
223 asyncio
.new_event_loop()