emmano
04/19/2020, 9:05 PMTestCoroutineScope
and TestCoroutineDispatcher
to control delays, how can the same kind of test be written for KN? Namely, it seems that runBlockingTest{}
is a JVM only construct. Probably because it uses DelayController
which is not available in KN (or so it seems). Here is the test class. The first test passes in both platforms, The second one only passes in JVM land. Any help will be appreciated.Dispatcher
not having an EventLoop
since at soon as the code reaches a delay()
I get the "There is no event loop. Use runBlocking { ... } to start one" message. That makes sense because it is a custom dispatcher that executes things as they come. Using this custom dispatcher allowed me to not use runBlockingTest
in JVM side of things.runBlockingTest()
is still JVM onlyKris Wong
04/22/2020, 12:49 PMemmano
04/22/2020, 1:30 PMrunBlockingTest()
equivalent for Kotlin Native. I am new to KMP, I am sorry if I am missing something.Kris Wong
04/22/2020, 1:31 PMexpect runBlocking
emmano
04/22/2020, 1:32 PMactual
implementation for both platforms, meaning I am not using rubBlockingTest{}
on the JVMKris Wong
04/22/2020, 1:53 PMemmano
04/22/2020, 2:33 PMactual fun <T> test(block: suspend (CoroutineScope) -> T) {
runBlocking(SynchronousDispatcher()) {
block(this)
}
}
class SynchronousDispatcher : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
block.run()
}
}
runBlockingTest()
on the JVM sideactual
Flows
delay()
on the KN sideKris Wong
04/22/2020, 2:38 PMinternal actual fun <T> runBlocking(
context: CoroutineContext,
block: suspend CoroutineScope.() -> T
)T = kotlinx.coroutines.runBlocking(context, block)
emmano
04/22/2020, 3:44 PMCoroutineContext
be for the Kotlin Native side?Dispatcher
should I use?runBlocking()
do so.Kris Wong
04/22/2020, 3:50 PMemmano
04/22/2020, 3:53 PMDispatcher
you mean Dispatchers.Main
? What I mean is, on the actual test I need to specify a CoroutineContext
with your version of runBlocking()
, what should that be?Kris Wong
04/22/2020, 3:55 PMemmano
04/22/2020, 4:02 PMcontext
will be provided by the framework? Meaning by declaring the expect
runBlocking()
with context
as a parameter, context
will be somehow be passed in to each individual platform?Kris Wong
04/22/2020, 4:20 PMinternal expect fun <T> runBlocking(
context: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> T
)T
emmano
04/22/2020, 4:33 PMDispatcher
does not wait for all coroutines to complete. Thanks for the help though, I appreciate it.Kris Wong
04/22/2020, 5:01 PMemmano
04/24/2020, 1:55 PMFlow
so I can later assert them. Similar to how .test()
works in Rx. The implementation I have works in the JVM, but not in KN.