Skip to content

Instantly share code, notes, and snippets.

@fernandospr
Last active May 10, 2024 18:14
Show Gist options
  • Save fernandospr/f46b2634ecdfebf79b979274c995ac94 to your computer and use it in GitHub Desktop.
Save fernandospr/f46b2634ecdfebf79b979274c995ac94 to your computer and use it in GitHub Desktop.
Testing RX operations with delay
fun countdownTimer(
duration: Long,
scheduler: Scheduler = Schedulers.io()
) = Observable.interval(1, TimeUnit.SECONDS, scheduler)
.takeWhile { it < duration }
.map { duration - it }
class CountdownTimerTest {
@Test
fun test() {
val testScheduler = TestScheduler()
val duration = 10L
val testObserver = countdownTimer(duration, testScheduler).test()
testScheduler.advanceTimeBy(duration + 1, TimeUnit.SECONDS)
testObserver
.assertValues(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
.assertComplete()
}
}
fun main() {
val duration = 10L
val disposable = countdownTimer(duration)
.subscribe(
{
println("$it")
},
{
println("[ERROR] $it")
}
)
Thread.sleep(TimeUnit.SECONDS.toMillis(duration + 1))
disposable.dispose()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment