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

Casey Brooks

12/20/2019, 6:57 PM
Is it possible to run a suspending test in Kotlin/Native for iOS? I’m trying to test a Ktor client. I get immutability exceptions when doing
runBlocking
, so I tried to set up a polling solution with
.launch { }
and
usleep()
, but it doesn’t seem like the launched block ever executes.
Polling test code
Copy code
println("before launch")
PlatformApiUtils.mainCoroutineScope.launch {
    println("before block")
    block()
    println("after block")
    isDoneAtomic.lazySet(true)
}
println("after launch")

println("before polling")
while(true) {
    ... poll for completion of '.launch { }'
}
Dispatcher
Copy code
class UI : CoroutineDispatcher() {
    override fun dispatch(context: CoroutineContext, block: Runnable) {
        val queue = dispatch_get_main_queue()
        dispatch_async(queue) {
            block.run()
        }
    }
}
Output
Copy code
before launch
after launch
before polling // I'd expect logging from inside the block after this line
polling timeout reached
The dispatcher works fine on the iOS simulator, it just doesn’t seem to start in the tests
2 Views