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

Convert RecordingEventListener to Kotlin

This commit is contained in:
Jesse Wilson
2019-12-29 17:06:54 -05:00
parent f7b10c2ae5
commit d52e2d34d6
8 changed files with 480 additions and 553 deletions

View File

@@ -8,3 +8,7 @@ dependencies {
compileOnly deps.jsr305 compileOnly deps.jsr305
} }
animalsniffer {
ignoreFailures = true
}

View File

@@ -0,0 +1,172 @@
/*
* Copyright (C) 2017 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3
import java.io.IOException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.Proxy
/** Data classes that correspond to each of the methods of [EventListener]. */
sealed class CallEvent {
abstract val call: Call
val name: String
get() = javaClass.simpleName
open fun closes(): CallEvent? = null
data class ProxySelectStart(
override val call: Call,
val url: HttpUrl
) : CallEvent()
data class ProxySelectEnd(
override val call: Call,
val url: HttpUrl,
val proxies: List<Proxy>?
) : CallEvent()
data class DnsStart(
override val call: Call,
val domainName: String
) : CallEvent()
data class DnsEnd(
override val call: Call,
val domainName: String,
val inetAddressList: List<InetAddress>
) : CallEvent() {
override fun closes() = DnsStart(call, domainName)
}
data class ConnectStart(
override val call: Call,
val inetSocketAddress: InetSocketAddress,
val proxy: Proxy?
) : CallEvent()
data class ConnectEnd(
override val call: Call,
val inetSocketAddress: InetSocketAddress,
val proxy: Proxy?,
val protocol: Protocol?
) : CallEvent() {
override fun closes() = ConnectStart(call, inetSocketAddress, proxy)
}
data class ConnectFailed(
override val call: Call,
val inetSocketAddress: InetSocketAddress,
val proxy: Proxy,
val protocol: Protocol?,
val ioe: IOException
) : CallEvent() {
override fun closes() = ConnectStart(call, inetSocketAddress, proxy)
}
data class SecureConnectStart(
override val call: Call
) : CallEvent()
data class SecureConnectEnd(
override val call: Call,
val handshake: Handshake?
) : CallEvent() {
override fun closes() = SecureConnectStart(call)
}
data class ConnectionAcquired(
override val call: Call,
val connection: Connection
) : CallEvent()
data class ConnectionReleased(
override val call: Call,
val connection: Connection
) : CallEvent() {
override fun closes() = ConnectionAcquired(call, connection)
}
data class CallStart(
override val call: Call
) : CallEvent()
data class CallEnd(
override val call: Call
) : CallEvent() {
override fun closes() = CallStart(call)
}
data class CallFailed(
override val call: Call,
val ioe: IOException
) : CallEvent()
data class RequestHeadersStart(
override val call: Call
) : CallEvent()
data class RequestHeadersEnd(
override val call: Call,
val headerLength: Long
) : CallEvent() {
override fun closes() = RequestHeadersStart(call)
}
data class RequestBodyStart(
override val call: Call
) : CallEvent()
data class RequestBodyEnd(
override val call: Call,
val bytesWritten: Long
) : CallEvent() {
override fun closes() = RequestBodyStart(call)
}
data class RequestFailed(
override val call: Call,
val ioe: IOException
) : CallEvent()
data class ResponseHeadersStart(
override val call: Call
) : CallEvent()
data class ResponseHeadersEnd(
override val call: Call,
val headerLength: Long
) : CallEvent() {
override fun closes() = RequestHeadersStart(call)
}
data class ResponseBodyStart(
override val call: Call
) : CallEvent()
data class ResponseBodyEnd(
override val call: Call,
val bytesRead: Long
) : CallEvent() {
override fun closes() = ResponseBodyStart(call)
}
data class ResponseFailed(
override val call: Call,
val ioe: IOException
) : CallEvent()
}

View File

