issue#103 Add delay for Dispatcher.Default to have time to propagate exceptions
This commit is contained in:
parent
00c222b461
commit
732aa05a87
2 changed files with 24 additions and 2 deletions
|
|
@ -21,6 +21,7 @@ class OkHttp3IdlingResource private constructor(
|
||||||
init {
|
init {
|
||||||
val currentCallback = dispatcher.idleCallback
|
val currentCallback = dispatcher.idleCallback
|
||||||
dispatcher.idleCallback = Runnable {
|
dispatcher.idleCallback = Runnable {
|
||||||
|
sleepForDispatcherDefaultCallInRetrofitErrorState()
|
||||||
callback?.onTransitionToIdle()
|
callback?.onTransitionToIdle()
|
||||||
currentCallback?.run()
|
currentCallback?.run()
|
||||||
}
|
}
|
||||||
|
|
@ -28,7 +29,13 @@ class OkHttp3IdlingResource private constructor(
|
||||||
|
|
||||||
override fun getName(): String = name
|
override fun getName(): String = name
|
||||||
|
|
||||||
override fun isIdleNow(): Boolean = dispatcher.runningCallsCount() == 0
|
override fun isIdleNow(): Boolean {
|
||||||
|
val isIdle = dispatcher.runningCallsCount() == 0
|
||||||
|
if (isIdle) {
|
||||||
|
sleepForDispatcherDefaultCallInRetrofitErrorState()
|
||||||
|
}
|
||||||
|
return isIdle
|
||||||
|
}
|
||||||
|
|
||||||
override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback?) {
|
override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback?) {
|
||||||
this.callback = callback
|
this.callback = callback
|
||||||
|
|
@ -46,5 +53,20 @@ class OkHttp3IdlingResource private constructor(
|
||||||
if (client == null) throw NullPointerException("client == null")
|
if (client == null) throw NullPointerException("client == null")
|
||||||
return OkHttp3IdlingResource(name, client.dispatcher)
|
return OkHttp3IdlingResource(name, client.dispatcher)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is required, because in case of Errors Retrofit uses Dispatcher.Default to suspendThrow
|
||||||
|
* see: retrofit2.KotlinExtensions.kt Exception.suspendAndThrow
|
||||||
|
* Relevant code issue: https://github.com/square/retrofit/blob/6cd6f7d8287f73909614cb7300fcde05f5719750/retrofit/src/main/java/retrofit2/KotlinExtensions.kt#L121
|
||||||
|
* This is the current suggested approach to their problem with Unchecked Exceptions
|
||||||
|
*
|
||||||
|
* Sadly Dispatcher.Default cannot be replaced yet, so we can't swap it out in tests:
|
||||||
|
* https://github.com/Kotlin/kotlinx.coroutines/issues/1365
|
||||||
|
*
|
||||||
|
* This brings us to this sleep for now.
|
||||||
|
*/
|
||||||
|
private fun sleepForDispatcherDefaultCallInRetrofitErrorState() {
|
||||||
|
Thread.sleep(200L)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ fun runOnUIAwaitOnCurrent(action: () -> Unit) {
|
||||||
|
|
||||||
fun loopMainThreadFor(delay: Long) {
|
fun loopMainThreadFor(delay: Long) {
|
||||||
if (Looper.getMainLooper().thread == Thread.currentThread()) {
|
if (Looper.getMainLooper().thread == Thread.currentThread()) {
|
||||||
Thread.sleep(200L)
|
Thread.sleep(delay)
|
||||||
} else {
|
} else {
|
||||||
Espresso.onView(ViewMatchers.isRoot()).perform(LoopMainThreadFor(delay))
|
Espresso.onView(ViewMatchers.isRoot()).perform(LoopMainThreadFor(delay))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue