I am using `runBlocking(someContext)`, and within ...
# coroutines
k
I am using
runBlocking(someContext)
, and within the block a call is made to a method that is calling
launch
using a scope that was created with the same context. however, it is not blocking. am I doing something wrong?
o
could you share the code in full, or at least condensed? that sounds correct but maybe there's a miscommunication
s
Structured Concurrency only works within one scope. If a
launch
is launched with a different scope, it will not participate in the structured concurrency of the calling scope. It looks like sharing a CoroutineContext does not circumvent this...
k
Copy code
runBlocking(anObject.coroutineContext) {
    anObject.doSomething()
}
and
Copy code
fun doSomething() {
    coroutineScope.launch {
        ...
    }
}
finally
Copy code
internal val coroutineContext = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
private val coroutineScope = CoroutineScope(coroutineContext + SupervisorJob())
a scope just contains a context, so my understanding was that this is the way to accomplish what I am after
o
no, this will not work because
doSomething
is not using a child job of
runBlocking
you should pass down the scope given in the
runBlocking
lambda, and use that to launch the appropriate job, OR if you mean to make it separate, return the Job from
doSomething()
and
join()
on it in
runBlocking
k
hmm
i am just trying to test something that is otherwise fully encapsulated, in the least invasive way
testing classes with no output always ends up being a pain =/
alright, at least I understand the problem, thanks!