https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
b

basher

02/26/2019, 4:39 PM
I think it is in common. I'm currently running tests using it
I have fun into IDE issues before getting it to be recognized
b

basher

02/26/2019, 4:40 PM
Huh. I guess we only build for JVM and native, which explains why that works
Sorry!
p

pardom

02/26/2019, 4:46 PM
Using an instance of
CoroutineDispatcher
local to the test class did the trick: https://gist.github.com/pardom/74a9f1abc17688badf6b8dc06e13b1d2#file-runtimetest-kt-L13-L17
n

nestserau

02/26/2019, 4:50 PM
You can reimplement runBlocking: common
Copy code
expect fun <T> runBlocking(block: suspend () -> T): T
ios
Copy code
actual fun <T> runBlocking(block: suspend () -> T): T {
    val expectation = Expectation<T>()

    GlobalScope.launch(MainRunLoopDispatcher) {
        expectation.fulfill(block.invoke())
    }

    return expectation.wait() ?: throw RuntimeException("runBlocking failed")
}

private object MainRunLoopDispatcher : CoroutineDispatcher() {
    override fun dispatch(context: CoroutineContext, block: Runnable) {
        NSRunLoop.mainRunLoop().performBlock {
            block.run()
        }
    }
}
android
Copy code
actual fun <T> runBlocking(block: suspend () -> T): T = kotlinx.coroutines.runBlocking { block.invoke() }
I’m not the original author, but I don’t remember the original author unfortunately. Hope that helps.
j

jw

02/26/2019, 4:50 PM
the common pattern is to expect/actual it
r

russhwolf

02/26/2019, 4:50 PM
If you don't need js, you can add your own expect/actual declarations to get runblocking in jvm/native.
j

jw

02/26/2019, 4:50 PM
even in JS is still works because you can return a promise from the test APIs
and the runner will wait
this will all be obviated once
@Test suspend fun
lands though
r

russhwolf

02/26/2019, 4:52 PM
Yeah the ticket for
@Test suspend
has more details on workarounds: https://youtrack.jetbrains.com/issue/KT-22228
j

jw

02/26/2019, 4:54 PM
1.3.30 🤞
🤞 5
👌 3