1
0
mirror of https://github.com/square/okhttp.git synced 2025-08-07 12:42:57 +03:00

OkHttpClientTestRule check connectionCount instead of idle (#5226)

* OkHttpClientTestRule check connectionCount instead of idle

Clients should be clean after use, not just from idle connections.

* Abandon unclean clients

* Simplify logic
This commit is contained in:
Yuri Schimke
2019-06-26 06:34:51 +01:00
committed by Jesse Wilson
parent d1b99e6925
commit d81ec0f23b
2 changed files with 21 additions and 5 deletions

View File

@@ -44,9 +44,11 @@ class OkHttpClientTestRule : TestRule {
}
fun ensureAllConnectionsReleased() {
val connectionPool = prototype!!.connectionPool
connectionPool.evictAll()
assertThat(connectionPool.idleConnectionCount()).isEqualTo(0)
prototype?.let {
val connectionPool = it.connectionPool
connectionPool.evictAll()
assertThat(connectionPool.connectionCount()).isEqualTo(0)
}
}
override fun apply(base: Statement, description: Description): Statement {
@@ -68,12 +70,25 @@ class OkHttpClientTestRule : TestRule {
}
private fun releaseClient() {
prototypes.push(prototype)
prototype = null
prototype?.let {
prototypes.push(it)
prototype = null
}
}
}
}
/**
* Called if a test is known to be leaky.
*/
fun abandonClient() {
prototype?.let {
prototype = null
it.dispatcher.executorService.shutdownNow()
it.connectionPool.evictAll()
}
}
companion object {
/**
* Quick and dirty pool of OkHttpClient instances. Each has its own independent dispatcher and