diff --git a/okhttp-testing-support/src/main/java/okhttp3/OkHttpClientTestRule.kt b/okhttp-testing-support/src/main/java/okhttp3/OkHttpClientTestRule.kt index 90e067b8a869..d94f9f5c170d 100644 --- a/okhttp-testing-support/src/main/java/okhttp3/OkHttpClientTestRule.kt +++ b/okhttp-testing-support/src/main/java/okhttp3/OkHttpClientTestRule.kt @@ -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 diff --git a/okhttp/src/test/java/okhttp3/internal/ws/WebSocketHttpTest.java b/okhttp/src/test/java/okhttp3/internal/ws/WebSocketHttpTest.java index ad966506bd32..4806f923dbcf 100644 --- a/okhttp/src/test/java/okhttp3/internal/ws/WebSocketHttpTest.java +++ b/okhttp/src/test/java/okhttp3/internal/ws/WebSocketHttpTest.java @@ -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() {