Skip to content

Commit

Permalink
OkHttpClientTestRule check connectionCount instead of idle (#5226)
Browse files Browse the repository at this point in the history
* OkHttpClientTestRule check connectionCount instead of idle

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

* Abandon unclean clients

* Simplify logic
  • Loading branch information
yschimke authored and squarejesse committed Jun 26, 2019
1 parent d1b99e6 commit d81ec0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public final class WebSocketHttpTest {
clientListener.assertExhausted();

// TODO: assert all connections are released once leaks are fixed
clientTestRule.abandonClient();
}

@Test public void textMessage() {
Expand Down

0 comments on commit d81ec0f

Please sign in to comment.