Skip to content

Commit

Permalink
Use request scope ClientProperties.READ_TIMEOUT in Jetty && Netty (#4415
Browse files Browse the repository at this point in the history
)

Fixes 4405

Signed-off-by: Jan Supol <[email protected]>
  • Loading branch information
jansupol committed Mar 27, 2020
1 parent 6c64522 commit 5c4303b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
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));
}
}
}

0 comments on commit 5c4303b

Please sign in to comment.