我偶然发现了一个奇怪的 testScheduler 行为,我无法理解。下面的代码被大大简化了,但它源于现实生活中的问题。
考虑这个测试:
@Test
fun testSchedulerFun(){
val testScheduler = TestScheduler()
val stringsProcessor = PublishProcessor.create<String>()
val completable = Completable.complete()
completable
.doOnComplete { stringsProcessor.onNext("onComplete") }
.subscribeOn(testScheduler)
.subscribe()
val testSubscriber = stringsProcessor
.subscribeOn(testScheduler) //this line of code messes the test
.test()
testScheduler.triggerActions()
testSubscriber
.assertValues("onComplete")
}
**当我订阅 test stringsProcessor
ontestScheduler
时,测试失败。当我删除该行时,它成功了。**
我看到的事件流程是:
- 触发动作
- 正在订阅 completable 和 stringsProcessor 并将它们的事件传播到下游。
- 显然,在 testSubscriber 完成后
stringsProcessor.onNext("onComplete")
评估。
我想知道为什么