Search code examples
kotlinrx-javareactive-programmingrx-java2

Refcount Observable does not emit on second subscription


refCount is behaving unexpectedly in this use case. It's like because there was an initial subscription it does not emit again on the second subscription.

Why doesn't refCount() emit for the second subscription?

// 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)
}

Solution

  • As @akarnokd mentioned, this was a bug and fixed in RxJava2 v2.2.12 Link