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:
committed by
Jesse Wilson
parent
d1b99e6925
commit
d81ec0f23b
@@ -44,9 +44,11 @@ class OkHttpClientTestRule : TestRule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun ensureAllConnectionsReleased() {
|
fun ensureAllConnectionsReleased() {
|
||||||
val connectionPool = prototype!!.connectionPool
|
prototype?.let {
|
||||||
connectionPool.evictAll()
|
val connectionPool = it.connectionPool
|
||||||
assertThat(connectionPool.idleConnectionCount()).isEqualTo(0)
|
connectionPool.evictAll()
|
||||||
|
assertThat(connectionPool.connectionCount()).isEqualTo(0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun apply(base: Statement, description: Description): Statement {
|
override fun apply(base: Statement, description: Description): Statement {
|
||||||
@@ -68,12 +70,25 @@ class OkHttpClientTestRule : TestRule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun releaseClient() {
|
private fun releaseClient() {
|
||||||
prototypes.push(prototype)
|
prototype?.let {
|
||||||
prototype = null
|
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 {
|
companion object {
|
||||||
/**
|
/**
|
||||||
* Quick and dirty pool of OkHttpClient instances. Each has its own independent dispatcher and
|
* Quick and dirty pool of OkHttpClient instances. Each has its own independent dispatcher and
|
||||||
|
@@ -84,6 +84,7 @@ public final class WebSocketHttpTest {
|
|||||||
clientListener.assertExhausted();
|
clientListener.assertExhausted();
|
||||||
|
|
||||||
// TODO: assert all connections are released once leaks are fixed
|
// TODO: assert all connections are released once leaks are fixed
|
||||||
|
clientTestRule.abandonClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void textMessage() {
|
@Test public void textMessage() {
|
||||||
|
Reference in New Issue
Block a user