This article is crude.
I had set trampoline
in RxJavaPlugins.set *** SchedulerHandler
, but only setIoSchedulerHandler
.
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
I was writing a Test Rule like this,
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
RxJavaPlugins.setComputationSchedulerHandler { Schedulers.trampoline() }
...
If the code under test has a buffer
operator, the buffer operator internally uses the computation
scheduler, so overwriting the trampoline
scheduler will keep waiting indefinitely. The test never ends.
@Test
fun testBuffer() {
val subject = PublishSubject.create<Int>()
val testObserver = subject
.buffer(1, TimeUnit.SECONDS)
.filter { it.isNotEmpty() }
.test()
subject.onNext(1)
subject.onNext(2)
testObserver.await(2, TimeUnit.SECONDS)
testObserver.assertValues(listOf(1, 2))
}
Recommended Posts