https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

Luis Munoz

11/04/2019, 8:04 PM
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

octylFractal

11/04/2019, 8:06 PM
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

Luis Munoz

11/04/2019, 8:57 PM
@octylFractal now sure I understand, can you show an re-write that will work everytime?
o

octylFractal

11/04/2019, 9:33 PM
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

Luis Munoz

11/05/2019, 4:11 PM
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

octylFractal

11/05/2019, 9:36 PM
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
5 Views