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

1.x: make Completable.subscribe() report isUnsubscribed consistently #3707

Merged
merged 1 commit into from
Feb 15, 2016
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
8 changes: 7 additions & 1 deletion src/main/java/rx/Completable.java
Original file line number Diff line number Diff line change
Expand Up @@ -1828,12 +1828,13 @@ public final Subscription subscribe() {
subscribe(new CompletableSubscriber() {
@Override
public void onCompleted() {
// nothing to do
mad.unsubscribe();
}

@Override
public void onError(Throwable e) {
ERROR_HANDLER.handleError(e);
mad.unsubscribe();
}

@Override
Expand Down Expand Up @@ -1864,11 +1865,13 @@ public void onCompleted() {
} catch (Throwable e) {
ERROR_HANDLER.handleError(e);
}
mad.unsubscribe();
}

@Override
public void onError(Throwable e) {
ERROR_HANDLER.handleError(e);
mad.unsubscribe();
}

@Override
Expand Down Expand Up @@ -1900,7 +1903,9 @@ public void onCompleted() {
onComplete.call();
} catch (Throwable e) {
onError(e);
return;
}
mad.unsubscribe();
Copy link
Member

Choose a reason for hiding this comment

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

Should be in finally?

Copy link
Member

Choose a reason for hiding this comment

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

Or just remove the above return?

Copy link
Member Author

Choose a reason for hiding this comment

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

OnError calls it too, shouldn't call again after onError returns.

}

@Override
Expand All @@ -1911,6 +1916,7 @@ public void onError(Throwable e) {
e = new CompositeException(Arrays.asList(e, ex));
ERROR_HANDLER.handleError(e);
}
mad.unsubscribe();
}

@Override
Expand Down
138 changes: 138 additions & 0 deletions src/test/java/rx/CompletableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3604,4 +3604,142 @@ public Completable call(Integer t) {
assertTrue(listEx.get(1).toString(), listEx.get(1) instanceof TestException);
}

@Test
public void subscribeReportsUnsubscribed() {
PublishSubject<String> stringSubject = PublishSubject.create();
Completable completable = stringSubject.toCompletable();

Subscription completableSubscription = completable.subscribe();

stringSubject.onCompleted();

assertTrue("Not unsubscribed?", completableSubscription.isUnsubscribed());
}

@Test
public void subscribeReportsUnsubscribedOnError() {
PublishSubject<String> stringSubject = PublishSubject.create();
Completable completable = stringSubject.toCompletable();

Subscription completableSubscription = completable.subscribe();

stringSubject.onError(new TestException());

assertTrue("Not unsubscribed?", completableSubscription.isUnsubscribed());
}

@Test
public void subscribeActionReportsUnsubscribed() {
PublishSubject<String> stringSubject = PublishSubject.create();
Completable completable = stringSubject.toCompletable();

Subscription completableSubscription = completable.subscribe(Actions.empty());

stringSubject.onCompleted();

assertTrue("Not unsubscribed?", completableSubscription.isUnsubscribed());
}

@Test
public void subscribeActionReportsUnsubscribedAfter() {
PublishSubject<String> stringSubject = PublishSubject.create();
Completable completable = stringSubject.toCompletable();

final AtomicReference<Subscription> subscriptionRef = new AtomicReference<Subscription>();
Subscription completableSubscription = completable.subscribe(new Action0() {
@Override
public void call() {
if (subscriptionRef.get().isUnsubscribed()) {
subscriptionRef.set(null);
}
}
});
subscriptionRef.set(completableSubscription);

stringSubject.onCompleted();

assertTrue("Not unsubscribed?", completableSubscription.isUnsubscribed());
assertNotNull("Unsubscribed before the call to onCompleted", subscriptionRef.get());
}

@Test
public void subscribeActionReportsUnsubscribedOnError() {
PublishSubject<String> stringSubject = PublishSubject.create();
Completable completable = stringSubject.toCompletable();

Subscription completableSubscription = completable.subscribe(Actions.empty());

stringSubject.onError(new TestException());

assertTrue("Not unsubscribed?", completableSubscription.isUnsubscribed());
}

@Test
public void subscribeAction2ReportsUnsubscribed() {
PublishSubject<String> stringSubject = PublishSubject.create();
Completable completable = stringSubject.toCompletable();

Subscription completableSubscription = completable.subscribe(Actions.empty(), Actions.empty());

stringSubject.onCompleted();

assertTrue("Not unsubscribed?", completableSubscription.isUnsubscribed());
}

@Test
public void subscribeAction2ReportsUnsubscribedOnError() {
PublishSubject<String> stringSubject = PublishSubject.create();
Completable completable = stringSubject.toCompletable();

Subscription completableSubscription = completable.subscribe(Actions.empty(), Actions.empty());

stringSubject.onError(new TestException());

assertTrue("Not unsubscribed?", completableSubscription.isUnsubscribed());
}

@Test
public void subscribeAction2ReportsUnsubscribedAfter() {
PublishSubject<String> stringSubject = PublishSubject.create();
Completable completable = stringSubject.toCompletable();

final AtomicReference<Subscription> subscriptionRef = new AtomicReference<Subscription>();
Subscription completableSubscription = completable.subscribe(Actions.empty(), new Action0() {
@Override
public void call() {
if (subscriptionRef.get().isUnsubscribed()) {
subscriptionRef.set(null);
}
}
});
subscriptionRef.set(completableSubscription);

stringSubject.onCompleted();

assertTrue("Not unsubscribed?", completableSubscription.isUnsubscribed());
assertNotNull("Unsubscribed before the call to onCompleted", subscriptionRef.get());
}

@Test
public void subscribeAction2ReportsUnsubscribedOnErrorAfter() {
PublishSubject<String> stringSubject = PublishSubject.create();
Completable completable = stringSubject.toCompletable();

final AtomicReference<Subscription> subscriptionRef = new AtomicReference<Subscription>();
Subscription completableSubscription = completable.subscribe(new Action1<Throwable>() {
@Override
public void call(Throwable e) {
if (subscriptionRef.get().isUnsubscribed()) {
subscriptionRef.set(null);
}
}
}, Actions.empty());
subscriptionRef.set(completableSubscription);

stringSubject.onError(new TestException());

assertTrue("Not unsubscribed?", completableSubscription.isUnsubscribed());
assertNotNull("Unsubscribed before the call to onError", subscriptionRef.get());
}

}