Skip to content

2.x: fix Observable.combineLatest to dispose eagerly #5114

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

Merged
merged 1 commit into from
Feb 18, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ public void subscribe(ObservableSource<? extends T>[] sources) {
public void dispose() {
if (!cancelled) {
cancelled = true;

cancelSources();
if (getAndIncrement() == 0) {
cancel(queue);
clear(queue);
}
}
}
Expand All @@ -138,6 +138,10 @@ public boolean isDisposed() {

void cancel(SpscLinkedArrayQueue<?> q) {
clear(q);
cancelSources();
}

void cancelSources() {
for (CombinerObserver<T, R> s : observers) {
s.dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1517,4 +1517,38 @@ public void run() throws Exception {
RxJavaPlugins.reset();
}
}

@Test
public void eagerDispose() {
final PublishProcessor<Integer> pp1 = PublishProcessor.create();
final PublishProcessor<Integer> pp2 = PublishProcessor.create();

TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {
@Override
public void onNext(Integer t) {
super.onNext(t);
cancel();
if (pp1.hasSubscribers()) {
onError(new IllegalStateException("pp1 not disposed"));
} else
if (pp2.hasSubscribers()) {
onError(new IllegalStateException("pp2 not disposed"));
} else {
onComplete();
}
}
};

Flowable.combineLatest(pp1, pp2, new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer t1, Integer t2) throws Exception {
return t1 + t2;
}
})
.subscribe(ts);

pp1.onNext(1);
pp2.onNext(2);
ts.assertResult(3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1165,4 +1165,37 @@ public void run() throws Exception {
}
}

@Test
public void eagerDispose() {
final PublishSubject<Integer> ps1 = PublishSubject.create();
final PublishSubject<Integer> ps2 = PublishSubject.create();

TestObserver<Integer> ts = new TestObserver<Integer>() {
@Override
public void onNext(Integer t) {
super.onNext(t);
cancel();
if (ps1.hasObservers()) {
onError(new IllegalStateException("ps1 not disposed"));
} else
if (ps2.hasObservers()) {
onError(new IllegalStateException("ps2 not disposed"));
} else {
onComplete();
}
}
};

Observable.combineLatest(ps1, ps2, new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer t1, Integer t2) throws Exception {
return t1 + t2;
}
})
.subscribe(ts);

ps1.onNext(1);
ps2.onNext(2);
ts.assertResult(3);
}
}