-
Notifications
You must be signed in to change notification settings - Fork 3
[CDX-358] Handle undeliverable RxJava exceptions #160
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fb93148
[CDX-358] Handle undeliverable RxJava exceptions
c937920
Version Bump
f8a524e
Revert "Version Bump"
5bdae08
add tests
09aaf14
re-throw unexpected exceptions on the current thread
599576a
fall back to the JVM default exception handler if the thread has none…
1cb75a0
restore the interrupt flag for InterruptedExceptions + add more tests
a9cf10f
Remove unnecessary var assignment
HHHindawy a252f8f
Add Synchronized to setupRxJavaErrorHandler
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
library/src/test/java/io/constructor/core/ConstructorIoRxErrorHandlerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package io.constructor.core | ||
|
|
||
| import io.reactivex.exceptions.UndeliverableException | ||
| import io.reactivex.functions.Consumer | ||
| import io.reactivex.plugins.RxJavaPlugins | ||
| import org.junit.After | ||
| import org.junit.Assert.assertSame | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import java.io.IOException | ||
| import java.net.SocketTimeoutException | ||
|
|
||
| class ConstructorIoRxErrorHandlerTest { | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| RxJavaPlugins.reset() | ||
| ConstructorIo.setupRxJavaErrorHandler() | ||
| } | ||
|
|
||
| @After | ||
| fun teardown() { | ||
| RxJavaPlugins.reset() | ||
| } | ||
|
|
||
| @Test | ||
| fun isIdempotent() { | ||
| val handlerAfterFirstCall = RxJavaPlugins.getErrorHandler() | ||
| ConstructorIo.setupRxJavaErrorHandler() | ||
| assertSame(handlerAfterFirstCall, RxJavaPlugins.getErrorHandler()) | ||
| } | ||
|
|
||
| @Test | ||
| fun doesNotOverwriteExistingErrorHandler() { | ||
| RxJavaPlugins.reset() | ||
| val existingHandler = Consumer<Throwable> { } | ||
| RxJavaPlugins.setErrorHandler(existingHandler) | ||
|
|
||
| ConstructorIo.setupRxJavaErrorHandler() | ||
|
|
||
| assertSame(existingHandler, RxJavaPlugins.getErrorHandler()) | ||
| } | ||
|
|
||
| @Test | ||
| fun handlesUndeliverableIOException() { | ||
| // Should not throw - IOException is handled gracefully | ||
| RxJavaPlugins.onError(UndeliverableException(IOException("network timeout"))) | ||
| } | ||
|
|
||
| @Test | ||
| fun handlesUndeliverableInterruptedException() { | ||
| // Should not throw - InterruptedException is handled gracefully | ||
| // and the interrupt flag should be restored on the current thread | ||
| Thread.interrupted() // clear any pre-existing interrupt flag | ||
| RxJavaPlugins.onError(UndeliverableException(InterruptedException("thread interrupted"))) | ||
| assertTrue("Interrupt flag should be restored", Thread.interrupted()) | ||
| } | ||
|
|
||
| @Test | ||
| fun handlesRawIOException() { | ||
| // Should not throw - IOException without UndeliverableException wrapper | ||
| RxJavaPlugins.onError(IOException("connection reset")) | ||
| } | ||
|
|
||
| @Test | ||
| fun handlesSocketTimeoutException() { | ||
| // Should not throw - SocketTimeoutException is a subclass of IOException | ||
| RxJavaPlugins.onError(UndeliverableException(SocketTimeoutException("connect timed out"))) | ||
| } | ||
|
|
||
| @Test | ||
| fun forwardsUnexpectedExceptionToUncaughtExceptionHandler() { | ||
| val original = Thread.currentThread().uncaughtExceptionHandler | ||
| try { | ||
| var caughtThrowable: Throwable? = null | ||
| Thread.currentThread().uncaughtExceptionHandler = | ||
| Thread.UncaughtExceptionHandler { _, throwable -> caughtThrowable = throwable } | ||
|
|
||
| val error = IllegalStateException("unexpected error") | ||
| RxJavaPlugins.onError(UndeliverableException(error)) | ||
|
|
||
| assertSame(error, caughtThrowable) | ||
| } finally { | ||
| Thread.currentThread().uncaughtExceptionHandler = original | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun logsUnexpectedExceptionWhenNoUncaughtHandler() { | ||
| val original = Thread.currentThread().uncaughtExceptionHandler | ||
| val defaultHandler = Thread.getDefaultUncaughtExceptionHandler() | ||
| try { | ||
| Thread.currentThread().uncaughtExceptionHandler = null | ||
| Thread.setDefaultUncaughtExceptionHandler(null) | ||
|
|
||
| // Should not throw — falls back to logging when both handlers are null | ||
| RxJavaPlugins.onError(UndeliverableException(IllegalStateException("no handler"))) | ||
| } finally { | ||
| Thread.currentThread().uncaughtExceptionHandler = original | ||
| Thread.setDefaultUncaughtExceptionHandler(defaultHandler) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun forwardsNullCauseExceptionToUncaughtExceptionHandler() { | ||
| val original = Thread.currentThread().uncaughtExceptionHandler | ||
| try { | ||
| var caughtThrowable: Throwable? = null | ||
| Thread.currentThread().uncaughtExceptionHandler = | ||
| Thread.UncaughtExceptionHandler { _, throwable -> caughtThrowable = throwable } | ||
|
|
||
| val error = UndeliverableException(null) | ||
| RxJavaPlugins.onError(error) | ||
|
|
||
| // When cause is null, the UndeliverableException itself is forwarded | ||
| assertSame(error, caughtThrowable) | ||
| } finally { | ||
| Thread.currentThread().uncaughtExceptionHandler = original | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.