Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bug that cache doesn't unsubscribe the source Observable when th... #2238

Merged
merged 1 commit into from
Jan 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/rx/internal/operators/OnSubscribeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ public OnSubscribeCache(Observable<? extends T> source, int capacity) {
@Override
public void call(Subscriber<? super T> s) {
if (SRC_SUBSCRIBED_UPDATER.compareAndSet(this, 0, 1)) {
source.unsafeSubscribe(Subscribers.from(cache));
source.subscribe(cache);
/*
* Note that we will never unsubscribe from 'source' as we want to receive and cache all of its values.
* Note that we will never unsubscribe from 'source' unless we receive `onCompleted` or `onError`,
* as we want to receive and cache all of its values.
*
* This means this should never be used on an infinite or very large sequence, similar to toList().
*/
Expand Down
15 changes: 14 additions & 1 deletion src/test/java/rx/internal/operators/OnSubscribeCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
Expand All @@ -27,10 +30,10 @@

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.internal.operators.OnSubscribeCache;
import rx.observers.TestSubscriber;
import rx.schedulers.Schedulers;
import rx.subjects.AsyncSubject;
Expand Down Expand Up @@ -148,4 +151,14 @@ public void testWithPublishSubjectAndRepeat() {
public void testWithReplaySubjectAndRepeat() {
testWithCustomSubjectAndRepeat(ReplaySubject.<Integer> create(), 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3);
}

@Test
public void testUnsubscribeSource() {
Action0 unsubscribe = mock(Action0.class);
Observable<Integer> o = Observable.just(1).doOnUnsubscribe(unsubscribe).cache();
o.subscribe();
o.subscribe();
o.subscribe();
verify(unsubscribe, times(1)).call();
}
}