Last active
November 26, 2017 14:13
-
-
Save eungju/31dc5c615cc662dddafd14651e417605 to your computer and use it in GitHub Desktop.
Catch UndeliverableException
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 simple() { | |
val observable = Observable.create<Int> { emitter -> | |
emitter.onNext(1) | |
emitter.onError(IOException("Timeout")) | |
} | |
observable | |
.take(1) | |
.subscribe({ assertEquals(1, it) }, { assertTrue(it is UndeliverableException) }) | |
} | |
@Test | |
fun complicated() { | |
val catchException = object : Thread.UncaughtExceptionHandler { | |
var caught: Throwable? = null | |
override fun uncaughtException(thread: Thread, throwable: Throwable) { | |
caught = throwable | |
} | |
} | |
val latch = CountDownLatch(1) | |
val observable = Observable.fromCallable { | |
println("Doing something") | |
latch.await() //Expect InterruptedException | |
} | |
val subscription = observable | |
.subscribeOn(Schedulers.from(Executors.newSingleThreadExecutor({ | |
Thread(it) | |
.apply { uncaughtExceptionHandler = catchException } | |
}))) | |
.subscribe { | |
fail() | |
} | |
Thread.sleep(100) //Slack | |
subscription.dispose() //Cause InterruptedException | |
Thread.sleep(100) //Slack | |
assertTrue(catchException.caught is UndeliverableException) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment