https://kotlinlang.org logo
#getting-started
Title
# getting-started
d

Daniele Segato

01/22/2021, 10:13 AM
Is there some documentation on how to inspect a
CoroutineScope
/
CoroutineContext
? I would I unit test this function?
Copy code
fun createCoroutineScope(): CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
So in my test I have to check if the returned
CoroutineScope
is using a
SupervisedJob
or a particular
Dispatcher
how do I do that?? (or any other property of the context/scope)
d

diesieben07

01/22/2021, 10:18 AM
A
CoroutineContext
is basically a type-safe map, the keys are
CoroutineContext.Key
. In your example
scope.coroutineContext[Job]
would give you the
SupervisorJob
(because
Job
has a
companion object
which is the
CoroutineContext.Key
);
scope.coroutineContext[CoroutineDispatcher]
will give you
Dispatchers.Default
in the same way.
d

Daniele Segato

01/22/2021, 10:19 AM
thanks @diesieben07 where do I find a list of keys? I tried to make Intellij tell me but failed
d

diesieben07

01/22/2021, 10:20 AM
"Show type hierarchy" (
Ctrl-H
) on
CoroutineContext.Key
does it for me. I can't find that in the menus though, I always just use the shortcut 😄
d

Daniele Segato

01/22/2021, 12:00 PM
oh, for some reason I didn't though of using CoroutineContext.Key hierarchy.... btw you can find it if you do
Ctrl+Shift+A
and look for type hierarchy
hum by the way I can get the job with
scope.coroutineContext[Job]
but than I do not know how to check it's a SupervisorJob. it's not a class, it's a top level function and the implementation is private
2 Views