For testing purposes, I need a flow that never com...
# coroutines
t
For testing purposes, I need a flow that never completes (similar to
Flowable.never()
) Is something like this correct ?
Copy code
object NeverFlow : Flow<Nothing> {
    override suspend fun collect(collector: FlowCollector<Nothing>) {
        delay(Long.MAX_VALUE)
    }
}
s
Copy code
val flow: Flow<Nothing> = flow {
        suspendCancellableCoroutine { /* do nothing */ }
    }
👍 1
Basically, replace the
delay(Long.MAX_VALUE)
call with an empty
suspendCancellableCoroutine
call
e
Actually, that is exactly what delay would do if you call it with
Long.MAX_VALUE
— it assumes you suspend forever.
s
Had some issues with unit-testing code that uses
delay(Long.MAX_VALUE)
, due to Long.MAX_VALUE rolling over to a negative value of you add just 1 millisecond. Maybe that has been fixed in the latest coroutines-test libraries. If not, calling
delay(Long.MAX_VALUE - 1)
may still have that roll-over issue...