Skip to content

Instantly share code, notes, and snippets.

@eungju
Last active November 26, 2017 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eungju/31dc5c615cc662dddafd14651e417605 to your computer and use it in GitHub Desktop.
Save eungju/31dc5c615cc662dddafd14651e417605 to your computer and use it in GitHub Desktop.
Catch UndeliverableException
@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