Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android JUnit test blocks indefinitely when Observable observed on AndroidSchedulers.mainThread()

I'm writing a simple test that is equivalent to:

Test fun testObservable() {
    val returnedObservable = Observable.create(object : Observable.OnSubscribe<String> {
        override fun call(t: Subscriber<in String>) {
            t.onNext("hello")
            t.onCompleted()
        }

    }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())

    val result = returnedObservable.toBlocking().first()
    assertEquals("hello", result)
}

The test blocks indefinitely on returnedObservable.toBlocking().first() when .observeOn(AndroidSchedulers.mainThread()) is present.

Is there a way to transform the observable to return the result?

The returnedObservable is returned from method call with .subscribeOn and .observeOn already applied so removing those is not an option.

like image 899
atok Avatar asked Dec 14 '25 06:12

atok


1 Answers

I guess it is a bug mentioned here: https://github.com/ReactiveX/RxAndroid/issues/50

btw why don't you use RxKotlin?

You example will look much better:

    val returnedObservable = observable<String> { subscriber ->
        subscriber.onNext("hello")
        subscriber.onCompleted()
    }
    .subscribeOn(Schedules.io())
    .observeOn(AndroidSchedulers.mainThread())
like image 147
Sergey Mashkov Avatar answered Dec 15 '25 20:12

Sergey Mashkov