Skip to content

Commit d81ec0f

Browse files
yschimkesquarejesse
authored andcommitted
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
1 parent d1b99e6 commit d81ec0f

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

okhttp-testing-support/src/main/java/okhttp3/OkHttpClientTestRule.kt

+20-5
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ class OkHttpClientTestRule : TestRule {
4444
}
4545

4646
fun ensureAllConnectionsReleased() {
47-
val connectionPool = prototype!!.connectionPool
48-
connectionPool.evictAll()
49-
assertThat(connectionPool.idleConnectionCount()).isEqualTo(0)
47+
prototype?.let {
48+
val connectionPool = it.connectionPool
49+
connectionPool.evictAll()
50+
assertThat(connectionPool.connectionCount()).isEqualTo(0)
51+
}
5052
}
5153

5254
override fun apply(base: Statement, description: Description): Statement {
@@ -68,12 +70,25 @@ class OkHttpClientTestRule : TestRule {
6870
}
6971

7072
private fun releaseClient() {
71-
prototypes.push(prototype)
72-
prototype = null
73+
prototype?.let {
74+
prototypes.push(it)
75+
prototype = null
76+
}
7377
}
7478
}
7579
}
7680

81+
/**
82+
* Called if a test is known to be leaky.
83+
*/
84+
fun abandonClient() {
85+
prototype?.let {
86+
prototype = null
87+
it.dispatcher.executorService.shutdownNow()
88+
it.connectionPool.evictAll()
89+
}
90+
}
91+
7792
companion object {
7893
/**
7994
* Quick and dirty pool of OkHttpClient instances. Each has its own independent dispatcher and

okhttp/src/test/java/okhttp3/internal/ws/WebSocketHttpTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public final class WebSocketHttpTest {
8484
clientListener.assertExhausted();
8585

8686
// TODO: assert all connections are released once leaks are fixed
87+
clientTestRule.abandonClient();
8788
}
8889

8990
@Test public void textMessage() {

0 commit comments

Comments
 (0)