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.emmano
04/19/2020, 9:06 PMDispatcher
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.emmano
04/22/2020, 12:44 PMrunBlockingTest()
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 PMemmano
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)
}
}
emmano
04/22/2020, 2:34 PMclass SynchronousDispatcher : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
block.run()
}
}
emmano
04/22/2020, 2:35 PMrunBlockingTest()
on the JVM sideemmano
04/22/2020, 2:35 PMactual
emmano
04/22/2020, 2:35 PMFlows
emmano
04/22/2020, 2:36 PMdelay()
on the KN sideemmano
04/22/2020, 2:37 PMKris Wong
04/22/2020, 2:38 PMinternal actual fun <T> runBlocking(
context: CoroutineContext,
block: suspend CoroutineScope.() -> T
)T = kotlinx.coroutines.runBlocking(context, block)
Kris Wong
04/22/2020, 2:40 PMemmano
04/22/2020, 3:44 PMCoroutineContext
be for the Kotlin Native side?emmano
04/22/2020, 3:45 PMDispatcher
should I use?emmano
04/22/2020, 3:48 PMrunBlocking()
do so.Kris Wong
04/22/2020, 3:50 PMKris 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?emmano
04/22/2020, 4:02 PMemmano
04/22/2020, 4:10 PMKris 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.