Unit testing Rxjava observables that have a delay
Asked Answered
S

3

17

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)
}
Sangfroid answered 14/11, 2017 at 23:28 Comment(0)
S
46

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)
}
Sangfroid answered 15/11, 2017 at 18:11 Comment(0)
A
10

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.

Accession answered 15/11, 2017 at 6:57 Comment(1)
Just googled this issue and got here. Probably should have just asked it in Slack. lolProlongation
D
1

Found some strange behaviour of PublishSubject it need some time for subscribe, for example the test is failed:

private val scheduler = TestScheduler()
    @Test
    fun publishSubjectFailedTest() {
        val callback: DelayCallback = mock()

        val myPublishSubject: PublishSubject<Boolean> = PublishSubject.create()
        myPublishSubject
            .delay(10, TimeUnit.SECONDS, scheduler)
            .subscribeOn(scheduler)
            .observeOn(scheduler)
            .subscribe(
                Consumer<Boolean> {
                    callback.onCalldown()
                },
                Consumer<Throwable> {

                },
                Action {

                }
            )
        myPublishSubject.onNext(true)
        scheduler.advanceTimeBy(20, TimeUnit.SECONDS)
        verify(callback, times(1)).onCalldown()
    }

But if we add a time of scheduler before call onNext, for example scheduler.advanceTimeBy(1, TimeUnit.NANOSECONDS) then the test will be success:

 @Test
    fun publishSubjectSuccessTest() {
        val callback: DelayCallback = mock()

        val myPublishSubject: PublishSubject<Boolean> = PublishSubject.create()
        myPublishSubject
            .delay(10, TimeUnit.SECONDS, scheduler)
            .subscribeOn(scheduler)
            .observeOn(scheduler)
            .subscribe(
                Consumer<Boolean> {
                    callback.onCalldown()
                },
                Consumer<Throwable> {

                },
                Action {

                }
            )
        scheduler.advanceTimeBy(1, TimeUnit.NANOSECONDS)//added time of scheduler
        myPublishSubject.onNext(true)
        scheduler.advanceTimeBy(20, TimeUnit.SECONDS)
        verify(callback, times(1)).onCalldown()
    }
Diadiabase answered 20/9, 2019 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.