@@ -1,470 +0,0 @@
/*
* Copyright (C) 2017 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import javax.annotation.Nullable;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
public class RecordingEventListener extends EventListener {
final Deque<CallEvent> eventSequence = new ConcurrentLinkedDeque<>();
final List<Object> forbiddenLocks = new ArrayList<>();
/** Confirm that the thread does not hold a lock on {@code lock} during the callback. */
public void forbidLock(Object lock) {
forbiddenLocks.add(lock);
}
/**
* Removes recorded events up to (and including) an event is found whose class equals
* {@code eventClass} and returns it.
*/
public <T> T removeUpToEvent(Class<T> eventClass) {
List<CallEvent> fullEventSequence = new ArrayList<>(eventSequence);
Object event = eventSequence.poll();
while (event != null && !eventClass.isInstance(event)) {
event = eventSequence.poll();
}
if (event == null) {
throw new AssertionError(
eventClass.getSimpleName() + " not found. Found " + fullEventSequence + ".");
}
return eventClass.cast(event);
}
public List<String> recordedEventTypes() {
List<String> eventTypes = new ArrayList<>();
for (CallEvent event : eventSequence) {
eventTypes.add(event.getName());
}
return eventTypes;
}
public void clearAllEvents() {
eventSequence.clear();
}
private void logEvent(CallEvent e) {
for (Object lock : forbiddenLocks) {
assertThat(Thread.holdsLock(lock)).overridingErrorMessage(lock.toString()).isFalse();
}
CallEvent startEvent = e.closes();
if (startEvent != null) {
assertTrue(eventSequence.contains(startEvent));
}
eventSequence.offer(e);
}
@Override public void proxySelectStart(Call call, HttpUrl url) {
logEvent(new ProxySelectStart(call, url));
}
@Override public void proxySelectEnd(Call call, HttpUrl url,
List<Proxy> proxies) {
logEvent(new ProxySelectEnd(call, url, proxies));
}
@Override public void dnsStart(Call call, String domainName) {
logEvent(new DnsStart(call, domainName));
}
@Override public void dnsEnd(Call call, String domainName, List<InetAddress> inetAddressList) {
logEvent(new DnsEnd(call, domainName, inetAddressList));
}
@Override public void connectStart(Call call, InetSocketAddress inetSocketAddress,
Proxy proxy) {
logEvent(new ConnectStart(call, inetSocketAddress, proxy));
}
@Override public void secureConnectStart(Call call) {
logEvent(new SecureConnectStart(call));
}
@Override public void secureConnectEnd(Call call, Handshake handshake) {
logEvent(new SecureConnectEnd(call, handshake));
}
@Override public void connectEnd(Call call, InetSocketAddress inetSocketAddress,
@Nullable Proxy proxy, Protocol protocol) {
logEvent(new ConnectEnd(call, inetSocketAddress, proxy, protocol));
}
@Override public void connectFailed(Call call, InetSocketAddress inetSocketAddress, Proxy proxy,
@Nullable Protocol protocol, IOException ioe) {
logEvent(new ConnectFailed(call, inetSocketAddress, proxy, protocol, ioe));
}
@Override public void connectionAcquired(Call call, Connection connection) {
logEvent(new ConnectionAcquired(call, connection));
}
@Override public void connectionReleased(Call call, Connection connection) {
logEvent(new ConnectionReleased(call, connection));
}
@Override public void callStart(Call call) {
logEvent(new CallStart(call));
}
@Override public void requestHeadersStart(Call call) {
logEvent(new RequestHeadersStart(call));
}
@Override public void requestHeadersEnd(Call call, Request request) {
logEvent(new RequestHeadersEnd(call, request.headers().byteCount()));
}
@Override public void requestBodyStart(Call call) {
logEvent(new RequestBodyStart(call));
}
@Override public void requestBodyEnd(Call call, long byteCount) {
logEvent(new RequestBodyEnd(call, byteCount));
}
@Override public void requestFailed(Call call, IOException ioe) {
logEvent(new RequestFailed(call, ioe));
}
@Override public void responseHeadersStart(Call call) {
logEvent(new ResponseHeadersStart(call));
}
@Override public void responseHeadersEnd(Call call, Response response) {
logEvent(new ResponseHeadersEnd(call, response.headers().byteCount()));
}
@Override public void responseBodyStart(Call call) {
logEvent(new ResponseBodyStart(call));
}
@Override public void responseBodyEnd(Call call, long byteCount) {
logEvent(new ResponseBodyEnd(call, byteCount));
}
@Override public void responseFailed(Call call, IOException ioe) {
logEvent(new ResponseFailed(call, ioe));
}
@Override public void callEnd(Call call) {
logEvent(new CallEnd(call));
}
@Override public void callFailed(Call call, IOException ioe) {
logEvent(new CallFailed(call, ioe));
}
static class CallEvent {
final Call call;
final List<Object> params;
CallEvent(Call call, Object... params) {
this.call = call;
this.params = asList(params);
}
public String getName() {
return getClass().getSimpleName();
}
@Override public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CallEvent)) return false;
CallEvent callEvent = (CallEvent) o;
if (!getName().equals(callEvent.getName())) return false;
if (!call.equals(callEvent.call)) return false;
return params.equals(callEvent.params);
}
@Override public int hashCode() {
int result = call.hashCode();
result = 31 * result + getName().hashCode();
result = 31 * result + params.hashCode();
return result;
}
public @Nullable CallEvent closes() {
return null;
}
}
static final class ProxySelectStart extends CallEvent {
final HttpUrl url;
ProxySelectStart(Call call, HttpUrl url) {
super(call, url);
this.url = url;
}
}
static final class ProxySelectEnd extends CallEvent {
final HttpUrl url;
ProxySelectEnd(Call call, HttpUrl url, List<Proxy> proxies) {
super(call, url, proxies);
this.url = url;
}
}
static final class DnsStart extends CallEvent {
final String domainName;
DnsStart(Call call, String domainName) {
super(call, domainName);
this.domainName = domainName;
}
}
static final class DnsEnd extends CallEvent {
final String domainName;
final List<InetAddress> inetAddressList;
DnsEnd(Call call, String domainName, List<InetAddress> inetAddressList) {
super(call, domainName, inetAddressList);
this.domainName = domainName;
this.inetAddressList = inetAddressList;
}
@Override public @Nullable CallEvent closes() {
return new DnsStart(call, domainName);
}
}
static final class ConnectStart extends CallEvent {
final InetSocketAddress inetSocketAddress;
final Proxy proxy;
ConnectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) {
super(call, inetSocketAddress, proxy);
this.inetSocketAddress = inetSocketAddress;
this.proxy = proxy;
}
}
static final class ConnectEnd extends CallEvent {
final InetSocketAddress inetSocketAddress;
final Protocol protocol;
final Proxy proxy;
ConnectEnd(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol) {
super(call, inetSocketAddress, proxy, protocol);
this.inetSocketAddress = inetSocketAddress;
this.proxy = proxy;
this.protocol = protocol;
}
@Override public CallEvent closes() {
return new ConnectStart(call, inetSocketAddress, proxy);
}
}
static final class ConnectFailed extends CallEvent {
final InetSocketAddress inetSocketAddress;
final Protocol protocol;
final Proxy proxy;
final IOException ioe;
ConnectFailed(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol,
IOException ioe) {
super(call, inetSocketAddress, proxy, protocol, ioe);
this.inetSocketAddress = inetSocketAddress;
this.proxy = proxy;
this.protocol = protocol;
this.ioe = ioe;
}
@Override public @Nullable CallEvent closes() {
return new ConnectStart(call, inetSocketAddress, proxy);
}
}
static final class SecureConnectStart extends CallEvent {
SecureConnectStart(Call call) {
super(call);
}
}
static final class SecureConnectEnd extends CallEvent {
final Handshake handshake;
SecureConnectEnd(Call call, Handshake handshake) {
super(call, handshake);
this.handshake = handshake;
}
@Override public @Nullable CallEvent closes() {
return new SecureConnectStart(call);
}
}
static final class ConnectionAcquired extends CallEvent {
final Connection connection;
ConnectionAcquired(Call call, Connection connection) {
super(call, connection);
this.connection = connection;
}
}
static final class ConnectionReleased extends CallEvent {
final Connection connection;
ConnectionReleased(Call call, Connection connection) {
super(call, connection);
this.connection = connection;
}
@Override public @Nullable CallEvent closes() {
return new ConnectionAcquired(call, connection);
}
}
static final class CallStart extends CallEvent {
CallStart(Call call) {
super(call);
}
}
static final class CallEnd extends CallEvent {
CallEnd(Call call) {
super(call);
}
@Override public @Nullable CallEvent closes() {
return new CallStart(call);
}
}
static final class CallFailed extends CallEvent {
final IOException ioe;
CallFailed(Call call, IOException ioe) {
super(call, ioe);
this.ioe = ioe;
}
}
static final class RequestHeadersStart extends CallEvent {
RequestHeadersStart(Call call) {
super(call);
}
}
static final class RequestHeadersEnd extends CallEvent {
final long headerLength;
RequestHeadersEnd(Call call, long headerLength) {
super(call, headerLength);
this.headerLength = headerLength;
}
@Override public @Nullable CallEvent closes() {
return new RequestHeadersStart(call);
}
}
static final class RequestBodyStart extends CallEvent {
RequestBodyStart(Call call) {
super(call);
}
}
static final class RequestBodyEnd extends CallEvent {
final long bytesWritten;
RequestBodyEnd(Call call, long bytesWritten) {
super(call, bytesWritten);
this.bytesWritten = bytesWritten;
}
@Override public @Nullable CallEvent closes() {
return new RequestBodyStart(call);
}
}
static final class RequestFailed extends CallEvent {
final IOException ioe;
RequestFailed(Call call, IOException ioe) {
super(call, ioe);
this.ioe = ioe;
}
}
static final class ResponseHeadersStart extends CallEvent {
ResponseHeadersStart(Call call) {
super(call);
}
}
static final class ResponseHeadersEnd extends CallEvent {
final long headerLength;
ResponseHeadersEnd(Call call, long headerLength) {
super(call, headerLength);
this.headerLength = headerLength;
}
@Override public @Nullable CallEvent closes() {
return new RequestHeadersStart(call);
}
}
static final class ResponseBodyStart extends CallEvent {
ResponseBodyStart(Call call) {
super(call);
}
}
static final class ResponseBodyEnd extends CallEvent {
final long bytesRead;
ResponseBodyEnd(Call call, long bytesRead) {
super(call, bytesRead);
this.bytesRead = bytesRead;
}
@Override public @Nullable CallEvent closes() {
return new ResponseBodyStart(call);
}
}
static final class ResponseFailed extends CallEvent {
final IOException ioe;
ResponseFailed(Call call, IOException ioe) {
super(call, ioe);
this.ioe = ioe;
}
}
}

