Hey guys, I’m trying to understand `Job`’s `cancel...
# coroutines
r
Hey guys, I’m trying to understand `Job`’s
cancel
method. I’m working on creating a class that implements
CoroutineScope
and I’m following the example here https://github.com/Kotlin/kotlinx.coroutines/blob/master/core/kotlinx-coroutines-core/test/guide/example-context-10.kt. I would expect that
job.cancel()
would return true in this case since the invocation of cancel is what cancels the job, however, when I print the result of
job.cancel()
I get false.
Copy code
fun destroy() {
     val temp = job.cancel()
     print(temp.toString()) //prints "false" even though I would expect it to print "true"
}
Am I misunderstanding cancel?
e
Documentation on cancel result is wrong. In fact
cancel
on
Job()
always returns
false
at them moment. It might change.
r
@elizarov Thanks for the clarification! Any thoughts on writing tests for coroutine cancelation scenarios?
e
We have tons of test, but they are all useless, since we don’t have any real use-cases for that boolean. It is just a left-over from earlier work. The proposal is to remove it: https://github.com/Kotlin/kotlinx.coroutines/issues/713
👍 1
d
Thanks @elizarov for the information! The only (current) use we had for the boolean was in unit testing the coroutine to ensure that it was cancelled properly, but we'll figure out another way.
👍 1