I have the code above to wrap my tests in so they ...
# coroutines
l
I have the code above to wrap my tests in so they timeout. But some how test is not timing out, they keep running for ever. Anyone know why this wrapper may fail?
o
ref docs:
The code that is executing inside the block is cancelled on timeout and the active or next invocation of the cancellable suspending function inside the block throws a TimeoutCancellationException.
so if you do not check for cancellation in the block, it will never time out
l
@octylFractal now sure I understand, can you show an re-write that will work everytime?
o
no, there is no such thing with coroutines. cancellation is cooperative. if
block
never checks for cancellation, it cannot be stopped pre-emptively
if you were to call
yield()
,
channel.send()
, or many other cancellable functions in
block
, it should work if the coroutine eventually hits one of them
l
there has to be a solution. Maybe using a launch or some way to run a block that will guarantee stopping after a certain timeout
o
do note that this won't stop the block from running, it will only ensure that the current code can continue to execute. additionally, you probably want to put the count down in a
try-finally