View File

@@ -0,0 +1,218 @@
/*
* Copyright (C) 2017 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3
import okhttp3.CallEvent.CallEnd
import okhttp3.CallEvent.CallFailed
import okhttp3.CallEvent.CallStart
import okhttp3.CallEvent.ConnectEnd
import okhttp3.CallEvent.ConnectFailed
import okhttp3.CallEvent.ConnectStart
import okhttp3.CallEvent.ConnectionAcquired
import okhttp3.CallEvent.ConnectionReleased
import okhttp3.CallEvent.DnsEnd
import okhttp3.CallEvent.DnsStart
import okhttp3.CallEvent.ProxySelectEnd
import okhttp3.CallEvent.ProxySelectStart
import okhttp3.CallEvent.RequestBodyEnd
import okhttp3.CallEvent.RequestBodyStart
import okhttp3.CallEvent.RequestFailed
import okhttp3.CallEvent.RequestHeadersEnd
import okhttp3.CallEvent.RequestHeadersStart
import okhttp3.CallEvent.ResponseBodyEnd
import okhttp3.CallEvent.ResponseBodyStart
import okhttp3.CallEvent.ResponseFailed
import okhttp3.CallEvent.ResponseHeadersEnd
import okhttp3.CallEvent.ResponseHeadersStart
import okhttp3.CallEvent.SecureConnectEnd
import okhttp3.CallEvent.SecureConnectStart
import org.assertj.core.api.Assertions.assertThat
import org.junit.Assert.assertTrue
import java.io.IOException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.Proxy
import java.util.Deque
import java.util.concurrent.ConcurrentLinkedDeque
open class RecordingEventListener : EventListener() {
val eventSequence: Deque<CallEvent> = ConcurrentLinkedDeque()
private val forbiddenLocks = mutableListOf<Any>()
/** Confirm that the thread does not hold a lock on `lock` during the callback. */
fun forbidLock(lock: Any) {
forbiddenLocks.add(lock)
}
/**
* Removes recorded events up to (and including) an event is found whose class equals [eventClass]
* and returns it.
*/
fun <T> removeUpToEvent(eventClass: Class<T>): T {
val fullEventSequence = eventSequence.toMutableList()
var event = eventSequence.poll()
while (event != null && !eventClass.isInstance(event)) {
event = eventSequence.poll()
}
if (event == null) {
throw AssertionError("${eventClass.simpleName} not found. Found $fullEventSequence.")
}
return eventClass.cast(event)
}
fun recordedEventTypes() = eventSequence.map { it.name }
fun clearAllEvents() {
eventSequence.clear()
}
private fun logEvent(e: CallEvent) {
for (lock in forbiddenLocks) {
assertThat(Thread.holdsLock(lock))
.overridingErrorMessage(lock.toString())
.isFalse()
}
val startEvent = e.closes()
if (startEvent != null) {
assertTrue(eventSequence.contains(startEvent))
}
eventSequence.offer(e)
}
override fun proxySelectStart(
call: Call,
url: HttpUrl
) = logEvent(ProxySelectStart(call, url))
override fun proxySelectEnd(
call: Call,
url: HttpUrl,
proxies: List<Proxy>
) = logEvent(ProxySelectEnd(call, url, proxies))
override fun dnsStart(
call: Call,
domainName: String
) = logEvent(DnsStart(call, domainName))
override fun dnsEnd(
call: Call,
domainName: String,
inetAddressList: List<InetAddress>
) = logEvent(DnsEnd(call, domainName, inetAddressList))
override fun connectStart(
call: Call,
inetSocketAddress: InetSocketAddress,
proxy: Proxy
) = logEvent(ConnectStart(call, inetSocketAddress, proxy))
override fun secureConnectStart(
call: Call
) = logEvent(SecureConnectStart(call))
override fun secureConnectEnd(
call: Call,
handshake: Handshake?
) = logEvent(SecureConnectEnd(call, handshake))
override fun connectEnd(
call: Call,
inetSocketAddress: InetSocketAddress,
proxy: Proxy,
protocol: Protocol?
) = logEvent(ConnectEnd(call, inetSocketAddress, proxy, protocol))
override fun connectFailed(
call: Call,
inetSocketAddress: InetSocketAddress,
proxy: Proxy,
protocol: Protocol?,
ioe: IOException
) = logEvent(ConnectFailed(call, inetSocketAddress, proxy, protocol, ioe))
override fun connectionAcquired(
call: Call,
connection: Connection
) = logEvent(ConnectionAcquired(call, connection))
override fun connectionReleased(
call: Call,
connection: Connection
) = logEvent(ConnectionReleased(call, connection))
override fun callStart(
call: Call
) = logEvent(CallStart(call))
override fun requestHeadersStart(
call: Call
) = logEvent(RequestHeadersStart(call))
override fun requestHeadersEnd(
call: Call,
request: Request
) = logEvent(RequestHeadersEnd(call, request.headers.byteCount()))
override fun requestBodyStart(
call: Call
) = logEvent(RequestBodyStart(call))
override fun requestBodyEnd(
call: Call,
byteCount: Long
) = logEvent(RequestBodyEnd(call, byteCount))
override fun requestFailed(
call: Call,
ioe: IOException
) = logEvent(RequestFailed(call, ioe))
override fun responseHeadersStart(
call: Call
) = logEvent(ResponseHeadersStart(call))
override fun responseHeadersEnd(
call: Call,
response: Response
) = logEvent(ResponseHeadersEnd(call, response.headers.byteCount()))
override fun responseBodyStart(
call: Call
) = logEvent(ResponseBodyStart(call))
override fun responseBodyEnd(
call: Call,
byteCount: Long
) = logEvent(ResponseBodyEnd(call, byteCount))
override fun responseFailed(
call: Call,
ioe: IOException
) = logEvent(ResponseFailed(call, ioe))
override fun callEnd(
call: Call
) = logEvent(CallEnd(call))
override fun callFailed(
call: Call,
ioe: IOException
) = logEvent(CallFailed(call, ioe))
}

