Skip to content

Instantly share code, notes, and snippets.

@eungju

eungju/RxTest.kt

Last active Nov 26, 2017
Embed
What would you like to do?
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