Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
2.x: Fix Flowable.concatMap backpressure w/ scalars
  • Loading branch information
akarnokd committed Oct 5, 2020
commit 0b6716eca759eb6b351f99332ca7efa1f3b812c5
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package io.reactivex.internal.operators.flowable;

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

import org.reactivestreams.*;

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

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

static final class WeakScalarSubscription<T> implements Subscription {
static final class SimpleScalarSubscription<T>
extends AtomicBoolean
implements Subscription {
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 > 0 && compareAndSet(false, true)) {
Subscriber<? super T> a = downstream;
a.onNext(value);
a.onComplete();
Expand Down Expand Up @@ -538,7 +538,7 @@ void drain() {
continue;
} else {
active = true;
inner.setSubscription(new WeakScalarSubscription<R>(vr, inner));
inner.setSubscription(new SimpleScalarSubscription<R>(vr, inner));
}
} else {
active = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import io.reactivex.*;
import io.reactivex.exceptions.*;
import io.reactivex.functions.*;
import io.reactivex.internal.operators.flowable.FlowableConcatMap.WeakScalarSubscription;
import io.reactivex.internal.functions.Functions;
import io.reactivex.internal.operators.flowable.FlowableConcatMap.SimpleScalarSubscription;
import io.reactivex.processors.PublishProcessor;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.TestSubscriber;

Expand All @@ -33,7 +35,7 @@ public class FlowableConcatMapTest {
@Test
public void weakSubscriptionRequest() {
TestSubscriber<Integer> ts = new TestSubscriber<Integer>(0);
WeakScalarSubscription<Integer> ws = new WeakScalarSubscription<Integer>(1, ts);
SimpleScalarSubscription<Integer> ws = new SimpleScalarSubscription<Integer>(1, ts);
ts.onSubscribe(ws);

ws.request(0);
Expand Down Expand Up @@ -105,6 +107,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(Functions.<Flowable<Integer>>identity(), 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(Functions.<Flowable<Integer>>identity(), n + 1, true)
.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 pollThrows() {
Flowable.just(1)
Expand Down