Skip to content

3.x: onReduceBackpressure internals cleanup #7151

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 3 commits into from
Jan 19, 2021
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 @@ -49,19 +49,20 @@ static final class BackpressureReduceSubscriber<T> extends AbstractBackpressureT
@Override
public void onNext(T t) {
T v = current.get();
if (v != null) {
v = current.getAndSet(null);
}
if (v == null) {
current.lazySet(t);
} else if ((v = current.getAndSet(null)) != null) {
} else {
try {
current.lazySet(Objects.requireNonNull(reducer.apply(v, t), "The reducer returned a null value"));
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
upstream.cancel();
onError(ex);
cancel();
return;
}
} else {
current.lazySet(t);
}
drain();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,23 @@ static final class BackpressureReduceWithSubscriber<T, R> extends AbstractBackpr
@Override
public void onNext(T t) {
R v = current.get();
if (v != null) {
v = current.getAndSet(null);
}
try {
if (v == null) {
current.lazySet(Objects.requireNonNull(
reducer.apply(Objects.requireNonNull(supplier.get(), "The supplier returned a null value"), t),
"The reducer returned a null value"
));
} else if ((v = current.getAndSet(null)) != null) {
current.lazySet(Objects.requireNonNull(reducer.apply(v, t), "The reducer returned a null value"));
} else {
current.lazySet(Objects.requireNonNull(
reducer.apply(Objects.requireNonNull(supplier.get(), "The supplier returned a null value"), t),
"The reducer returned a null value"
));
current.lazySet(Objects.requireNonNull(reducer.apply(v, t), "The reducer returned a null value"));
}
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
upstream.cancel();
onError(ex);
cancel();
return;
}
drain();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ public void synchronousDrop() {
ts.assertTerminated();
}

@Test
public void reduceBackpressuredSync() {
PublishProcessor<Integer> source = PublishProcessor.create();
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0L);

source.onBackpressureReduce(Integer::sum).subscribe(ts);

source.onNext(1);
source.onNext(2);
source.onNext(3);

ts.request(1);

ts.assertValuesOnly(6);

source.onNext(4);
source.onComplete();

ts.assertValuesOnly(6);

ts.request(1);
ts.assertResult(6, 4);
}

private <T> TestSubscriberEx<T> createDelayedSubscriber() {
return new TestSubscriberEx<T>(1L) {
final Random rnd = new Random();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ public void simpleBackpressure() {
ts.assertNotComplete();
}

@Test
public void reduceBackpressuredSync() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the difference from synchronousDrop test, maybe just rename it to reduceBacpressuredSync?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different request pattern.

PublishProcessor<Integer> source = PublishProcessor.create();
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0L);

source.onBackpressureReduce(() -> 0, Integer::sum).subscribe(ts);

source.onNext(1);
source.onNext(2);
source.onNext(3);

ts.request(1);

ts.assertValuesOnly(6);

source.onNext(4);
source.onComplete();

ts.assertValuesOnly(6);

ts.request(1);
ts.assertResult(6, 4);
}

@Test
public void synchronousDrop() {
PublishProcessor<Integer> source = PublishProcessor.create();
Expand Down