View File

@@ -54,10 +54,11 @@ import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLProtocolException; import javax.net.ssl.SSLProtocolException;
import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import okhttp3.RecordingEventListener.CallEnd; import okhttp3.CallEvent.CallEnd;
import okhttp3.RecordingEventListener.ConnectionAcquired; import okhttp3.CallEvent.ConnectStart;
import okhttp3.RecordingEventListener.ConnectionReleased; import okhttp3.CallEvent.ConnectionAcquired;
import okhttp3.RecordingEventListener.ResponseFailed; import okhttp3.CallEvent.ConnectionReleased;
import okhttp3.CallEvent.ResponseFailed;
import okhttp3.internal.DoubleInetAddressDns; import okhttp3.internal.DoubleInetAddressDns;
import okhttp3.internal.RecordingOkAuthenticator; import okhttp3.internal.RecordingOkAuthenticator;
import okhttp3.internal.Version; import okhttp3.internal.Version;
@@ -2988,7 +2989,9 @@ public final class CallTest {
assertThat(response.body().string()).isNotBlank(); assertThat(response.body().string()).isNotBlank();
} }
long connectCount = listener.eventSequence.stream().filter((event) -> event instanceof RecordingEventListener.ConnectStart).count(); long connectCount = listener.getEventSequence().stream()
.filter((event) -> event instanceof ConnectStart)
.count();
long expected = platform.isJdk8() ? 2 : 1; long expected = platform.isJdk8() ? 2 : 1;
assertThat(connectCount).isEqualTo(expected); assertThat(connectCount).isEqualTo(expected);
} }

