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 AMabbic
05/21/2023, 9:20 AMabbic
05/21/2023, 9:21 AMfun <T: Any> Flow<T>.ifDelayed(timeMillis: Long, block: () -> Unit): Flow<T> = channelFlow {
val delayJob = launch {
delay(timeMillis)
block()
}
collect {
delayJob.cancelAndJoin()
send(it)
}
}
abbic
05/21/2023, 9:22 AMCLOVIS
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 AMabbic
05/21/2023, 1:36 PMCLOVIS
05/21/2023, 2:05 PMCLOVIS
05/21/2023, 2:06 PMKirill Zhukov
09/23/2023, 4:27 AMYou don’t really care about the control of time at allI think in many cases you actually do! So, I would agree that
advanceTimeBy
doesn’t appear to be very helpful in situations like this. I’ve noticed a common sentiment among developers, including myself, who are confused about what advanceTimeBy
is supposed to do and how to use it.Kirill Zhukov
09/23/2023, 4:28 AM