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

Use request scope ClientProperties.READ_TIMEOUT in Jetty && Netty #4415

Merged
merged 1 commit into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -301,7 +301,7 @@ private Request translateRequest(final ClientRequest clientRequest) {
request.method(clientRequest.getMethod());

request.followRedirects(clientRequest.resolveProperty(ClientProperties.FOLLOW_REDIRECTS, true));
final Object readTimeout = clientRequest.getConfiguration().getProperties().get(ClientProperties.READ_TIMEOUT);
final Object readTimeout = clientRequest.resolveProperty(ClientProperties.READ_TIMEOUT, -1);
if (readTimeout != null && readTimeout instanceof Integer && (Integer) readTimeout > 0) {
request.timeout((Integer) readTimeout, TimeUnit.MILLISECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -89,7 +89,7 @@ public void testFast() {
@Test
public void testSlow() {
final URI u = target().getUri();
ClientConfig config = new ClientConfig().property(ClientProperties.READ_TIMEOUT, 1000);
ClientConfig config = new ClientConfig().property(ClientProperties.READ_TIMEOUT, 1_000);
config.connectorProvider(new JettyConnectorProvider());
Client c = ClientBuilder.newClient(config);
WebTarget t = c.target(u);
Expand All @@ -103,4 +103,22 @@ public void testSlow() {
c.close();
}
}

@Test
public void testTimeoutInRequest() {
final URI u = target().getUri();
ClientConfig config = new ClientConfig();
config.connectorProvider(new JettyConnectorProvider());
Client c = ClientBuilder.newClient(config);
WebTarget t = c.target(u);
try {
t.path("test/timeout").request().property(ClientProperties.READ_TIMEOUT, 1_000).get();
fail("Timeout expected.");
} catch (ProcessingException e) {
assertThat("Unexpected processing exception cause",
e.getCause(), instanceOf(TimeoutException.class));
} finally {
c.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ public ClientResponse apply(ClientRequest jerseyRequest) {
try {
CompletableFuture<ClientResponse> resultFuture = execute(jerseyRequest);

Integer timeout = ClientProperties.getValue(jerseyRequest.getConfiguration().getProperties(),
ClientProperties.READ_TIMEOUT, 0);
Integer timeout = jerseyRequest.resolveProperty(ClientProperties.READ_TIMEOUT, 0);

return (timeout != null && timeout > 0) ? resultFuture.get(timeout, TimeUnit.MILLISECONDS)
: resultFuture.get();
Expand Down Expand Up @@ -183,8 +182,7 @@ protected void initChannel(SocketChannel ch) throws Exception {
});

// connect timeout
Integer connectTimeout = ClientProperties.getValue(jerseyRequest.getConfiguration().getProperties(),
ClientProperties.CONNECT_TIMEOUT, 0);
Integer connectTimeout = jerseyRequest.resolveProperty(ClientProperties.CONNECT_TIMEOUT, 0);
if (connectTimeout > 0) {
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -64,7 +64,6 @@ protected Application configure() {

@Override
protected void configureClient(ClientConfig config) {
config.property(ClientProperties.READ_TIMEOUT, 1000);
config.connectorProvider(new NettyConnectorProvider());
}

Expand All @@ -78,11 +77,22 @@ public void testFast() {
@Test
public void testSlow() {
try {
target("test/timeout").request().get();
target("test/timeout").property(ClientProperties.READ_TIMEOUT, 1_000).request().get();
fail("Timeout expected.");
} catch (ProcessingException e) {
assertThat("Unexpected processing exception cause",
e.getCause(), instanceOf(TimeoutException.class));
}
}

@Test
public void testTimeoutInRequest() {
try {
target("test/timeout").request().property(ClientProperties.READ_TIMEOUT, 1_000).get();
fail("Timeout expected.");
} catch (ProcessingException e) {
assertThat("Unexpected processing exception cause",
e.getCause(), instanceOf(TimeoutException.class));
}
}
}