I stumbled upon a weird testScheduler behavior that I cannot wrap my head around. The code below is greatly simplified, but it origins in a real life issue.
Consider this test:
@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")
}
**When I subscribe the tested stringsProcessor on testScheduler, the test fails. When I remove that line it succeeds. **
The flow of events as I see it is:
- triggerActions
- completable and stringsProcessor are being subscribed and propagate their events downstream.
- And apparently the
stringsProcessor.onNext("onComplete")is evaluated after the testSubscriber has finished.
I want to know why