Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: java11 request options timeout. #1233

Merged
merged 4 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 11 additions & 6 deletions java11/src/main/java/feign/http2client/Http2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
*/
package feign.http2client;

import feign.Client;
import feign.Request;
import feign.Request.Options;
import feign.Response;
import feign.Util;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
Expand All @@ -26,11 +31,10 @@
import java.net.http.HttpRequest.Builder;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.time.Duration;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import feign.*;
import feign.Request.Options;

public class Http2Client implements Client {

Expand All @@ -49,12 +53,13 @@ public Http2Client(HttpClient client) {

@Override
public Response execute(Request request, Options options) throws IOException {
final HttpRequest httpRequest = newRequestBuilder(request).build();
final HttpRequest httpRequest = newRequestBuilder(request, options).build();

HttpResponse<byte[]> httpResponse;
try {
httpResponse = client.send(httpRequest, BodyHandlers.ofByteArray());
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Invalid uri " + request.url(), e);
}

Expand All @@ -71,7 +76,7 @@ public Response execute(Request request, Options options) throws IOException {
return response;
}

private Builder newRequestBuilder(Request request) throws IOException {
private Builder newRequestBuilder(Request request, Options options) throws IOException {
URI uri;
try {
uri = new URI(request.url());
Expand All @@ -89,6 +94,7 @@ private Builder newRequestBuilder(Request request) throws IOException {

final Builder requestBuilder = HttpRequest.newBuilder()
.uri(uri)
.timeout(Duration.ofMillis(options.readTimeoutMillis()))
.version(Version.HTTP_2);

final Map<String, Collection<String>> headers = filterRestrictedHeaders(request.headers());
Expand Down Expand Up @@ -152,8 +158,7 @@ private String[] asString(Map<String, Collection<String>> headers) {
.stream()
.map(value -> Arrays.asList(entry.getKey(), value))
.flatMap(List::stream))
.collect(Collectors.toList())
.toArray(new String[0]);
.toArray(String[]::new);
}

}
23 changes: 23 additions & 0 deletions java11/src/test/java/feign/http2client/test/Http2ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import org.assertj.core.api.Assertions;
import org.hamcrest.CoreMatchers;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.net.http.HttpTimeoutException;
import java.util.concurrent.TimeUnit;

import feign.*;
import feign.client.AbstractClientTest;
import feign.http2client.Http2Client;
Expand All @@ -37,6 +41,10 @@ public interface TestInterface {
@RequestLine("PATCH /patch")
@Headers({"Accept: text/plain"})
String patch();

@RequestLine("POST /timeout")
@Headers({"Accept: text/plain"})
String timeout();
}

@Override
Expand Down Expand Up @@ -80,6 +88,21 @@ public void testVeryLongResponseNullLength() {
// client is too smart to fall for a body that is 8 bytes long
}

@Test
public void timeoutTest() {
server.enqueue(new MockResponse().setBody("foo").setBodyDelay(30, TimeUnit.SECONDS));

final TestInterface api = newBuilder()
.retryer(Retryer.NEVER_RETRY)
.options(new Request.Options(1, TimeUnit.SECONDS, 1, TimeUnit.SECONDS, true))
.target(TestInterface.class, server.url("/").toString());

thrown.expect(FeignException.class);
thrown.expectCause(CoreMatchers.isA(HttpTimeoutException.class));

api.timeout();
}

@Override
public Feign.Builder newBuilder() {
return Feign.builder().client(new Http2Client());
Expand Down