refCount在这种用例中表现异常。就像是因为有一个初始订阅,它在第二个订阅中不再发出。
为什么第二个订阅不发出refCount()?
// Why does this test fail?
@Test
fun `refCount - on second subscription - emits value`() {
val subject = BehaviorSubject.create<Int>()
val observable = subject
.doOnNext { System.out.println("This emits for second subscriber") }
.replay(1)
.refCount()
.doOnNext { System.out.println("This does NOT emit for second subscriber") }
// This line causes the test to fail.
observable.takeUntil(Observable.just(Unit)).test()
subject.onNext(2)
val subscriber = observable.take(1).test()
val finished = subscriber.awaitTerminalEvent(2, TimeUnit.SECONDS)
assertTrue(finished)
}