Last active
May 10, 2024 18:14
-
-
Save fernandospr/f46b2634ecdfebf79b979274c995ac94 to your computer and use it in GitHub Desktop.
Testing RX operations with delay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun countdownTimer( | |
duration: Long, | |
scheduler: Scheduler = Schedulers.io() | |
) = Observable.interval(1, TimeUnit.SECONDS, scheduler) | |
.takeWhile { it < duration } | |
.map { duration - it } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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