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

Create OkHttpClient instances eagerly in tests

We used to do this lazily because OkHttpClient instances were
somewhat heavy: each standalone instance held its own
ExecutorService for the connection pool.

Now that we have TaskRunner each instance is much more
lightweight and the drawbacks of creating instances eagerly
is negligible.
This commit is contained in:
Jesse Wilson
2019-09-26 22:04:58 -04:00
parent 253d2ead96
commit 9849ab8e3b
16 changed files with 51 additions and 122 deletions

View File

@@ -24,7 +24,6 @@ import okhttp3.mockwebserver.MockWebServer;
import okhttp3.sse.EventSource; import okhttp3.sse.EventSource;
import okhttp3.sse.EventSources; import okhttp3.sse.EventSources;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@@ -35,11 +34,7 @@ public final class EventSourceHttpTest {
@Rule public final OkHttpClientTestRule clientTestRule = new OkHttpClientTestRule(); @Rule public final OkHttpClientTestRule clientTestRule = new OkHttpClientTestRule();
private final EventSourceRecorder listener = new EventSourceRecorder(); private final EventSourceRecorder listener = new EventSourceRecorder();
private OkHttpClient client; private OkHttpClient client = clientTestRule.newClient();
@Before public void setUp() {
client = clientTestRule.newClient();
}
@After public void after() { @After public void after() {
listener.assertExhausted(); listener.assertExhausted();

View File

@@ -22,31 +22,36 @@ import org.junit.rules.TestRule
import org.junit.runner.Description import org.junit.runner.Description
import org.junit.runners.model.Statement import org.junit.runners.model.Statement
import java.net.InetAddress import java.net.InetAddress
import java.util.concurrent.ConcurrentLinkedDeque
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
/** Apply this rule to tests that need an OkHttpClient instance. */ /** Apply this rule to tests that need an OkHttpClient instance. */
class OkHttpClientTestRule : TestRule { class OkHttpClientTestRule : TestRule {
private val clientEventsList = mutableListOf<String>() private val clientEventsList = mutableListOf<String>()
private var prototype: OkHttpClient? = null private var testClient: OkHttpClient? = null
/** /**
* Returns an OkHttpClient for all tests to use as a starting point. * Returns an OkHttpClient for tests to use as a starting point.
* *
* The shared instance allows all tests to share a single connection pool, which prevents idle * The returned client installs a default event listener that gathers debug information. This will
* connections from consuming unnecessary resources while connections wait to be evicted. * be logged if the test fails.
* *
* This client is also configured to be slightly more deterministic, returning a single IP * This client is also configured to be slightly more deterministic, returning a single IP
* address for all hosts, regardless of the actual number of IP addresses reported by DNS. * address for all hosts, regardless of the actual number of IP addresses reported by DNS.
*/ */
fun newClient(): OkHttpClient { fun newClient(): OkHttpClient {
return newClientBuilder().build() var client = testClient
if (client == null) {
client = OkHttpClient.Builder()
.dns(SINGLE_INET_ADDRESS_DNS) // Prevent unexpected fallback addresses.
.eventListener(ClientRuleEventListener { addEvent(it) })
.build()
testClient = client
}
return client
} }
fun newClientBuilder(): OkHttpClient.Builder { fun newClientBuilder(): OkHttpClient.Builder {
return checkNotNull(prototype) { "don't create clients in test initialization!" } return newClient().newBuilder()
.newBuilder()
.eventListener(ClientRuleEventListener { addEvent(it) })
} }
@Synchronized private fun addEvent(it: String) { @Synchronized private fun addEvent(it: String) {
@@ -54,7 +59,7 @@ class OkHttpClientTestRule : TestRule {
} }
fun ensureAllConnectionsReleased() { fun ensureAllConnectionsReleased() {
prototype?.let { testClient?.let {
val connectionPool = it.connectionPool val connectionPool = it.connectionPool
connectionPool.evictAll() connectionPool.evictAll()
assertThat(connectionPool.connectionCount()).isEqualTo(0) assertThat(connectionPool.connectionCount()).isEqualTo(0)
@@ -72,7 +77,6 @@ class OkHttpClientTestRule : TestRule {
override fun apply(base: Statement, description: Description): Statement { override fun apply(base: Statement, description: Description): Statement {
return object : Statement() { return object : Statement() {
override fun evaluate() { override fun evaluate() {
acquireClient()
try { try {
base.evaluate() base.evaluate()
logEventsIfFlaky(description) logEventsIfFlaky(description)
@@ -86,15 +90,8 @@ class OkHttpClientTestRule : TestRule {
} }
} }
private fun acquireClient() {
prototype = prototypes.poll() ?: freshClient()
}
private fun releaseClient() { private fun releaseClient() {
prototype?.let { testClient?.dispatcher?.executorService?.shutdown()
prototypes.push(it)
prototype = null
}
} }
} }
} }
@@ -119,40 +116,16 @@ class OkHttpClientTestRule : TestRule {
} }
} }
/**
* 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
* connection pool. This way we can reuse expensive resources while preventing concurrent tests
* from interfering with each other.
*/
internal val prototypes = ConcurrentLinkedDeque<OkHttpClient>()
/** /**
* A network that resolves only one IP address per host. Use this when testing route selection * A network that resolves only one IP address per host. Use this when testing route selection
* fallbacks to prevent the host machine's various IP addresses from interfering. * fallbacks to prevent the host machine's various IP addresses from interfering.
*/ */
internal val SINGLE_INET_ADDRESS_DNS = object : Dns { private val SINGLE_INET_ADDRESS_DNS = object : Dns {
override fun lookup(hostname: String): List<InetAddress> { override fun lookup(hostname: String): List<InetAddress> {
val addresses = Dns.SYSTEM.lookup(hostname) val addresses = Dns.SYSTEM.lookup(hostname)
return listOf(addresses[0]) return listOf(addresses[0])
} }
} }
private fun freshClient(): OkHttpClient {
return OkHttpClient.Builder()
.dns(SINGLE_INET_ADDRESS_DNS) // Prevent unexpected fallback addresses.
.build()
}
} }
} }

View File

@@ -94,7 +94,7 @@ class DiskLruCache internal constructor(
maxSize: Long, maxSize: Long,
/** Used for asynchronous journal rebuilds. */ /** Used for asynchronous journal rebuilds. */
taskRunner: TaskRunner = TaskRunner.INSTANCE taskRunner: TaskRunner
) : Closeable, Flushable { ) : Closeable, Flushable {
/** The maximum number of bytes that this cache should use to store its data. */ /** The maximum number of bytes that this cache should use to store its data. */
@get:Synchronized @set:Synchronized var maxSize: Long = maxSize @get:Synchronized @set:Synchronized var maxSize: Long = maxSize

View File

@@ -19,7 +19,6 @@ import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer import okhttp3.mockwebserver.MockWebServer
import okhttp3.testing.PlatformRule import okhttp3.testing.PlatformRule
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.rules.TestRule import org.junit.rules.TestRule
@@ -32,11 +31,7 @@ class CallKotlinTest {
@JvmField @Rule val server = MockWebServer() @JvmField @Rule val server = MockWebServer()
@JvmField @Rule val clientTestRule = OkHttpClientTestRule() @JvmField @Rule val clientTestRule = OkHttpClientTestRule()
private lateinit var client: OkHttpClient private var client = clientTestRule.newClient()
@Before fun setUp() {
client = clientTestRule.newClient()
}
@Test @Test
fun legalToExecuteTwiceCloning() { fun legalToExecuteTwiceCloning() {

View File

@@ -110,7 +110,9 @@ public final class CallTest {
private RecordingEventListener listener = new RecordingEventListener(); private RecordingEventListener listener = new RecordingEventListener();
private HandshakeCertificates handshakeCertificates = localhost(); private HandshakeCertificates handshakeCertificates = localhost();
private OkHttpClient client; private OkHttpClient client = clientTestRule.newClientBuilder()
.eventListener(listener)
.build();
private RecordingCallback callback = new RecordingCallback(); private RecordingCallback callback = new RecordingCallback();
private TestLogHandler logHandler = new TestLogHandler(); private TestLogHandler logHandler = new TestLogHandler();
private Cache cache = new Cache(new File("/cache/"), Integer.MAX_VALUE, fileSystem); private Cache cache = new Cache(new File("/cache/"), Integer.MAX_VALUE, fileSystem);
@@ -120,9 +122,6 @@ public final class CallTest {
platform.assumeNotOpenJSSE(); platform.assumeNotOpenJSSE();
logger.addHandler(logHandler); logger.addHandler(logHandler);
client = clientTestRule.newClientBuilder()
.eventListener(listener)
.build();
} }
@After public void tearDown() throws Exception { @After public void tearDown() throws Exception {

View File

@@ -24,7 +24,6 @@ import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.SocketPolicy; import okhttp3.mockwebserver.SocketPolicy;
import okhttp3.testing.PlatformRule; import okhttp3.testing.PlatformRule;
import okhttp3.tls.HandshakeCertificates; import okhttp3.tls.HandshakeCertificates;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TestRule; import org.junit.rules.TestRule;
@@ -43,11 +42,7 @@ public final class ConnectionReuseTest {
@Rule public final OkHttpClientTestRule clientTestRule = new OkHttpClientTestRule(); @Rule public final OkHttpClientTestRule clientTestRule = new OkHttpClientTestRule();
private HandshakeCertificates handshakeCertificates = localhost(); private HandshakeCertificates handshakeCertificates = localhost();
private OkHttpClient client; private OkHttpClient client = clientTestRule.newClient();
@Before public void setUp() {
client = clientTestRule.newClient();
}
@Test public void connectionsAreReused() throws Exception { @Test public void connectionsAreReused() throws Exception {
server.enqueue(new MockResponse().setBody("a")); server.enqueue(new MockResponse().setBody("a"));

View File

@@ -35,11 +35,10 @@ class ConscryptTest {
@JvmField @Rule val clientTestRule = OkHttpClientTestRule() @JvmField @Rule val clientTestRule = OkHttpClientTestRule()
private lateinit var client: OkHttpClient private val client = clientTestRule.newClient()
@Before fun setUp() { @Before fun setUp() {
platform.assumeConscrypt() platform.assumeConscrypt()
client = clientTestRule.newClient()
} }
@Test @Test

View File

@@ -30,7 +30,6 @@ import java.util.Map;
import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest; import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@@ -45,11 +44,7 @@ public class CookiesTest {
@Rule public final MockWebServer server = new MockWebServer(); @Rule public final MockWebServer server = new MockWebServer();
@Rule public final OkHttpClientTestRule clientTestRule = new OkHttpClientTestRule(); @Rule public final OkHttpClientTestRule clientTestRule = new OkHttpClientTestRule();
private OkHttpClient client; private OkHttpClient client = clientTestRule.newClient();
@Before public void setUp() {
client = clientTestRule.newClient();
}
@Test @Test
public void testNetscapeResponse() throws Exception { public void testNetscapeResponse() throws Exception {

View File

@@ -20,16 +20,15 @@ public final class DispatcherTest {
RecordingWebSocketListener webSocketListener = new RecordingWebSocketListener(); RecordingWebSocketListener webSocketListener = new RecordingWebSocketListener();
Dispatcher dispatcher = new Dispatcher(executor); Dispatcher dispatcher = new Dispatcher(executor);
RecordingEventListener listener = new RecordingEventListener(); RecordingEventListener listener = new RecordingEventListener();
OkHttpClient client; OkHttpClient client = clientTestRule.newClientBuilder()
.dispatcher(dispatcher)
.eventListener(listener)
.build();
@Before public void setUp() throws Exception { @Before public void setUp() throws Exception {
dispatcher.setMaxRequests(20); dispatcher.setMaxRequests(20);
dispatcher.setMaxRequestsPerHost(10); dispatcher.setMaxRequestsPerHost(10);
listener.forbidLock(dispatcher); listener.forbidLock(dispatcher);
client = clientTestRule.newClientBuilder()
.dispatcher(dispatcher)
.eventListener(listener)
.build();
} }
@Test public void maxRequestsZero() throws Exception { @Test public void maxRequestsZero() throws Exception {

View File

@@ -51,14 +51,13 @@ public final class DuplexTest {
private RecordingEventListener listener = new RecordingEventListener(); private RecordingEventListener listener = new RecordingEventListener();
private HandshakeCertificates handshakeCertificates = localhost(); private HandshakeCertificates handshakeCertificates = localhost();
private OkHttpClient client; private OkHttpClient client = clientTestRule.newClientBuilder()
.eventListener(listener)
.build();
@Before public void setUp() { @Before public void setUp() {
platform.assumeNotOpenJSSE(); platform.assumeNotOpenJSSE();
platform.assumeHttp2Support(); platform.assumeHttp2Support();
client = clientTestRule.newClientBuilder()
.eventListener(listener)
.build();
} }
@Test public void http1DoesntSupportDuplex() throws IOException { @Test public void http1DoesntSupportDuplex() throws IOException {

View File

@@ -82,16 +82,14 @@ public final class EventListenerTest {
private final RecordingEventListener listener = new RecordingEventListener(); private final RecordingEventListener listener = new RecordingEventListener();
private final HandshakeCertificates handshakeCertificates = localhost(); private final HandshakeCertificates handshakeCertificates = localhost();
private OkHttpClient client; private OkHttpClient client = clientTestRule.newClientBuilder()
.eventListener(listener)
.build();
private SocksProxy socksProxy; private SocksProxy socksProxy;
@Before public void setUp() { @Before public void setUp() {
platform.assumeNotOpenJSSE(); platform.assumeNotOpenJSSE();
client = clientTestRule.newClientBuilder()
.eventListener(listener)
.build();
listener.forbidLock(RealConnectionPool.Companion.get(client.connectionPool())); listener.forbidLock(RealConnectionPool.Companion.get(client.connectionPool()));
listener.forbidLock(client.dispatcher()); listener.forbidLock(client.dispatcher());
} }

View File

@@ -39,7 +39,6 @@ import okio.GzipSink;
import okio.Okio; import okio.Okio;
import okio.Sink; import okio.Sink;
import okio.Source; import okio.Source;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@@ -50,13 +49,9 @@ public final class InterceptorTest {
@Rule public MockWebServer server = new MockWebServer(); @Rule public MockWebServer server = new MockWebServer();
@Rule public final OkHttpClientTestRule clientTestRule = new OkHttpClientTestRule(); @Rule public final OkHttpClientTestRule clientTestRule = new OkHttpClientTestRule();
private OkHttpClient client; private OkHttpClient client = clientTestRule.newClient();
private RecordingCallback callback = new RecordingCallback(); private RecordingCallback callback = new RecordingCallback();
@Before public void setUp() {
client = clientTestRule.newClient();
}
@Test public void applicationInterceptorsCanShortCircuitResponses() throws Exception { @Test public void applicationInterceptorsCanShortCircuitResponses() throws Exception {
server.shutdown(); // Accept no connections. server.shutdown(); // Accept no connections.

View File

@@ -36,13 +36,11 @@ class OpenJSSETest {
@JvmField @Rule var platform = PlatformRule() @JvmField @Rule var platform = PlatformRule()
@JvmField @Rule val clientTestRule = OkHttpClientTestRule() @JvmField @Rule val clientTestRule = OkHttpClientTestRule()
@JvmField @Rule val server = MockWebServer() @JvmField @Rule val server = MockWebServer()
lateinit var client: OkHttpClient var client = clientTestRule.newClient()
@Before @Before
fun setUp() { fun setUp() {
platform.assumeOpenJSSE() platform.assumeOpenJSSE()
client = clientTestRule.newClient()
} }
@Test @Test
@@ -109,4 +107,4 @@ class OpenJSSETest {
.build() .build()
server.useHttps(handshakeCertificates.sslSocketFactory(), false) server.useHttps(handshakeCertificates.sslSocketFactory(), false)
} }
} }

View File

@@ -114,12 +114,11 @@ public final class URLConnectionTest {
@Rule public final OkHttpClientTestRule clientTestRule = new OkHttpClientTestRule(); @Rule public final OkHttpClientTestRule clientTestRule = new OkHttpClientTestRule();
private HandshakeCertificates handshakeCertificates = localhost(); private HandshakeCertificates handshakeCertificates = localhost();
private OkHttpClient client; private OkHttpClient client = clientTestRule.newClient();
private @Nullable Cache cache; private @Nullable Cache cache;
@Before public void setUp() { @Before public void setUp() {
server.setProtocolNegotiationEnabled(false); server.setProtocolNegotiationEnabled(false);
client = clientTestRule.newClient();
} }
@After public void tearDown() throws Exception { @After public void tearDown() throws Exception {

View File

@@ -25,7 +25,6 @@ import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.MockWebServer;
import okhttp3.testing.Flaky; import okhttp3.testing.Flaky;
import okio.BufferedSink; import okio.BufferedSink;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@@ -39,11 +38,7 @@ public final class WholeOperationTimeoutTest {
@Rule public final MockWebServer server = new MockWebServer(); @Rule public final MockWebServer server = new MockWebServer();
@Rule public final OkHttpClientTestRule clientTestRule = new OkHttpClientTestRule(); @Rule public final OkHttpClientTestRule clientTestRule = new OkHttpClientTestRule();
private OkHttpClient client; private final OkHttpClient client = clientTestRule.newClient();
@Before public void setUp() {
client = clientTestRule.newClient();
}
@Test public void defaultConfigIsNoTimeout() throws Exception { @Test public void defaultConfigIsNoTimeout() throws Exception {
Request request = new Request.Builder() Request request = new Request.Builder()

View File

@@ -72,28 +72,23 @@ public final class WebSocketHttpTest {
private final WebSocketRecorder clientListener = new WebSocketRecorder("client"); private final WebSocketRecorder clientListener = new WebSocketRecorder("client");
private final WebSocketRecorder serverListener = new WebSocketRecorder("server"); private final WebSocketRecorder serverListener = new WebSocketRecorder("server");
private final Random random = new Random(0); private final Random random = new Random(0);
private OkHttpClient client; private OkHttpClient client = clientTestRule.newClientBuilder()
.writeTimeout(500, TimeUnit.MILLISECONDS)
.readTimeout(500, TimeUnit.MILLISECONDS)
.addInterceptor(chain -> {
Response response = chain.proceed(chain.request());
// Ensure application interceptors never see a null body.
assertThat(response.body()).isNotNull();
return response;
})
.build();
@Before public void setUp() { @Before public void setUp() {
platform.assumeNotOpenJSSE(); platform.assumeNotOpenJSSE();
client = clientTestRule.newClientBuilder()
.writeTimeout(500, TimeUnit.MILLISECONDS)
.readTimeout(500, TimeUnit.MILLISECONDS)
.addInterceptor(chain -> {
Response response = chain.proceed(chain.request());
// Ensure application interceptors never see a null body.
assertThat(response.body()).isNotNull();
return response;
})
.build();
} }
@After public void tearDown() { @After public void tearDown() {
clientListener.assertExhausted(); clientListener.assertExhausted();
// TODO: assert all connections are released once leaks are fixed
clientTestRule.abandonClient();
} }
@Test public void textMessage() { @Test public void textMessage() {