Home Unit testing Rxjava observables that have a delay
Reply: 2

Unit testing Rxjava observables that have a delay

triad
1#
triad Published in 2017-11-14 23:28:45Z

I want to be able to unit test an Observable that has a delayed emission, but without actually waiting for the delay time. Is there a way to do this?

I'm currently using a CountDownHatch to delay the assert, and that works fine, but increases the test run time.

Example:

val myObservable: PublishSubject<Boolean> = PublishSubject.create<Boolean>()
fun myObservable(): Observable<Boolean> = myObservable.delay(3, TimeUnit.SECONDS)

@Test
fun testMyObservable() {
    val testObservable = myObservable().test()
    myObservable.onNext(true)

    // val lock = CountDownLatch(1)
    // lock.await(3100, TimeUnit.MILLISECONDS)
    testObservable.assertValue(true)
}
Aaron He
2#
Aaron He Reply to 2017-11-15 06:57:19Z

TestScheduler is perfect for this. It has a handy method advanceTimeBy(long, TimeUnit) allows you to control the timing. And Observable.delay has an overload method takes a Scheduler.

So just use the default Scheduler.computation() in the myObservable function, and use TestScheduler for unit testing.

triad
3#
triad Reply to 2017-11-15 18:11:06Z

I was able to come up with a complete solution thanks to @aaron-he's answer.

It uses a combination of TestScheduler to advance the time and also RxJavaPlugins to override the default scheduler with the testScheduler. This allowed testing the myObservable() function without modification or needing to pass in a Scheduler.

@Before
fun setUp() = RxJavaPlugins.reset()

@After
fun tearDown() = RxJavaPlugins.reset()

@Test
fun testMyObservable() {
    val testScheduler = TestScheduler()
    RxJavaPlugins.setComputationSchedulerHandler { testScheduler }

    val testObservable = myObservable().test()
    myObservable.onNext(true)

    testScheduler.advanceTimeBy(2, TimeUnit.SECONDS)
    testObservable.assertEmpty()
    testScheduler.advanceTimeBy(2, TimeUnit.SECONDS)
    testObservable.assertValue(true)
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301147 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO