parent
7a55ce48a9
commit
600d6c6fc0
|
@ -122,11 +122,11 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
|
||||||
catch (ExecutionException ex) {
|
catch (ExecutionException ex) {
|
||||||
Throwable cause = ex.getCause();
|
Throwable cause = ex.getCause();
|
||||||
|
|
||||||
if (cause instanceof CancellationException) {
|
if (cause instanceof CancellationException ce) {
|
||||||
if (timeoutHandler != null && timeoutHandler.isTimeout()) {
|
if (timeoutHandler != null) {
|
||||||
throw new HttpTimeoutException("Request timed out");
|
timeoutHandler.handleCancellationException(ce);
|
||||||
}
|
}
|
||||||
throw new IOException("Request was cancelled");
|
throw new IOException("Request cancelled", cause);
|
||||||
}
|
}
|
||||||
if (cause instanceof UncheckedIOException uioEx) {
|
if (cause instanceof UncheckedIOException uioEx) {
|
||||||
throw uioEx.getCause();
|
throw uioEx.getCause();
|
||||||
|
@ -142,10 +142,10 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (CancellationException ex) {
|
catch (CancellationException ex) {
|
||||||
if (timeoutHandler != null && timeoutHandler.isTimeout()) {
|
if (timeoutHandler != null) {
|
||||||
throw new HttpTimeoutException("Request timed out");
|
timeoutHandler.handleCancellationException(ex);
|
||||||
}
|
}
|
||||||
throw new IOException("Request was cancelled");
|
throw new IOException("Request cancelled", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,7 +244,8 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
|
||||||
private static final class TimeoutHandler {
|
private static final class TimeoutHandler {
|
||||||
|
|
||||||
private final CompletableFuture<Void> timeoutFuture;
|
private final CompletableFuture<Void> timeoutFuture;
|
||||||
private final AtomicBoolean isTimeout = new AtomicBoolean(false);
|
|
||||||
|
private final AtomicBoolean timeout = new AtomicBoolean(false);
|
||||||
|
|
||||||
private TimeoutHandler(CompletableFuture<HttpResponse<InputStream>> future, Duration timeout) {
|
private TimeoutHandler(CompletableFuture<HttpResponse<InputStream>> future, Duration timeout) {
|
||||||
|
|
||||||
|
@ -252,8 +253,8 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
|
||||||
.completeOnTimeout(null, timeout.toMillis(), TimeUnit.MILLISECONDS);
|
.completeOnTimeout(null, timeout.toMillis(), TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
this.timeoutFuture.thenRun(() -> {
|
this.timeoutFuture.thenRun(() -> {
|
||||||
|
this.timeout.set(true);
|
||||||
if (future.cancel(true) || future.isCompletedExceptionally() || !future.isDone()) {
|
if (future.cancel(true) || future.isCompletedExceptionally() || !future.isDone()) {
|
||||||
isTimeout.set(true);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -263,7 +264,6 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
@ -282,8 +282,10 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isTimeout() {
|
public void handleCancellationException(CancellationException ex) throws HttpTimeoutException {
|
||||||
return isTimeout.get();
|
if (this.timeout.get()) {
|
||||||
|
throw new HttpTimeoutException(ex.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,117 +0,0 @@
|
||||||
package org.springframework.http.client;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
import static org.mockito.Mockito.*;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.http.HttpClient;
|
|
||||||
import java.net.http.HttpRequest;
|
|
||||||
import java.net.http.HttpResponse;
|
|
||||||
import java.net.http.HttpTimeoutException;
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.concurrent.*;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
|
|
||||||
class JdkClientHttpRequestTest {
|
|
||||||
|
|
||||||
private HttpClient mockHttpClient;
|
|
||||||
private URI uri = URI.create("http://example.com");
|
|
||||||
private HttpMethod method = HttpMethod.GET;
|
|
||||||
|
|
||||||
private ExecutorService executor;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void setup() {
|
|
||||||
mockHttpClient = mock(HttpClient.class);
|
|
||||||
executor = Executors.newSingleThreadExecutor();
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterEach
|
|
||||||
void tearDown() {
|
|
||||||
executor.shutdownNow();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void executeInternal_withTimeout_shouldThrowHttpTimeoutException() throws Exception {
|
|
||||||
Duration timeout = Duration.ofMillis(10);
|
|
||||||
|
|
||||||
JdkClientHttpRequest request = new JdkClientHttpRequest(mockHttpClient, uri, method, executor, timeout);
|
|
||||||
|
|
||||||
CompletableFuture<HttpResponse<InputStream>> future = new CompletableFuture<>();
|
|
||||||
|
|
||||||
when(mockHttpClient.sendAsync(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
|
|
||||||
.thenReturn(future);
|
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
|
|
||||||
CountDownLatch startLatch = new CountDownLatch(1);
|
|
||||||
|
|
||||||
// Cancellation thread waits for startLatch, then cancels the future after a delay
|
|
||||||
Thread canceller = new Thread(() -> {
|
|
||||||
try {
|
|
||||||
startLatch.await();
|
|
||||||
Thread.sleep(500);
|
|
||||||
future.cancel(true);
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
}
|
|
||||||
});
|
|
||||||
canceller.start();
|
|
||||||
|
|
||||||
IOException ex = assertThrows(IOException.class, () -> {
|
|
||||||
startLatch.countDown();
|
|
||||||
request.executeInternal(headers, null);
|
|
||||||
});
|
|
||||||
|
|
||||||
assertThat(ex)
|
|
||||||
.isInstanceOf(HttpTimeoutException.class)
|
|
||||||
.hasMessage("Request timed out");
|
|
||||||
|
|
||||||
canceller.join();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void executeInternal_withTimeout_shouldThrowIOException() throws Exception {
|
|
||||||
Duration timeout = Duration.ofMillis(500);
|
|
||||||
|
|
||||||
JdkClientHttpRequest request = new JdkClientHttpRequest(mockHttpClient, uri, method, executor, timeout);
|
|
||||||
|
|
||||||
CompletableFuture<HttpResponse<InputStream>> future = new CompletableFuture<>();
|
|
||||||
|
|
||||||
when(mockHttpClient.sendAsync(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
|
|
||||||
.thenReturn(future);
|
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
|
|
||||||
CountDownLatch startLatch = new CountDownLatch(1);
|
|
||||||
|
|
||||||
Thread canceller = new Thread(() -> {
|
|
||||||
try {
|
|
||||||
startLatch.await();
|
|
||||||
Thread.sleep(10);
|
|
||||||
future.cancel(true);
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
}
|
|
||||||
});
|
|
||||||
canceller.start();
|
|
||||||
|
|
||||||
IOException ex = assertThrows(IOException.class, () -> {
|
|
||||||
startLatch.countDown();
|
|
||||||
request.executeInternal(headers, null);
|
|
||||||
});
|
|
||||||
|
|
||||||
assertThat(ex)
|
|
||||||
.isInstanceOf(IOException.class)
|
|
||||||
.hasMessage("Request was cancelled");
|
|
||||||
|
|
||||||
canceller.join();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-present the original author or authors.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* https://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 org.springframework.http.client;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.net.http.HttpTimeoutException;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
import static org.mockito.Mockito.any;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for {@link JdkClientHttpRequest}.
|
||||||
|
*/
|
||||||
|
class JdkClientHttpRequestTests {
|
||||||
|
|
||||||
|
private final HttpClient client = mock(HttpClient.class);
|
||||||
|
|
||||||
|
private ExecutorService executor;
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
executor = Executors.newSingleThreadExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
executor.shutdownNow();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void futureCancelledAfterTimeout() {
|
||||||
|
CompletableFuture<HttpResponse<InputStream>> future = new CompletableFuture<>();
|
||||||
|
when(client.sendAsync(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(future);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> createRequest(Duration.ofMillis(10)).executeInternal(new HttpHeaders(), null))
|
||||||
|
.isExactlyInstanceOf(HttpTimeoutException.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void futureCancelled() {
|
||||||
|
CompletableFuture<HttpResponse<InputStream>> future = new CompletableFuture<>();
|
||||||
|
future.cancel(true);
|
||||||
|
when(client.sendAsync(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(future);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> createRequest(null).executeInternal(new HttpHeaders(), null))
|
||||||
|
.isExactlyInstanceOf(IOException.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private JdkClientHttpRequest createRequest(Duration timeout) {
|
||||||
|
return new JdkClientHttpRequest(client, URI.create("http://abc.com"), HttpMethod.GET, executor, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue