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

knthmn

04/07/2021, 3:31 AM
I want to unit test an interface like this
Copy code
interface Foo {
    fun flowValue(): Flow<Value>  // flow the stored value
    suspend fun setValue(value: Value)  // update the stored value
}
however the implementation is not for me to control, and sometimes
flowValue()
sometimes emit the old value several times before emitting the set value. What is the best way to test this?
My current solution is just to do
Copy code
setValue(bar)
flowValue().first { it == bar }
but it only deadlocks instead of failing when the implementation is wrong. The implementation contains Java threading too.
l

louiscad

04/07/2021, 7:56 AM
Well, either there's bugs in the interface implementation that wraps what you don't control, or that thing you don't control isn't reliable. Or you might you use wrong API wrapper 🤔
k

knthmn

04/07/2021, 9:54 AM
It is just implemented asynchronously (e.g. a video player). The value I want will eventually come and it is ok since I only care about the most updated status. I just want a better way to test it.
l

louiscad

04/07/2021, 9:55 AM
The library turbine from Square might come handy
u

uli

04/07/2021, 4:37 PM
I would say it does not deadlock. It just waits for ever because it can not know if any better values will follow. So you could wrap it in a
timeout {}
👍🏼 1
l

louiscad

04/07/2021, 5:52 PM
You mean
withTimeout { … }
?
👍🏼 1
👍 1
2 Views