I have a public function that conditionally calls ...
# coroutines
m
I have a public function that conditionally calls a private function which launches a coroutine that does file IO. In order to make it testable without
Thread.sleep
, I’ve done the following:
Copy code
class MyClass {
    fun pubFun(coroutineScope: CoroutineScope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>): Result {
        if (condition) privFun(coroutineScope)
    }
    private fun privFun(coroutineScope: CoroutineScope) = coroutineScope.launch {
        // Do file IO
    }
}

@Test
fun testPrivFun() {
    //setup
    val result = runBlocking { myClass.pubFun(this@runBlocking) }
}
I’ve found that this works, but I don’t totally grok what I’m doing. What if any antipatterns am I incorporating? Also, using
runBlocking
at the function level doesn’t create the same behavior (ie
fun testPrivFun() = runBlocking { ...
. Why is that? Note that the result of
pubFun
isn’t tied to the completion of
privFun
.
privFun
is a fire and forget call