Skip to content

3.x: Fix Flowable.concatMap backpressure w/ scalars #7089

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
Oct 5, 2020
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 @@ -13,7 +13,7 @@
package io.reactivex.rxjava3.internal.operators.flowable;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.*;

import org.reactivestreams.*;

Expand Down Expand Up @@ -308,7 +308,7 @@ void drain() {
continue;
} else {
active = true;
inner.setSubscription(new WeakScalarSubscription<>(vr, inner));
inner.setSubscription(new SimpleScalarSubscription<>(vr, inner));
}

} else {
Expand All @@ -325,20 +325,22 @@ void drain() {
}
}

static final class WeakScalarSubscription<T> implements Subscription {
static final class SimpleScalarSubscription<T>
extends AtomicBoolean
implements Subscription {
private static final long serialVersionUID = -7606889335172043256L;

final Subscriber<? super T> downstream;
final T value;
boolean once;

WeakScalarSubscription(T value, Subscriber<? super T> downstream) {
SimpleScalarSubscription(T value, Subscriber<? super T> downstream) {
this.value = value;
this.downstream = downstream;
}

@Override
public void request(long n) {
if (n > 0 && !once) {
once = true;
if (n > 0L && compareAndSet(false, true)) {
Subscriber<? super T> a = downstream;
a.onNext(value);
a.onComplete();
Expand Down Expand Up @@ -507,7 +509,7 @@ void drain() {
continue;
} else {
active = true;
inner.setSubscription(new WeakScalarSubscription<>(vr, inner));
inner.setSubscription(new SimpleScalarSubscription<>(vr, inner));
}
} else {
active = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public void run() {
continue;
} else {
active = true;
inner.setSubscription(new WeakScalarSubscription<>(vr, inner));
inner.setSubscription(new SimpleScalarSubscription<>(vr, inner));
}

} else {
Expand Down Expand Up @@ -528,7 +528,7 @@ public void run() {
continue;
} else {
active = true;
inner.setSubscription(new WeakScalarSubscription<>(vr, inner));
inner.setSubscription(new SimpleScalarSubscription<>(vr, inner));
}
} else {
active = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,56 @@ public Publisher<? extends Object> apply(String v)
.assertResult("RxSingleScheduler");
}

@Test
public void innerScalarRequestRace() {
Flowable<Integer> just = Flowable.just(1);
int n = 1000;
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
PublishProcessor<Flowable<Integer>> source = PublishProcessor.create();

TestSubscriber<Integer> ts = source
.concatMap(v -> v, n + 1, ImmediateThinScheduler.INSTANCE)
.test(1L);

TestHelper.race(() -> {
for (int j = 0; j < n; j++) {
source.onNext(just);
}
}, () -> {
for (int j = 0; j < n; j++) {
ts.request(1);
}
});

ts.assertValueCount(n);
}
}

@Test
public void innerScalarRequestRaceDelayError() {
Flowable<Integer> just = Flowable.just(1);
int n = 1000;
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
PublishProcessor<Flowable<Integer>> source = PublishProcessor.create();

TestSubscriber<Integer> ts = source
.concatMapDelayError(v -> v, true, n + 1, ImmediateThinScheduler.INSTANCE)
.test(1L);

TestHelper.race(() -> {
for (int j = 0; j < n; j++) {
source.onNext(just);
}
}, () -> {
for (int j = 0; j < n; j++) {
ts.request(1);
}
});

ts.assertValueCount(n);
}
}

@Test
public void boundaryFusionDelayError() {
Flowable.range(1, 10000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.exceptions.*;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.operators.flowable.FlowableConcatMap.WeakScalarSubscription;
import io.reactivex.rxjava3.processors.UnicastProcessor;
import io.reactivex.rxjava3.internal.operators.flowable.FlowableConcatMap.SimpleScalarSubscription;
import io.reactivex.rxjava3.processors.*;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.subscribers.TestSubscriber;
import io.reactivex.rxjava3.testsupport.TestHelper;

public class FlowableConcatMapTest extends RxJavaTest {

@Test
public void weakSubscriptionRequest() {
public void simpleSubscriptionRequest() {
TestSubscriber<Integer> ts = new TestSubscriber<>(0);
WeakScalarSubscription<Integer> ws = new WeakScalarSubscription<>(1, ts);
SimpleScalarSubscription<Integer> ws = new SimpleScalarSubscription<>(1, ts);
ts.onSubscribe(ws);

ws.request(0);
Expand Down Expand Up @@ -79,6 +79,56 @@ public Publisher<? extends Object> apply(String v)
.assertResult("RxSingleScheduler");
}

@Test
public void innerScalarRequestRace() {
Flowable<Integer> just = Flowable.just(1);
int n = 1000;
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
PublishProcessor<Flowable<Integer>> source = PublishProcessor.create();

TestSubscriber<Integer> ts = source
.concatMap(v -> v, n + 1)
.test(1L);

TestHelper.race(() -> {
for (int j = 0; j < n; j++) {
source.onNext(just);
}
}, () -> {
for (int j = 0; j < n; j++) {
ts.request(1);
}
});

ts.assertValueCount(n);
}
}

@Test
public void innerScalarRequestRaceDelayError() {
Flowable<Integer> just = Flowable.just(1);
int n = 1000;
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
PublishProcessor<Flowable<Integer>> source = PublishProcessor.create();

TestSubscriber<Integer> ts = source
.concatMapDelayError(v -> v, true, n + 1)
.test(1L);

TestHelper.race(() -> {
for (int j = 0; j < n; j++) {
source.onNext(just);
}
}, () -> {
for (int j = 0; j < n; j++) {
ts.request(1);
}
});

ts.assertValueCount(n);
}
}

@Test
public void boundaryFusionDelayError() {
Flowable.range(1, 10000)
Expand Down