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

Removed unnecessary upstream.cancel() call for casually finished upstream sequences. #6992

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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.reactivestreams.*;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.*;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.internal.fuseable.*;
Expand Down Expand Up @@ -124,7 +123,7 @@ public void cancel() {
}
}

static final class MulticastProcessor<T> extends Flowable<T> implements FlowableSubscriber<T>, Disposable {
static final class MulticastProcessor<T> extends Flowable<T> implements FlowableSubscriber<T> {

@SuppressWarnings("rawtypes")
static final MulticastSubscription[] EMPTY = new MulticastSubscription[0];
Expand Down Expand Up @@ -192,18 +191,18 @@ public void onSubscribe(Subscription s) {
}
}

@Override
public void dispose() {
SubscriptionHelper.cancel(upstream);
if (wip.getAndIncrement() == 0) {
SimpleQueue<T> q = queue;
if (q != null) {
q.clear();
if (!done) {
SubscriptionHelper.cancel(upstream);
if (wip.getAndIncrement() == 0) {
SimpleQueue<T> q = queue;
if (q != null) {
q.clear();
}
}
}
}

@Override
public boolean isDisposed() {
return upstream.get() == SubscriptionHelper.CANCELLED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.*;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.*;
Expand Down Expand Up @@ -538,4 +539,68 @@ public boolean test(Integer w) throws Exception {
.test()
.assertResult(1000000);
}

@Test
public void noUpstreamCancelOnCasualChainClose() {
AtomicBoolean parentUpstreamCancelled = new AtomicBoolean(false);
Flowable.range(1, 10)
.doOnCancel(new Action() {
Copy link
Member

Choose a reason for hiding this comment

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

You can use lambdas in tests now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool!
Should I change it?

Copy link
Member

Choose a reason for hiding this comment

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

Yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If that's fine, I've refactored whole file to lambdas.

@Override
public void run() throws Throwable {
parentUpstreamCancelled.set(true);
}
})
.publish(new Function<Flowable<Integer>, Publisher<Integer>>() {
@Override
public Publisher<Integer> apply(Flowable<Integer> v) throws Exception {
return v;
}
})
.test()
.awaitDone(1, TimeUnit.SECONDS);
assertFalse("Unnecessary upstream .cancel() call in FlowablePublishMulticast", parentUpstreamCancelled.get());
}

@Test
public void noUpstreamCancelOnCasualChainCloseWithInnerCancels() {
AtomicBoolean parentUpstreamCancelled = new AtomicBoolean(false);
Flowable.range(1, 10)
.doOnCancel(new Action() {
@Override
public void run() throws Throwable {
parentUpstreamCancelled.set(true);
}
})
.publish(new Function<Flowable<Integer>, Publisher<Integer>>() {
@Override
public Publisher<Integer> apply(Flowable<Integer> v) throws Exception {
return Flowable.concat(v.take(1), v.skip(5));
}
})
.test()
.awaitDone(1, TimeUnit.SECONDS);
assertFalse("Unnecessary upstream .cancel() call in FlowablePublishMulticast", parentUpstreamCancelled.get());
}

@Test
public void upstreamCancelOnDownstreamCancel() {
AtomicBoolean parentUpstreamCancelled = new AtomicBoolean(false);
Flowable.range(1, 10)
.doOnCancel(new Action() {
@Override
public void run() throws Throwable {
parentUpstreamCancelled.set(true);
}
})
.publish(new Function<Flowable<Integer>, Publisher<Integer>>() {
@Override
public Publisher<Integer> apply(Flowable<Integer> v) throws Exception {
return v;
}
})
.take(1)
.test()
.awaitDone(1, TimeUnit.SECONDS);
assertTrue("Upstream .cancel() not called in FlowablePublishMulticast", parentUpstreamCancelled.get());
}
}