Skip to content

Instantly share code, notes, and snippets.

@gtomek
Created May 16, 2023 19:15
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 gtomek/caf7334f29905fcea4865b15c19f2e41 to your computer and use it in GitHub Desktop.
Save gtomek/caf7334f29905fcea4865b15c19f2e41 to your computer and use it in GitHub Desktop.
OkHttp3 IdlingResource in kotlin
class OkHttp3IdlingResource(
private val name: String,
private val dispatcher: Dispatcher
) : IdlingResource {
private val callbackReference = AtomicReference<ResourceCallback>()
init {
dispatcher.idleCallback = Runnable {
callbackReference.get()?.onTransitionToIdle()
}
}
override fun getName(): String = name
override fun isIdleNow(): Boolean {
val isIdle = dispatcher.runningCallsCount() == 0
val callback = callbackReference.get()
if (isIdle && callback != null) callback.onTransitionToIdle()
return isIdle
}
override fun registerIdleTransitionCallback(callback: ResourceCallback?) {
callbackReference.set(callback)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment