Skip to content

Commit

Permalink
catch onCompleted unsubscribe error and report to RxJavaPlugin error …
Browse files Browse the repository at this point in the history
…handler
  • Loading branch information
davidmoten committed Aug 14, 2015
1 parent adfabec commit 5e78f01
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/main/java/rx/observers/SafeSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,16 @@ public void onCompleted() {
// handle errors if the onCompleted implementation fails, not just if the Observable fails
_onError(e);
} finally {
// auto-unsubscribe
unsubscribe();
try {
// auto-unsubscribe
unsubscribe();
} catch (Throwable e2) {
try {
RxJavaPlugins.getInstance().getErrorHandler().handleError(e2);
} catch (Throwable pluginException) {
handlePluginException(pluginException);
}
}
}
}
}
Expand Down
44 changes: 41 additions & 3 deletions src/test/java/rx/observers/SafeSubscriberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
*/
package rx.observers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicInteger;

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

import rx.exceptions.*;
import rx.exceptions.OnErrorFailedException;
import rx.exceptions.OnErrorNotImplementedException;
import rx.exceptions.TestException;
import rx.functions.Action0;
import rx.plugins.*;
import rx.plugins.RxJavaErrorHandler;
import rx.plugins.RxJavaPlugins;
import rx.subscriptions.Subscriptions;

public class SafeSubscriberTest {
Expand Down Expand Up @@ -227,4 +235,34 @@ public void call() {

safe.onError(new TestException());
}

@Test
public void testPluginErrorHandlerReceivesExceptionWhenUnsubscribeAfterCompletionThrows() {
final AtomicInteger calls = new AtomicInteger();
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {
@Override
public void handleError(Throwable e) {
calls.incrementAndGet();
}
});

final AtomicInteger errors = new AtomicInteger();
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {
@Override
public void onError(Throwable e) {
errors.incrementAndGet();
}
};
SafeSubscriber<Integer> safe = new SafeSubscriber<Integer>(ts);
safe.add(Subscriptions.create(new Action0() {
@Override
public void call() {
throw new RuntimeException();
}
}));

safe.onCompleted();
assertEquals(1, (int) calls.get());
assertEquals(0, (int) errors.get());
}
}

0 comments on commit 5e78f01

Please sign in to comment.