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

2.x: Fix switchMap incorrect sync-fusion & error management #6618

Merged
merged 1 commit into from
Aug 14, 2019
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 @@ -314,7 +314,7 @@ void drain() {
if (r != Long.MAX_VALUE) {
requested.addAndGet(-e);
}
inner.get().request(e);
inner.request(e);
}
}

Expand Down Expand Up @@ -398,6 +398,7 @@ public void onError(Throwable t) {
if (index == p.unique && p.error.addThrowable(t)) {
if (!p.delayErrors) {
p.upstream.cancel();
p.done = true;
}
done = true;
p.drain();
Expand All @@ -418,5 +419,11 @@ public void onComplete() {
public void cancel() {
SubscriptionHelper.cancel(this);
}

public void request(long n) {
if (fusionMode != QueueSubscription.SYNC) {
get().request(n);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ void innerError(SwitchMapInnerObserver<T, R> inner, Throwable ex) {
if (inner.index == unique && errors.addThrowable(ex)) {
if (!delayErrors) {
upstream.dispose();
done = true;
}
inner.done = true;
drain();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1201,4 +1201,32 @@ public Object apply(Integer w) throws Exception {
.assertNoErrors()
.assertComplete();
}

@Test
public void switchMapFusedIterable() {
Flowable.range(1, 2)
.switchMap(new Function<Integer, Publisher<Integer>>() {
@Override
public Publisher<Integer> apply(Integer v)
throws Exception {
return Flowable.fromIterable(Arrays.asList(v * 10));
}
})
.test()
.assertResult(10, 20);
}

@Test
public void switchMapHiddenIterable() {
Flowable.range(1, 2)
.switchMap(new Function<Integer, Publisher<Integer>>() {
@Override
public Publisher<Integer> apply(Integer v)
throws Exception {
return Flowable.fromIterable(Arrays.asList(v * 10)).hide();
}
})
.test()
.assertResult(10, 20);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
package io.reactivex.internal.operators.observable;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.*;

import org.junit.*;
import org.mockito.InOrder;

import io.reactivex.*;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.*;
import io.reactivex.exceptions.*;
import io.reactivex.functions.*;
Expand All @@ -33,7 +36,7 @@
import io.reactivex.observers.TestObserver;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.*;
import io.reactivex.subjects.*;
import io.reactivex.subjects.PublishSubject;

public class ObservableSwitchTest {

Expand Down Expand Up @@ -1191,4 +1194,32 @@ public Object apply(Integer w) throws Exception {
.assertNoErrors()
.assertComplete();
}

@Test
public void switchMapFusedIterable() {
Observable.range(1, 2)
.switchMap(new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer v)
throws Exception {
return Observable.fromIterable(Arrays.asList(v * 10));
}
})
.test()
.assertResult(10, 20);
}

@Test
public void switchMapHiddenIterable() {
Observable.range(1, 2)
.switchMap(new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer v)
throws Exception {
return Observable.fromIterable(Arrays.asList(v * 10)).hide();
}
})
.test()
.assertResult(10, 20);
}
}