Skip to content

Commit

Permalink
Confirm that call timeouts don't apply to SSE or web sockets.
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed Nov 5, 2018
1 parent fc954f2 commit 48ff0c8
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 57 deletions.
5 changes: 5 additions & 0 deletions okhttp-sse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<artifactId>okhttp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>okhttp-testing-support</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void connect(OkHttpClient client) {
.eventListener(EventListener.NONE)
.build();
call = client.newCall(request);
call.timeout().clearTimeout();
call.enqueue(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package okhttp3.internal.sse;

import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.mockwebserver.MockResponse;
Expand All @@ -25,13 +26,14 @@
import org.junit.Rule;
import org.junit.Test;

import static okhttp3.TestUtil.defaultClient;
import static org.junit.Assert.assertEquals;

public final class EventSourceHttpTest {
@Rule public final MockWebServer server = new MockWebServer();

private final EventSourceRecorder listener = new EventSourceRecorder();
private final EventSource.Factory factory = EventSources.createFactory(new OkHttpClient());
private OkHttpClient client = defaultClient();

@After public void after() {
listener.assertExhausted();
Expand Down Expand Up @@ -69,10 +71,30 @@ public final class EventSourceHttpTest {
listener.assertFailure(null);
}

@Test public void callTimeoutIsNotApplied() throws Exception {
client = client.newBuilder()
.callTimeout(100, TimeUnit.MILLISECONDS)
.build();

server.enqueue(new MockResponse()
.setBodyDelay(500, TimeUnit.MILLISECONDS)
.setHeader("content-type", "text/event-stream")
.setBody("data: hey\n\n"));

EventSource source = newEventSource();

assertEquals("/", source.request().url().encodedPath());

listener.assertOpen();
listener.assertEvent(null, null, "hey");
listener.assertClose();
}

private EventSource newEventSource() {
Request request = new Request.Builder()
.url(server.url("/"))
.build();
EventSource.Factory factory = EventSources.createFactory(client);
return factory.newEventSource(request, listener);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
/*
* Copyright (C) 2018 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.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import okhttp3.internal.SingleInetAddressDns;
import okhttp3.internal.http2.Header;

public final class TestUtil {
public static final InetSocketAddress UNREACHABLE_ADDRESS
= new InetSocketAddress("198.51.100.1", 8080);

/**
* 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.
*/
private static final Dns SINGLE_INET_ADDRESS_DNS = new Dns() {
@Override public List<InetAddress> lookup(String hostname) throws UnknownHostException {
List<InetAddress> addresses = Dns.SYSTEM.lookup(hostname);
return Collections.singletonList(addresses.get(0));
}
};

private TestUtil() {
}

Expand All @@ -30,7 +58,7 @@ public static OkHttpClient defaultClient() {
return new OkHttpClient.Builder()
.connectionPool(connectionPool)
.dispatcher(dispatcher)
.dns(new SingleInetAddressDns()) // Prevent unexpected fallback addresses.
.dns(SINGLE_INET_ADDRESS_DNS) // Prevent unexpected fallback addresses.
.build();
}

Expand Down
4 changes: 0 additions & 4 deletions okhttp-tests/src/test/java/okhttp3/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
import javax.net.ssl.SSLSocketFactory;
import okhttp3.internal.DoubleInetAddressDns;
import okhttp3.internal.RecordingOkAuthenticator;
import okhttp3.internal.SingleInetAddressDns;
import okhttp3.internal.Util;
import okhttp3.internal.Version;
import okhttp3.internal.http.RecordingProxySelector;
Expand Down Expand Up @@ -1090,7 +1089,6 @@ private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exc

client = client.newBuilder()
.hostnameVerifier(new RecordingHostnameVerifier())
.dns(new SingleInetAddressDns())
// Attempt RESTRICTED_TLS then fall back to MODERN_TLS.
.connectionSpecs(Arrays.asList(ConnectionSpec.RESTRICTED_TLS, ConnectionSpec.MODERN_TLS))
.sslSocketFactory(
Expand Down Expand Up @@ -1119,7 +1117,6 @@ private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exc
// Attempt RESTRICTED_TLS then fall back to MODERN_TLS.
.connectionSpecs(Arrays.asList(ConnectionSpec.RESTRICTED_TLS, ConnectionSpec.MODERN_TLS))
.hostnameVerifier(new RecordingHostnameVerifier())
.dns(new SingleInetAddressDns())
.build();

Request request = new Request.Builder().url(server.url("/")).build();
Expand Down Expand Up @@ -1161,7 +1158,6 @@ private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exc
client = client.newBuilder()
.connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT))
.hostnameVerifier(new RecordingHostnameVerifier())
.dns(new SingleInetAddressDns())
.sslSocketFactory(
suppressTlsFallbackClientSocketFactory(), handshakeCertificates.trustManager())
.build();
Expand Down
13 changes: 5 additions & 8 deletions okhttp-tests/src/test/java/okhttp3/EventListenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import okhttp3.RecordingEventListener.SecureConnectStart;
import okhttp3.internal.DoubleInetAddressDns;
import okhttp3.internal.RecordingOkAuthenticator;
import okhttp3.internal.SingleInetAddressDns;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
Expand Down Expand Up @@ -80,7 +79,6 @@ public final class EventListenerTest {
public static final Matcher<Response> anyResponse = CoreMatchers.any(Response.class);
@Rule public final MockWebServer server = new MockWebServer();

private final SingleInetAddressDns singleDns = new SingleInetAddressDns();
private final RecordingEventListener listener = new RecordingEventListener();
private final HandshakeCertificates handshakeCertificates = localhost();

Expand All @@ -89,7 +87,6 @@ public final class EventListenerTest {

@Before public void setUp() {
client = defaultClient().newBuilder()
.dns(singleDns)
.eventListener(listener)
.build();

Expand Down Expand Up @@ -437,8 +434,8 @@ private Matcher<Response> matchesProtocol(final Protocol protocol) {
server.enqueue(new MockResponse());

FakeDns dns = new FakeDns();
dns.set("fakeurl", singleDns.lookup(server.getHostName()));
dns.set("www.fakeurl", singleDns.lookup(server.getHostName()));
dns.set("fakeurl", client.dns().lookup(server.getHostName()));
dns.set("www.fakeurl", client.dns().lookup(server.getHostName()));

client = client.newBuilder()
.dns(dns)
Expand Down Expand Up @@ -513,7 +510,7 @@ private Matcher<Response> matchesProtocol(final Protocol protocol) {
assertEquals(200, response.code());
response.body().close();

InetAddress address = singleDns.lookup(server.getHostName()).get(0);
InetAddress address = client.dns().lookup(server.getHostName()).get(0);
InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort());

ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class);
Expand Down Expand Up @@ -541,7 +538,7 @@ private Matcher<Response> matchesProtocol(final Protocol protocol) {
} catch (IOException expected) {
}

InetAddress address = singleDns.lookup(server.getHostName()).get(0);
InetAddress address = client.dns().lookup(server.getHostName()).get(0);
InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort());

ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class);
Expand Down Expand Up @@ -593,7 +590,7 @@ private Matcher<Response> matchesProtocol(final Protocol protocol) {
assertEquals(200, response.code());
response.body().close();

InetAddress address = singleDns.lookup(server.getHostName()).get(0);
InetAddress address = client.dns().lookup(server.getHostName()).get(0);
InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort());

ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class);
Expand Down
7 changes: 0 additions & 7 deletions okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
import okhttp3.internal.Internal;
import okhttp3.internal.RecordingAuthenticator;
import okhttp3.internal.RecordingOkAuthenticator;
import okhttp3.internal.SingleInetAddressDns;
import okhttp3.internal.Util;
import okhttp3.internal.Version;
import okhttp3.internal.huc.OkHttpURLConnection;
Expand All @@ -88,7 +87,6 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static java.util.Collections.singletonMap;
import static java.util.Locale.US;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
Expand Down Expand Up @@ -719,7 +717,6 @@ private void connectViaHttpsReusingConnections(boolean rebuildClient) throws Exc
server.enqueue(new MockResponse().setSocketPolicy(FAIL_HANDSHAKE));

urlFactory.setClient(urlFactory.client().newBuilder()
.dns(new SingleInetAddressDns())
.connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS))
.hostnameVerifier(new RecordingHostnameVerifier())
.sslSocketFactory(
Expand Down Expand Up @@ -1021,7 +1018,6 @@ private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exce
// Configure a single IP address for the host and a single configuration, so we only need one
// failure to fail permanently.
urlFactory.setClient(urlFactory.client().newBuilder()
.dns(new SingleInetAddressDns())
.sslSocketFactory(
handshakeCertificates.sslSocketFactory(), handshakeCertificates.trustManager())
.connectionSpecs(Util.immutableList(ConnectionSpec.MODERN_TLS))
Expand Down Expand Up @@ -2648,9 +2644,6 @@ protected ServerSocket configureServerSocket(ServerSocket serverSocket)
* https://code.google.com/p/android/issues/detail?id=41576
*/
@Test public void sameConnectionRedirectAndReuse() throws Exception {
urlFactory.setClient(urlFactory.client().newBuilder()
.dns(new SingleInetAddressDns())
.build());
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.setSocketPolicy(SHUTDOWN_INPUT_AT_END)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import okhttp3.TestUtil;
import okhttp3.internal.DoubleInetAddressDns;
import okhttp3.internal.RecordingOkAuthenticator;
import okhttp3.internal.SingleInetAddressDns;
import okhttp3.internal.Util;
import okhttp3.internal.connection.RealConnection;
import okhttp3.mockwebserver.Dispatcher;
Expand Down Expand Up @@ -125,7 +124,6 @@ private static OkHttpClient buildH2PriorKnowledgeClient() {
private static OkHttpClient buildHttp2Client() {
return defaultClient().newBuilder()
.protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1))
.dns(new SingleInetAddressDns())
.sslSocketFactory(
handshakeCertificates.sslSocketFactory(), handshakeCertificates.trustManager())
.hostnameVerifier(new RecordingHostnameVerifier())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,24 @@ public final class WebSocketHttpTest {
assertEquals(Collections.emptyList(), listener.recordedEventTypes());
}

@Test public void callTimeoutIsNotApplied() throws Exception {
client = client.newBuilder()
.callTimeout(100, TimeUnit.MILLISECONDS)
.build();

webServer.enqueue(new MockResponse()
.withWebSocketUpgrade(serverListener));
newWebSocket();

clientListener.assertOpen();
WebSocket server = serverListener.assertOpen();

Thread.sleep(500);

server.send("Hello, WebSockets!");
clientListener.assertTextMessage("Hello, WebSockets!");
}

private MockResponse upgradeResponse(RecordedRequest request) {
String key = request.getHeader("Sec-WebSocket-Key");
return new MockResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public void connect(OkHttpClient client) {
.header("Sec-WebSocket-Version", "13")
.build();
call = Internal.instance.newWebSocketCall(client, request);
call.timeout().clearTimeout();
call.enqueue(new Callback() {
@Override public void onResponse(Call call, Response response) {
try {
Expand Down

0 comments on commit 48ff0c8

Please sign in to comment.