Skip to content

Commit

Permalink
Merge pull request #5670 from square/jwilson.1230.time_to_first_byte
Browse files Browse the repository at this point in the history
Defer EventListener events until bytes are returned
  • Loading branch information
swankjesse authored Dec 30, 2019
2 parents 767fa33 + 53d2d79 commit 975be25
Show file tree
Hide file tree
Showing 9 changed files with 340 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,7 @@ class MockWebServer : ExternalResource(), Closeable {
if (byteCountNum == 0L) return
}

if (periodDelayMs != 0L) {
Thread.sleep(periodDelayMs)
}
sleepIfDelayed(periodDelayMs)
}
}

Expand Down Expand Up @@ -1047,25 +1045,25 @@ class MockWebServer : ExternalResource(), Closeable {
}
val http2Headers = mutableListOf<Header>()
val statusParts = response.status.split(' ', limit = 3)
val headersDelayMs = response.getHeadersDelay(TimeUnit.MILLISECONDS)
val bodyDelayMs = response.getBodyDelay(TimeUnit.MILLISECONDS)

if (statusParts.size < 2) {
throw AssertionError("Unexpected status: ${response.status}")
}
// TODO: constants for well-known header names.
http2Headers.add(Header(Header.RESPONSE_STATUS, statusParts[1]))
val headers = response.headers
for ((name, value) in headers) {
http2Headers.add(Header(name, value))
}
val trailers = response.trailers

sleepIfDelayed(response.getHeadersDelay(TimeUnit.MILLISECONDS))

sleepIfDelayed(headersDelayMs)
val body = response.getBody()
val outFinished = (body == null &&
response.pushPromises.isEmpty() &&
!response.isDuplex)
val flushHeaders = body == null
val flushHeaders = body == null || bodyDelayMs != 0L
require(!outFinished || trailers.size == 0) {
"unsupported: no body and non-empty trailers $trailers"
}
Expand All @@ -1076,7 +1074,7 @@ class MockWebServer : ExternalResource(), Closeable {
pushPromises(stream, request, response.pushPromises)
if (body != null) {
stream.getSink().buffer().use { sink ->
sleepIfDelayed(response.getBodyDelay(TimeUnit.MILLISECONDS))
sleepIfDelayed(bodyDelayMs)
throttledTransfer(response, socket, body, sink, body.size, false)
}
} else if (response.isDuplex) {
Expand Down
50 changes: 39 additions & 11 deletions okhttp-testing-support/src/main/java/okhttp3/CallEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,150 +22,178 @@ import java.net.Proxy

/** Data classes that correspond to each of the methods of [EventListener]. */
sealed class CallEvent {
abstract val timestampNs: Long
abstract val call: Call

val name: String
get() = javaClass.simpleName

open fun closes(): CallEvent? = null
/** Returns the open event that this close event closes, or null if this is not a close event. */
open fun closes(timestampNs: Long): CallEvent? = null

data class ProxySelectStart(
override val timestampNs: Long,
override val call: Call,
val url: HttpUrl
) : CallEvent()

data class ProxySelectEnd(
override val timestampNs: Long,
override val call: Call,
val url: HttpUrl,
val proxies: List<Proxy>?
) : CallEvent()

data class DnsStart(
override val timestampNs: Long,
override val call: Call,
val domainName: String
) : CallEvent()

data class DnsEnd(
override val timestampNs: Long,
override val call: Call,
val domainName: String,
val inetAddressList: List<InetAddress>
) : CallEvent() {
override fun closes() = DnsStart(call, domainName)
override fun closes(timestampNs: Long) = DnsStart(timestampNs, call, domainName)
}

data class ConnectStart(
override val timestampNs: Long,
override val call: Call,
val inetSocketAddress: InetSocketAddress,
val proxy: Proxy?
) : CallEvent()

data class ConnectEnd(
override val timestampNs: Long,
override val call: Call,
val inetSocketAddress: InetSocketAddress,
val proxy: Proxy?,
val protocol: Protocol?
) : CallEvent() {
override fun closes() = ConnectStart(call, inetSocketAddress, proxy)
override fun closes(timestampNs: Long) =
ConnectStart(timestampNs, call, inetSocketAddress, proxy)
}

data class ConnectFailed(
override val timestampNs: Long,
override val call: Call,
val inetSocketAddress: InetSocketAddress,
val proxy: Proxy,
val protocol: Protocol?,
val ioe: IOException
) : CallEvent() {
override fun closes() = ConnectStart(call, inetSocketAddress, proxy)
override fun closes(timestampNs: Long) =
ConnectStart(timestampNs, call, inetSocketAddress, proxy)
}

data class SecureConnectStart(
override val timestampNs: Long,
override val call: Call
) : CallEvent()

data class SecureConnectEnd(
override val timestampNs: Long,
override val call: Call,
val handshake: Handshake?
) : CallEvent() {
override fun closes() = SecureConnectStart(call)
override fun closes(timestampNs: Long) = SecureConnectStart(timestampNs, call)
}

data class ConnectionAcquired(
override val timestampNs: Long,
override val call: Call,
val connection: Connection
) : CallEvent()

data class ConnectionReleased(
override val timestampNs: Long,
override val call: Call,
val connection: Connection
) : CallEvent() {
override fun closes() = ConnectionAcquired(call, connection)
override fun closes(timestampNs: Long) = ConnectionAcquired(timestampNs, call, connection)
}

data class CallStart(
override val timestampNs: Long,
override val call: Call
) : CallEvent()

data class CallEnd(
override val timestampNs: Long,
override val call: Call
) : CallEvent() {
override fun closes() = CallStart(call)
override fun closes(timestampNs: Long) = CallStart(timestampNs, call)
}

data class CallFailed(
override val timestampNs: Long,
override val call: Call,
val ioe: IOException
) : CallEvent()

data class RequestHeadersStart(
override val timestampNs: Long,
override val call: Call
) : CallEvent()

data class RequestHeadersEnd(
override val timestampNs: Long,
override val call: Call,
val headerLength: Long
) : CallEvent() {
override fun closes() = RequestHeadersStart(call)
override fun closes(timestampNs: Long) = RequestHeadersStart(timestampNs, call)
}

data class RequestBodyStart(
override val timestampNs: Long,
override val call: Call
) : CallEvent()

data class RequestBodyEnd(
override val timestampNs: Long,
override val call: Call,
val bytesWritten: Long
) : CallEvent() {
override fun closes() = RequestBodyStart(call)
override fun closes(timestampNs: Long) = RequestBodyStart(timestampNs, call)
}

data class RequestFailed(
override val timestampNs: Long,
override val call: Call,
val ioe: IOException
) : CallEvent()

data class ResponseHeadersStart(
override val timestampNs: Long,
override val call: Call
) : CallEvent()

data class ResponseHeadersEnd(
override val timestampNs: Long,
override val call: Call,
val headerLength: Long
) : CallEvent() {
override fun closes() = RequestHeadersStart(call)
override fun closes(timestampNs: Long) = RequestHeadersStart(timestampNs, call)
}

data class ResponseBodyStart(
override val timestampNs: Long,
override val call: Call
) : CallEvent()

data class ResponseBodyEnd(
override val timestampNs: Long,
override val call: Call,
val bytesRead: Long
) : CallEvent() {
override fun closes() = ResponseBodyStart(call)
override fun closes(timestampNs: Long) = ResponseBodyStart(timestampNs, call)
}

data class ResponseFailed(
override val timestampNs: Long,
override val call: Call,
val ioe: IOException
) : CallEvent()
Expand Down
Loading

0 comments on commit 975be25

Please sign in to comment.