abbic
05/21/2023, 8:48 AMrunTest
and its making testing the timeout impossible.
am i missing something? im trying to use advanceTimeBy
to maneuver around this delay for tests but it seems useless as the delays are just skippedCLOVIS
05/21/2023, 9:17 AMabbic
05/21/2023, 9:19 AMfun <T: Any> Flow<T>.ifDelayed(timeMillis: Long, block: () -> Unit): Flow<T> = channelFlow {
val delayJob = launch {
delay(timeMillis)
block()
}
collect {
delayJob.cancelAndJoin()
send(it)
}
}
CLOVIS
05/21/2023, 9:49 AM@Test
fun lambdaIsNotCalledIfTimeoutDidntEnd() = runTest {
var called = false
flow {
delay(100)
emit(Unit)
}.ifDelayed(1000) {
called = true
}.collect()
assertFalse(called)
}
@Test
fun lambdaIsCalledIfTimeoutEnds() = runTest {
var called = false
flow {
delay(5000)
emit(Unit)
}.ifDelayed(1000) {
called = true
}.collect()
assertTrue(called)
}
abbic
05/21/2023, 10:52 AMCLOVIS
05/21/2023, 2:05 PM