I want to unit test an interface like this ```inte...
# coroutines
k
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
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
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
The library turbine from Square might come handy
u
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
You mean
withTimeout { … }
?
👍🏼 1
👍 1