View File

@@ -279,8 +279,8 @@ public final class DispatcherTest {
dispatcher.setMaxRequests(2); // Trigger promotion. dispatcher.setMaxRequests(2); // Trigger promotion.
callback.await(request2.url()).assertFailure(InterruptedIOException.class); callback.await(request2.url()).assertFailure(InterruptedIOException.class);
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "CallStart", assertThat(listener.recordedEventTypes())
"CallFailed"); .containsExactly("CallStart", "CallStart", "CallFailed");
} }
@Test public void executionRejectedAfterMaxRequestsPerHostChange() throws Exception { @Test public void executionRejectedAfterMaxRequestsPerHostChange() throws Exception {
@@ -292,8 +292,8 @@ public final class DispatcherTest {
client.newCall(request2).enqueue(callback); client.newCall(request2).enqueue(callback);
dispatcher.setMaxRequestsPerHost(2); // Trigger promotion. dispatcher.setMaxRequestsPerHost(2); // Trigger promotion.
callback.await(request2.url()).assertFailure(InterruptedIOException.class); callback.await(request2.url()).assertFailure(InterruptedIOException.class);
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "CallStart", assertThat(listener.recordedEventTypes())
"CallFailed"); .containsExactly("CallStart", "CallStart", "CallFailed");
} }
@Test public void executionRejectedAfterPrecedingCallFinishes() throws Exception { @Test public void executionRejectedAfterPrecedingCallFinishes() throws Exception {
@@ -305,8 +305,8 @@ public final class DispatcherTest {
client.newCall(request2).enqueue(callback); client.newCall(request2).enqueue(callback);
executor.finishJob("http://a/1"); // Trigger promotion. executor.finishJob("http://a/1"); // Trigger promotion.
callback.await(request2.url()).assertFailure(InterruptedIOException.class); callback.await(request2.url()).assertFailure(InterruptedIOException.class);
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "CallStart", assertThat(listener.recordedEventTypes())
"CallFailed"); .containsExactly("CallStart", "CallStart", "CallFailed");
} }
private Thread makeSynchronousCall(Call call) { private Thread makeSynchronousCall(Call call) {

View File

@@ -26,21 +26,21 @@ import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import okhttp3.RecordingEventListener.CallEnd; import okhttp3.CallEvent.CallEnd;
import okhttp3.RecordingEventListener.CallFailed; import okhttp3.CallEvent.CallFailed;
import okhttp3.RecordingEventListener.ConnectEnd; import okhttp3.CallEvent.ConnectEnd;
import okhttp3.RecordingEventListener.ConnectFailed; import okhttp3.CallEvent.ConnectFailed;
import okhttp3.RecordingEventListener.ConnectStart; import okhttp3.CallEvent.ConnectStart;
import okhttp3.RecordingEventListener.ConnectionAcquired; import okhttp3.CallEvent.ConnectionAcquired;
import okhttp3.RecordingEventListener.DnsEnd; import okhttp3.CallEvent.DnsEnd;
import okhttp3.RecordingEventListener.DnsStart; import okhttp3.CallEvent.DnsStart;
import okhttp3.RecordingEventListener.RequestBodyEnd; import okhttp3.CallEvent.RequestBodyEnd;
import okhttp3.RecordingEventListener.RequestHeadersEnd; import okhttp3.CallEvent.RequestHeadersEnd;
import okhttp3.RecordingEventListener.ResponseBodyEnd; import okhttp3.CallEvent.ResponseBodyEnd;
import okhttp3.RecordingEventListener.ResponseFailed; import okhttp3.CallEvent.ResponseFailed;
import okhttp3.RecordingEventListener.ResponseHeadersEnd; import okhttp3.CallEvent.ResponseHeadersEnd;
import okhttp3.RecordingEventListener.SecureConnectEnd; import okhttp3.CallEvent.SecureConnectEnd;
import okhttp3.RecordingEventListener.SecureConnectStart; import okhttp3.CallEvent.SecureConnectStart;
import okhttp3.internal.DoubleInetAddressDns; import okhttp3.internal.DoubleInetAddressDns;
import okhttp3.internal.RecordingOkAuthenticator; import okhttp3.internal.RecordingOkAuthenticator;
import okhttp3.internal.connection.RealConnectionPool; import okhttp3.internal.connection.RealConnectionPool;
@@ -202,7 +202,7 @@ public final class EventListenerTest {
"RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart",
"ResponseFailed", "ConnectionReleased", "CallFailed"); "ResponseFailed", "ConnectionReleased", "CallFailed");
ResponseFailed responseFailed = listener.removeUpToEvent(ResponseFailed.class); ResponseFailed responseFailed = listener.removeUpToEvent(ResponseFailed.class);
assertThat(responseFailed.ioe.getMessage()).isEqualTo("unexpected end of stream"); assertThat(responseFailed.getIoe().getMessage()).isEqualTo("unexpected end of stream");
} }
@Test public void canceledCallEventSequence() { @Test public void canceledCallEventSequence() {
@@ -270,7 +270,7 @@ public final class EventListenerTest {
if (requestHeaderLength != null) { if (requestHeaderLength != null) {
RequestHeadersEnd responseHeadersEnd = listener.removeUpToEvent(RequestHeadersEnd.class); RequestHeadersEnd responseHeadersEnd = listener.removeUpToEvent(RequestHeadersEnd.class);
Assert.assertThat("request header length", responseHeadersEnd.headerLength, Assert.assertThat("request header length", responseHeadersEnd.getHeaderLength(),
requestHeaderLength); requestHeaderLength);
} else { } else {
assertThat(listener.recordedEventTypes()).doesNotContain("RequestHeadersEnd"); assertThat(listener.recordedEventTypes()).doesNotContain("RequestHeadersEnd");
@@ -278,14 +278,14 @@ public final class EventListenerTest {
if (requestBodyBytes != null) { if (requestBodyBytes != null) {
RequestBodyEnd responseBodyEnd = listener.removeUpToEvent(RequestBodyEnd.class); RequestBodyEnd responseBodyEnd = listener.removeUpToEvent(RequestBodyEnd.class);
Assert.assertThat("request body bytes", responseBodyEnd.bytesWritten, requestBodyBytes); Assert.assertThat("request body bytes", responseBodyEnd.getBytesWritten(), requestBodyBytes);
} else { } else {
assertThat(listener.recordedEventTypes()).doesNotContain("RequestBodyEnd"); assertThat(listener.recordedEventTypes()).doesNotContain("RequestBodyEnd");
} }
if (responseHeaderLength != null) { if (responseHeaderLength != null) {
ResponseHeadersEnd responseHeadersEnd = listener.removeUpToEvent(ResponseHeadersEnd.class); ResponseHeadersEnd responseHeadersEnd = listener.removeUpToEvent(ResponseHeadersEnd.class);
Assert.assertThat("response header length", responseHeadersEnd.headerLength, Assert.assertThat("response header length", responseHeadersEnd.getHeaderLength(),
responseHeaderLength); responseHeaderLength);
} else { } else {
assertThat(listener.recordedEventTypes()).doesNotContain("ResponseHeadersEnd"); assertThat(listener.recordedEventTypes()).doesNotContain("ResponseHeadersEnd");
@@ -293,7 +293,7 @@ public final class EventListenerTest {
if (responseBodyBytes != null) { if (responseBodyBytes != null) {
ResponseBodyEnd responseBodyEnd = listener.removeUpToEvent(ResponseBodyEnd.class); ResponseBodyEnd responseBodyEnd = listener.removeUpToEvent(ResponseBodyEnd.class);
Assert.assertThat("response body bytes", responseBodyEnd.bytesRead, responseBodyBytes); Assert.assertThat("response body bytes", responseBodyEnd.getBytesRead(), responseBodyBytes);
} else { } else {
assertThat(listener.recordedEventTypes()).doesNotContain("ResponseBodyEnd"); assertThat(listener.recordedEventTypes()).doesNotContain("ResponseBodyEnd");
} }
@@ -381,13 +381,13 @@ public final class EventListenerTest {
response.body().close(); response.body().close();
DnsStart dnsStart = listener.removeUpToEvent(DnsStart.class); DnsStart dnsStart = listener.removeUpToEvent(DnsStart.class);
assertThat(dnsStart.call).isSameAs(call); assertThat(dnsStart.getCall()).isSameAs(call);
assertThat(dnsStart.domainName).isEqualTo(server.getHostName()); assertThat(dnsStart.getDomainName()).isEqualTo(server.getHostName());
DnsEnd dnsEnd = listener.removeUpToEvent(DnsEnd.class); DnsEnd dnsEnd = listener.removeUpToEvent(DnsEnd.class);
assertThat(dnsEnd.call).isSameAs(call); assertThat(dnsEnd.getCall()).isSameAs(call);
assertThat(dnsEnd.domainName).isEqualTo(server.getHostName()); assertThat(dnsEnd.getDomainName()).isEqualTo(server.getHostName());
assertThat(dnsEnd.inetAddressList.size()).isEqualTo(1); assertThat(dnsEnd.getInetAddressList().size()).isEqualTo(1);
} }
@Test public void noDnsLookupOnPooledConnection() throws IOException { @Test public void noDnsLookupOnPooledConnection() throws IOException {
@@ -459,8 +459,8 @@ public final class EventListenerTest {
listener.removeUpToEvent(DnsStart.class); listener.removeUpToEvent(DnsStart.class);
CallFailed callFailed = listener.removeUpToEvent(CallFailed.class); CallFailed callFailed = listener.removeUpToEvent(CallFailed.class);
assertThat(callFailed.call).isSameAs(call); assertThat(callFailed.getCall()).isSameAs(call);
assertThat(callFailed.ioe).isInstanceOf(UnknownHostException.class); assertThat(callFailed.getIoe()).isInstanceOf(UnknownHostException.class);
} }
@Test public void emptyDnsLookup() { @Test public void emptyDnsLookup() {
@@ -481,8 +481,8 @@ public final class EventListenerTest {
listener.removeUpToEvent(DnsStart.class); listener.removeUpToEvent(DnsStart.class);
CallFailed callFailed = listener.removeUpToEvent(CallFailed.class); CallFailed callFailed = listener.removeUpToEvent(CallFailed.class);
assertThat(callFailed.call).isSameAs(call); assertThat(callFailed.getCall()).isSameAs(call);
assertThat(callFailed.ioe).isInstanceOf(UnknownHostException.class); assertThat(callFailed.getIoe()).isInstanceOf(UnknownHostException.class);
} }
@Test public void successfulConnect() throws IOException { @Test public void successfulConnect() throws IOException {
@@ -499,14 +499,14 @@ public final class EventListenerTest {
InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort()); InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort());
ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class); ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class);
assertThat(connectStart.call).isSameAs(call); assertThat(connectStart.getCall()).isSameAs(call);
assertThat(connectStart.inetSocketAddress).isEqualTo(expectedAddress); assertThat(connectStart.getInetSocketAddress()).isEqualTo(expectedAddress);
assertThat(connectStart.proxy).isEqualTo(Proxy.NO_PROXY); assertThat(connectStart.getProxy()).isEqualTo(Proxy.NO_PROXY);
ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class); ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class);
assertThat(connectEnd.call).isSameAs(call); assertThat(connectEnd.getCall()).isSameAs(call);
assertThat(connectEnd.inetSocketAddress).isEqualTo(expectedAddress); assertThat(connectEnd.getInetSocketAddress()).isEqualTo(expectedAddress);
assertThat(connectEnd.protocol).isEqualTo(Protocol.HTTP_1_1); assertThat(connectEnd.getProtocol()).isEqualTo(Protocol.HTTP_1_1);
} }
@Test public void failedConnect() throws UnknownHostException { @Test public void failedConnect() throws UnknownHostException {
@@ -527,15 +527,15 @@ public final class EventListenerTest {
InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort()); InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort());
ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class); ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class);
assertThat(connectStart.call).isSameAs(call); assertThat(connectStart.getCall()).isSameAs(call);
assertThat(connectStart.inetSocketAddress).isEqualTo(expectedAddress); assertThat(connectStart.getInetSocketAddress()).isEqualTo(expectedAddress);
assertThat(connectStart.proxy).isEqualTo(Proxy.NO_PROXY); assertThat(connectStart.getProxy()).isEqualTo(Proxy.NO_PROXY);
ConnectFailed connectFailed = listener.removeUpToEvent(ConnectFailed.class); ConnectFailed connectFailed = listener.removeUpToEvent(ConnectFailed.class);
assertThat(connectFailed.call).isSameAs(call); assertThat(connectFailed.getCall()).isSameAs(call);
assertThat(connectFailed.inetSocketAddress).isEqualTo(expectedAddress); assertThat(connectFailed.getInetSocketAddress()).isEqualTo(expectedAddress);
assertThat(connectFailed.protocol).isNull(); assertThat(connectFailed.getProtocol()).isNull();
assertThat(connectFailed.ioe).isNotNull(); assertThat(connectFailed.getIoe()).isNotNull();
} }
@Test public void multipleConnectsForSingleCall() throws IOException { @Test public void multipleConnectsForSingleCall() throws IOException {
@@ -579,14 +579,14 @@ public final class EventListenerTest {
InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort()); InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort());
ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class); ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class);
assertThat(connectStart.call).isSameAs(call); assertThat(connectStart.getCall()).isSameAs(call);
assertThat(connectStart.inetSocketAddress).isEqualTo(expectedAddress); assertThat(connectStart.getInetSocketAddress()).isEqualTo(expectedAddress);
assertThat(connectStart.proxy).isEqualTo(server.toProxyAddress()); assertThat(connectStart.getProxy()).isEqualTo(server.toProxyAddress());
ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class); ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class);
assertThat(connectEnd.call).isSameAs(call); assertThat(connectEnd.getCall()).isSameAs(call);
assertThat(connectEnd.inetSocketAddress).isEqualTo(expectedAddress); assertThat(connectEnd.getInetSocketAddress()).isEqualTo(expectedAddress);
assertThat(connectEnd.protocol).isEqualTo(Protocol.HTTP_1_1); assertThat(connectEnd.getProtocol()).isEqualTo(Protocol.HTTP_1_1);
} }
@Test public void successfulSocksProxyConnect() throws Exception { @Test public void successfulSocksProxyConnect() throws Exception {
@@ -611,14 +611,14 @@ public final class EventListenerTest {
SocksProxy.HOSTNAME_THAT_ONLY_THE_PROXY_KNOWS, server.getPort()); SocksProxy.HOSTNAME_THAT_ONLY_THE_PROXY_KNOWS, server.getPort());
ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class); ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class);
assertThat(connectStart.call).isSameAs(call); assertThat(connectStart.getCall()).isSameAs(call);
assertThat(connectStart.inetSocketAddress).isEqualTo(expectedAddress); assertThat(connectStart.getInetSocketAddress()).isEqualTo(expectedAddress);
assertThat(connectStart.proxy).isEqualTo(proxy); assertThat(connectStart.getProxy()).isEqualTo(proxy);
ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class); ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class);
assertThat(connectEnd.call).isSameAs(call); assertThat(connectEnd.getCall()).isSameAs(call);
assertThat(connectEnd.inetSocketAddress).isEqualTo(expectedAddress); assertThat(connectEnd.getInetSocketAddress()).isEqualTo(expectedAddress);
assertThat(connectEnd.protocol).isEqualTo(Protocol.HTTP_1_1); assertThat(connectEnd.getProtocol()).isEqualTo(Protocol.HTTP_1_1);
} }
@Test public void authenticatingTunnelProxyConnect() throws IOException { @Test public void authenticatingTunnelProxyConnect() throws IOException {
@@ -646,7 +646,7 @@ public final class EventListenerTest {
listener.removeUpToEvent(ConnectStart.class); listener.removeUpToEvent(ConnectStart.class);
ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class); ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class);
assertThat(connectEnd.protocol).isNull(); assertThat(connectEnd.getProtocol()).isNull();
listener.removeUpToEvent(ConnectStart.class); listener.removeUpToEvent(ConnectStart.class);
listener.removeUpToEvent(ConnectEnd.class); listener.removeUpToEvent(ConnectEnd.class);
@@ -664,11 +664,11 @@ public final class EventListenerTest {
response.body().close(); response.body().close();
SecureConnectStart secureStart = listener.removeUpToEvent(SecureConnectStart.class); SecureConnectStart secureStart = listener.removeUpToEvent(SecureConnectStart.class);
assertThat(secureStart.call).isSameAs(call); assertThat(secureStart.getCall()).isSameAs(call);
SecureConnectEnd secureEnd = listener.removeUpToEvent(SecureConnectEnd.class); SecureConnectEnd secureEnd = listener.removeUpToEvent(SecureConnectEnd.class);
assertThat(secureEnd.call).isSameAs(call); assertThat(secureEnd.getCall()).isSameAs(call);
assertThat(secureEnd.handshake).isNotNull(); assertThat(secureEnd.getHandshake()).isNotNull();
} }
@Test public void failedSecureConnect() { @Test public void failedSecureConnect() {
@@ -686,11 +686,11 @@ public final class EventListenerTest {
} }
SecureConnectStart secureStart = listener.removeUpToEvent(SecureConnectStart.class); SecureConnectStart secureStart = listener.removeUpToEvent(SecureConnectStart.class);
assertThat(secureStart.call).isSameAs(call); assertThat(secureStart.getCall()).isSameAs(call);
CallFailed callFailed = listener.removeUpToEvent(CallFailed.class); CallFailed callFailed = listener.removeUpToEvent(CallFailed.class);
assertThat(callFailed.call).isSameAs(call); assertThat(callFailed.getCall()).isSameAs(call);
assertThat(callFailed.ioe).isNotNull(); assertThat(callFailed.getIoe()).isNotNull();
} }
@Test public void secureConnectWithTunnel() throws IOException { @Test public void secureConnectWithTunnel() throws IOException {
@@ -711,11 +711,11 @@ public final class EventListenerTest {
response.body().close(); response.body().close();
SecureConnectStart secureStart = listener.removeUpToEvent(SecureConnectStart.class); SecureConnectStart secureStart = listener.removeUpToEvent(SecureConnectStart.class);
assertThat(secureStart.call).isSameAs(call); assertThat(secureStart.getCall()).isSameAs(call);
SecureConnectEnd secureEnd = listener.removeUpToEvent(SecureConnectEnd.class); SecureConnectEnd secureEnd = listener.removeUpToEvent(SecureConnectEnd.class);
assertThat(secureEnd.call).isSameAs(call); assertThat(secureEnd.getCall()).isSameAs(call);
assertThat(secureEnd.handshake).isNotNull(); assertThat(secureEnd.getHandshake()).isNotNull();
} }
@Test public void multipleSecureConnectsForSingleCall() throws IOException { @Test public void multipleSecureConnectsForSingleCall() throws IOException {
@@ -784,8 +784,8 @@ public final class EventListenerTest {
response.body().close(); response.body().close();
ConnectionAcquired connectionAcquired = listener.removeUpToEvent(ConnectionAcquired.class); ConnectionAcquired connectionAcquired = listener.removeUpToEvent(ConnectionAcquired.class);
assertThat(connectionAcquired.call).isSameAs(call); assertThat(connectionAcquired.getCall()).isSameAs(call);
assertThat(connectionAcquired.connection).isNotNull(); assertThat(connectionAcquired.getConnection()).isNotNull();
} }
@Test public void noConnectionFoundOnFollowUp() throws IOException { @Test public void noConnectionFoundOnFollowUp() throws IOException {
@@ -830,8 +830,8 @@ public final class EventListenerTest {
response2.body().close(); response2.body().close();
ConnectionAcquired connectionAcquired2 = listener.removeUpToEvent(ConnectionAcquired.class); ConnectionAcquired connectionAcquired2 = listener.removeUpToEvent(ConnectionAcquired.class);
assertThat(connectionAcquired2.connection).isSameAs( assertThat(connectionAcquired2.getConnection()).isSameAs(
connectionAcquired1.connection); connectionAcquired1.getConnection());
} }
@Test public void multipleConnectionsFoundForSingleCall() throws IOException { @Test public void multipleConnectionsFoundForSingleCall() throws IOException {
@@ -893,7 +893,7 @@ public final class EventListenerTest {
} }
CallFailed callFailed = listener.removeUpToEvent(CallFailed.class); CallFailed callFailed = listener.removeUpToEvent(CallFailed.class);
assertThat(callFailed.ioe).isNotNull(); assertThat(callFailed.getIoe()).isNotNull();
} }
@Test public void emptyResponseBody() throws IOException { @Test public void emptyResponseBody() throws IOException {
@@ -989,11 +989,11 @@ public final class EventListenerTest {
if (expectedProtocol != null) { if (expectedProtocol != null) {
ConnectionAcquired connectionAcquired = listener.removeUpToEvent(ConnectionAcquired.class); ConnectionAcquired connectionAcquired = listener.removeUpToEvent(ConnectionAcquired.class);
assertThat(connectionAcquired.connection.protocol()).isEqualTo(expectedProtocol); assertThat(connectionAcquired.getConnection().protocol()).isEqualTo(expectedProtocol);
} }
CallFailed callFailed = listener.removeUpToEvent(CallFailed.class); CallFailed callFailed = listener.removeUpToEvent(CallFailed.class);
assertThat(callFailed.ioe).isNotNull(); assertThat(callFailed.getIoe()).isNotNull();
assertThat(request.ioe).isNotNull(); assertThat(request.ioe).isNotNull();
} }