HttpHandlerConnector uses non-blocking thread
Closes gh-23936
This commit is contained in:
@@ -25,6 +25,7 @@ import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.publisher.MonoProcessor;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpCookie;
|
||||
@@ -75,6 +76,13 @@ public class HttpHandlerConnector implements ClientHttpConnector {
|
||||
public Mono<ClientHttpResponse> connect(HttpMethod httpMethod, URI uri,
|
||||
Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
|
||||
|
||||
return Mono.defer(() -> doConnect(httpMethod, uri, requestCallback))
|
||||
.subscribeOn(Schedulers.parallel());
|
||||
}
|
||||
|
||||
private Mono<ClientHttpResponse> doConnect(
|
||||
HttpMethod httpMethod, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
|
||||
|
||||
MonoProcessor<ClientHttpResponse> result = MonoProcessor.create();
|
||||
|
||||
MockClientHttpRequest mockClientRequest = new MockClientHttpRequest(httpMethod, uri);
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
@@ -51,7 +52,7 @@ public class HttpHandlerConnectorTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void adaptRequest() throws Exception {
|
||||
public void adaptRequest() {
|
||||
|
||||
TestHttpHandler handler = new TestHttpHandler(response -> {
|
||||
response.setStatusCode(HttpStatus.OK);
|
||||
@@ -79,7 +80,7 @@ public class HttpHandlerConnectorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adaptResponse() throws Exception {
|
||||
public void adaptResponse() {
|
||||
|
||||
ResponseCookie cookie = ResponseCookie.from("custom-cookie", "c0").build();
|
||||
|
||||
@@ -104,6 +105,22 @@ public class HttpHandlerConnectorTests {
|
||||
assertThat(DataBufferTestUtils.dumpString(buffer, UTF_8)).isEqualTo("Custom body");
|
||||
}
|
||||
|
||||
@Test // gh-23936
|
||||
public void handlerOnNonBlockingThread() {
|
||||
|
||||
TestHttpHandler handler = new TestHttpHandler(response -> {
|
||||
|
||||
assertThat(Schedulers.isInNonBlockingThread()).isTrue();
|
||||
|
||||
response.setStatusCode(HttpStatus.OK);
|
||||
return response.setComplete();
|
||||
});
|
||||
|
||||
new HttpHandlerConnector(handler)
|
||||
.connect(HttpMethod.POST, URI.create("/path"), request -> request.writeWith(Mono.empty()))
|
||||
.block(Duration.ofSeconds(5));
|
||||
}
|
||||
|
||||
private DataBuffer toDataBuffer(String body) {
|
||||
return new DefaultDataBufferFactory().wrap(body.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user