Created
May 16, 2023 19:15
-
-
Save gtomek/caf7334f29905fcea4865b15c19f2e41 to your computer and use it in GitHub Desktop.
OkHttp3 IdlingResource in kotlin
This file contains hidden or 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
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