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: Improve coverage & related cleanup 03/05 #5891

Merged
merged 2 commits into from
Mar 5, 2018
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 @@ -125,9 +125,7 @@ public T next() {

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(batchSize);
}
SubscriptionHelper.setOnce(this, s, batchSize);
Copy link
Member Author

Choose a reason for hiding this comment

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

When the request call comes directly after setOnce, they can be combined into one and this resolves a lot of partial coverage cases.

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,7 @@ static final class BufferOpenSubscriber<Open>

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down Expand Up @@ -378,9 +376,7 @@ static final class BufferCloseSubscriber<T, C extends Collection<? super T>>

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ public void removeChild(ReplaySubscription<T> p) {

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(connection, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(connection, s, Long.MAX_VALUE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,7 @@ static final class CombineLatestInnerSubscriber<T>

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(prefetch);
}
SubscriptionHelper.setOnce(this, s, prefetch);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,7 @@ public boolean isDisposed() {

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down Expand Up @@ -470,9 +468,7 @@ public boolean isDisposed() {

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ static final class MergeWithObserver<T> extends AtomicInteger
}

@Override
public void onSubscribe(Subscription d) {
if (SubscriptionHelper.setOnce(mainSubscription, d)) {
d.request(prefetch);
}
public void onSubscribe(Subscription s) {
SubscriptionHelper.setOnce(mainSubscription, s, prefetch);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ static final class MergeWithObserver<T> extends AtomicInteger
}

@Override
public void onSubscribe(Subscription d) {
if (SubscriptionHelper.setOnce(mainSubscription, d)) {
d.request(prefetch);
}
public void onSubscribe(Subscription s) {
SubscriptionHelper.setOnce(mainSubscription, s, prefetch);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,36 +526,16 @@ static final class InnerSubscription<T> extends AtomicLong implements Subscripti
public void request(long n) {
// ignore negative requests
if (SubscriptionHelper.validate(n)) {
// In general, RxJava doesn't prevent concurrent requests (with each other or with
// a cancel) so we need a CAS-loop, but we need to handle
// request overflow and cancelled/not requested state as well.
for (;;) {
// get the current request amount
long r = get();
// if child called cancel() do nothing
if (r == CANCELLED) {
return;
}
// ignore zero requests except any first that sets in zero
if (r >= 0L && n == 0) {
return;
}
// otherwise, increase the request count
long u = BackpressureHelper.addCap(r, n);

// try setting the new request value
if (compareAndSet(r, u)) {
// increment the total request counter
BackpressureHelper.add(totalRequested, n);
// if successful, notify the parent dispatcher this child can receive more
// elements
parent.manageRequests();

parent.buffer.replay(this);
return;
}
// otherwise, someone else changed the state (perhaps a concurrent
// request or cancellation) so retry
// add to the current requested and cap it at MAX_VALUE
Copy link
Member Author

Choose a reason for hiding this comment

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

This was one of the earliest v2 operators where the infrastructure lacked the addCancel method, simplifying this method significantly.

// except when there was a concurrent cancellation
if (BackpressureHelper.addCancel(this, n) != CANCELLED) {
// increment the total request counter
BackpressureHelper.add(totalRequested, n);
// if successful, notify the parent dispatcher this child can receive more
// elements
parent.manageRequests();
// try replaying any cached content
parent.buffer.replay(this);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public void onComplete() {
completeMain();
}

boolean setOther(Subscription o) {
return SubscriptionHelper.setOnce(other, o);
void setOther(Subscription o) {
SubscriptionHelper.setOnce(other, o, Long.MAX_VALUE);
Copy link
Member Author

Choose a reason for hiding this comment

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

It is always Long.MAX_VALUE and moved up from below.

}

@Override
Expand Down Expand Up @@ -150,9 +150,7 @@ static final class SamplerSubscriber<T> implements FlowableSubscriber<Object> {

@Override
public void onSubscribe(Subscription s) {
if (parent.setOther(s)) {
s.request(Long.MAX_VALUE);
}
parent.setOther(s);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ final class OtherSubscriber extends AtomicReference<Subscription>

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ final class OtherSubscriber extends AtomicReference<Subscription> implements Flo

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,7 @@ static final class TimeoutConsumer extends AtomicReference<Subscription>

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ static final class WindowBoundaryMainSubscriber<T, B>

@Override
public void onSubscribe(Subscription d) {
if (SubscriptionHelper.setOnce(upstream, d)) {
d.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(upstream, d, Long.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,7 @@ static final class WithLatestInnerSubscriber

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ static final class OtherSubscriber<T> extends

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.reactivex.*;
import io.reactivex.disposables.*;
import io.reactivex.exceptions.Exceptions;

/**
* Waits until the source Future completes or the wait times out; treats a {@code null}
Expand Down Expand Up @@ -50,17 +51,11 @@ protected void subscribeActual(MaybeObserver<? super T> observer) {
} else {
v = future.get(timeout, unit);
}
} catch (InterruptedException ex) {
if (!d.isDisposed()) {
observer.onError(ex);
}
return;
} catch (ExecutionException ex) {
if (!d.isDisposed()) {
observer.onError(ex.getCause());
} catch (Throwable ex) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Reduce the code duplication to one catch and extract the actual Throwable if ex was the ExecutionException.

if (ex instanceof ExecutionException) {
ex = ex.getCause();
}
return;
} catch (TimeoutException ex) {
Exceptions.throwIfFatal(ex);
if (!d.isDisposed()) {
observer.onError(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ static final class TakeUntilOtherMaybeObserver<U>

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ static final class TimeoutOtherMaybeObserver<T, U>

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import io.reactivex.*;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.disposables.DisposableHelper;

public final class ObservableSkip<T> extends AbstractObservableWithUpstream<T, T> {
final long n;
Expand All @@ -40,9 +41,11 @@ static final class SkipObserver<T> implements Observer<T>, Disposable {
}

@Override
public void onSubscribe(Disposable s) {
this.d = s;
actual.onSubscribe(this);
public void onSubscribe(Disposable d) {
if (DisposableHelper.validate(this.d, d)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Validation was missing.

this.d = d;
actual.onSubscribe(this);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,7 @@ static final class JoinInnerSubscriber<T>

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(prefetch);
}
SubscriptionHelper.setOnce(this, s, prefetch);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ static final class ParallelReduceFullInnerSubscriber<T>

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,7 @@ static final class SortedJoinInnerSubscriber<T>

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static final class OtherObserver<T, U>

@Override
public void onSubscribe(Disposable d) {
if (DisposableHelper.set(this, d)) {
if (DisposableHelper.setOnce(this, d)) {
Copy link
Member Author

@akarnokd akarnokd Mar 5, 2018

Choose a reason for hiding this comment

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

This should have been setOnce all along.


actual.onSubscribe(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ static final class TakeUntilOtherSubscriber

@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this, s)) {
s.request(Long.MAX_VALUE);
}
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}

@Override
Expand Down
Loading