https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

tseisel

04/04/2019, 9:31 AM
How to unit test that a
suspend fun
suspends until a certain condition is met ? I tried the following :
Copy code
runBlocking() {
    val testSource = BehaviorProcessor.create<String>()
    val subjectUnderTest = MyCache(coroutineScope = this, source = testSource)

    val loading = async(start = CoroutineStart.UNDISPATCHED) { subjectUnderTest.getCachedValue() } // Suspend function call
    assertFalse(loading.isCompleted)
    
    // Sending an element to that processor satisfies the condition
    testSource.onNext("Foo")
    assertTrue(loading.isCompleted) // This fails : isCompleted is false
}
g

gildor

04/04/2019, 9:36 AM
why do you need async? Why not just call function and check result?
t

tseisel

04/04/2019, 9:44 AM
Maybe it makes no sense to test that scenario, but I want to check that the suspending function resumes when an element is sent to the
BehaviorProcessor
it depends on. The only way I thought of was using async, as calling the suspending function may block the test indefinitely, so I wouldn't be able to satisfy the condition.
8 Views