1
0
mirror of https://github.com/square/okhttp.git synced 2025-11-24 18:41:06 +03:00

Use TaskRunner in MockWebServer

The utility here is negligible. We're sharing threads between HTTP,
HTTP/2 and web sockets now.
This commit is contained in:
Jesse Wilson
2019-10-06 16:08:24 -04:00
parent b40fc526c1
commit 620be4c97b
2 changed files with 4 additions and 27 deletions

View File

@@ -27,7 +27,6 @@ import okhttp3.internal.addHeaderLenient
import okhttp3.internal.closeQuietly import okhttp3.internal.closeQuietly
import okhttp3.internal.concurrent.TaskRunner import okhttp3.internal.concurrent.TaskRunner
import okhttp3.internal.duplex.MwsDuplexAccess import okhttp3.internal.duplex.MwsDuplexAccess
import okhttp3.internal.execute
import okhttp3.internal.http.HttpMethod import okhttp3.internal.http.HttpMethod
import okhttp3.internal.http2.ErrorCode import okhttp3.internal.http2.ErrorCode
import okhttp3.internal.http2.Header import okhttp3.internal.http2.Header
@@ -81,8 +80,6 @@ import java.util.Collections
import java.util.Locale import java.util.Locale
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CountDownLatch import java.util.concurrent.CountDownLatch
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
@@ -135,7 +132,6 @@ class MockWebServer : ExternalResource(), Closeable {
private var serverSocket: ServerSocket? = null private var serverSocket: ServerSocket? = null
private var sslSocketFactory: SSLSocketFactory? = null private var sslSocketFactory: SSLSocketFactory? = null
private var executor: ExecutorService? = null
private var tunnelProxy: Boolean = false private var tunnelProxy: Boolean = false
private var clientAuth = CLIENT_AUTH_NONE private var clientAuth = CLIENT_AUTH_NONE
@@ -383,7 +379,6 @@ class MockWebServer : ExternalResource(), Closeable {
require(!started) { "start() already called" } require(!started) { "start() already called" }
started = true started = true
executor = Executors.newCachedThreadPool(threadFactory("MockWebServer", false))
this.inetSocketAddress = inetSocketAddress this.inetSocketAddress = inetSocketAddress
serverSocket = serverSocketFactory!!.createServerSocket() serverSocket = serverSocketFactory!!.createServerSocket()
@@ -393,7 +388,8 @@ class MockWebServer : ExternalResource(), Closeable {
serverSocket!!.bind(inetSocketAddress, 50) serverSocket!!.bind(inetSocketAddress, 50)
portField = serverSocket!!.localPort portField = serverSocket!!.localPort
executor!!.execute("MockWebServer $portField") {
taskRunner.newQueue().execute("MockWebServer $portField") {
try { try {
logger.info("${this@MockWebServer} starting to accept connections") logger.info("${this@MockWebServer} starting to accept connections")
acceptConnections() acceptConnections()
@@ -416,7 +412,6 @@ class MockWebServer : ExternalResource(), Closeable {
httpConnection.remove() httpConnection.remove()
} }
dispatcher.shutdown() dispatcher.shutdown()
executor!!.shutdown()
} }
} }
@@ -452,16 +447,8 @@ class MockWebServer : ExternalResource(), Closeable {
serverSocket!!.close() serverSocket!!.close()
// Await shutdown. // Await shutdown.
try {
if (!executor!!.awaitTermination(5, TimeUnit.SECONDS)) {
throw IOException("Gave up waiting for executor to shut down")
}
} catch (e: InterruptedException) {
throw AssertionError()
}
for (queue in taskRunner.activeQueues()) { for (queue in taskRunner.activeQueues()) {
if (!queue.awaitIdle(TimeUnit.MILLISECONDS.toNanos(500L))) { if (!queue.awaitIdle(TimeUnit.SECONDS.toNanos(5))) {
throw IOException("Gave up waiting for queue to shut down") throw IOException("Gave up waiting for queue to shut down")
} }
} }
@@ -477,7 +464,7 @@ class MockWebServer : ExternalResource(), Closeable {
} }
private fun serveConnection(raw: Socket) { private fun serveConnection(raw: Socket) {
executor!!.execute("MockWebServer ${raw.remoteSocketAddress}") { taskRunner.newQueue().execute("MockWebServer ${raw.remoteSocketAddress}") {
try { try {
SocketHandler(raw).handle() SocketHandler(raw).handle()
} catch (e: IOException) { } catch (e: IOException) {

View File

@@ -48,7 +48,6 @@ import java.util.Comparator
import java.util.LinkedHashMap import java.util.LinkedHashMap
import java.util.Locale import java.util.Locale
import java.util.TimeZone import java.util.TimeZone
import java.util.concurrent.Executor
import java.util.concurrent.ThreadFactory import java.util.concurrent.ThreadFactory
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.text.Charsets.UTF_32BE import kotlin.text.Charsets.UTF_32BE
@@ -383,15 +382,6 @@ inline fun threadName(name: String, block: () -> Unit) {
} }
} }
/** Execute [block], setting the executing thread's name to [name] for the duration. */
inline fun Executor.execute(name: String, crossinline block: () -> Unit) {
execute {
threadName(name) {
block()
}
}
}
fun Buffer.skipAll(b: Byte): Int { fun Buffer.skipAll(b: Byte): Int {
var count = 0 var count = 0
while (!exhausted() && this[0] == b) { while (!exhausted() && this[0] == b) {