Closed
Description
I recently started noticing that cache
was not behaving as I expected. I might have misunderstood its semantics, but I thought cache
was equivalent to replay
, except it would only connect
to the underlying observable on the first subscription.
I wrote this simple test that validates the semantics that I was expecting, and it's not passing:
final PublishSubject<Integer> subject = PublishSubject.create();
final Observable<Integer> cachedObservable = subject.cache(1);
TestObserver<Integer> observer;
// Subscribe for the first time, without prior events
observer = new TestObserver<>();
cachedObservable.subscribe(observer);
observer.assertReceivedOnNext(ImmutableList.<Integer>of());
subject.onNext(1);
observer.assertReceivedOnNext(ImmutableList.of(1));
subject.onNext(2);
observer.assertReceivedOnNext(ImmutableList.of(1, 2));
// Resubscribe
observer = new TestObserver<>();
cachedObservable.subscribe(observer);
// Should receive the last value because capacity is 1.
observer.assertReceivedOnNext(ImmutableList.of(2));
Was my assumption about its behavior wrong, or does this expose a bug?
If I change subject.cache(1)
to subject.replay(1)
and I connect
the returned observable, the test passes.
Metadata
Metadata
Assignees
Labels
No labels