Created
April 5, 2018 08:11
-
-
Save emartynov/bf5aeb1828def440db34e858217e965d to your computer and use it in GitHub Desktop.
This file contains 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
inline fun completable( | |
crossinline action: () -> Unit, | |
crossinline finally: () -> Unit | |
): Completable { | |
return Completable.create { emitter -> | |
try { | |
action() | |
emitter.onComplete() | |
} catch (t: Throwable) { | |
// Attempts to emit the specified {@code Throwable} error if the downstream | |
// hasn't cancelled the sequence or is otherwise terminated, returning false | |
// if the emission is not allowed to happen due to lifecycle restrictions. | |
// <p> | |
// Unlike {@link #onError(Throwable)}, the {@code RxJavaPlugins.onError} is not called | |
// if the error could not be delivered. | |
emitter.tryOnError(t) | |
} finally { | |
finally() | |
} | |
} | |
} |
This file contains 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
@Test | |
fun `Completable do not crash when terminated`() { | |
var ex: Throwable? = null | |
RxJavaPlugins.setErrorHandler { t -> ex = t } | |
Completable.mergeArray( | |
completable { | |
throw IOException("test1") | |
}.subscribeOn(Schedulers.io()), | |
completable { | |
throw IOException("test2") | |
}.subscribeOn(Schedulers.io()) | |
).onErrorComplete() | |
.blockingAwait() | |
RxJavaPlugins.setErrorHandler(null) | |
ex?.let { | |
throw it | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment