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: DoAfterTerminate handle if action throws #3823

Merged
merged 1 commit into from
Apr 4, 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import rx.Observable.Operator;
import rx.Subscriber;
import rx.exceptions.Exceptions;
import rx.functions.Action0;
import rx.plugins.RxJavaPlugins;

/**
* Registers an action to be called after an Observable invokes {@code onComplete} or {@code onError}.
Expand Down Expand Up @@ -53,7 +55,7 @@ public void onError(Throwable e) {
try {
child.onError(e);
} finally {
action.call();
callAction();
}
}

Expand All @@ -62,7 +64,16 @@ public void onCompleted() {
try {
child.onCompleted();
} finally {
callAction();
}
}

void callAction() {
try {
action.call();
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
RxJavaPlugins.getInstance().getErrorHandler().handleError(ex);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@
*/
package rx.internal.operators;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.*;

import rx.Observable;
import rx.Observer;
import rx.*;
import rx.functions.Action0;
import rx.observers.TestSubscriber;

public class OperatorDoAfterTerminateTest {

Expand Down Expand Up @@ -65,4 +61,37 @@ public void nullActionShouldBeCheckedInConstructor() {
assertEquals("Action can not be null", expected.getMessage());
}
}

@Test
public void nullFinallyActionShouldBeCheckedASAP() {
try {
Observable
.just("value")
.doAfterTerminate(null);

fail();
} catch (NullPointerException expected) {

}
}

@Test
public void ifFinallyActionThrowsExceptionShouldNotBeSwallowedAndActionShouldBeCalledOnce() {
Action0 finallyAction = mock(Action0.class);
doThrow(new IllegalStateException()).when(finallyAction).call();

TestSubscriber<String> testSubscriber = new TestSubscriber<String>();

Observable
.just("value")
.doAfterTerminate(finallyAction)
.subscribe(testSubscriber);

testSubscriber.assertValue("value");

verify(finallyAction).call();
// Actual result:
// Not only IllegalStateException was swallowed
// But finallyAction was called twice!
}
}