Add a 8 Kb flush threshold to RxNettyServerHttpResponse
Issue: SPR-14991
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -26,8 +27,10 @@ import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||
import org.springframework.http.codec.BodyExtractors;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.reactive.ClientRequest;
|
||||
import org.springframework.web.client.reactive.WebClient;
|
||||
|
||||
@@ -45,11 +48,8 @@ public class FlushingIntegrationTests extends AbstractHttpHandlerIntegrationTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlushing() throws Exception {
|
||||
|
||||
ClientRequest<Void> request = ClientRequest.GET("http://localhost:" + port).build();
|
||||
|
||||
|
||||
public void writeAndFlushWith() throws Exception {
|
||||
ClientRequest<Void> request = ClientRequest.GET("http://localhost:" + port + "/write-and-flush").build();
|
||||
Mono<String> result = this.webClient
|
||||
.exchange(request)
|
||||
.flatMap(response -> response.body(BodyExtractors.toFlux(String.class)))
|
||||
@@ -62,6 +62,20 @@ public class FlushingIntegrationTests extends AbstractHttpHandlerIntegrationTest
|
||||
.verify(Duration.ofSeconds(5L));
|
||||
}
|
||||
|
||||
@Test // SPR-14991
|
||||
public void writeAndAutoFlushOnComplete() {
|
||||
ClientRequest<Void> request = ClientRequest.GET("http://localhost:" + port + "/write-and-complete").build();
|
||||
Mono<String> result = this.webClient
|
||||
.exchange(request)
|
||||
.flatMap(response -> response.bodyToFlux(String.class))
|
||||
.reduce((s1, s2) -> s1 + s2);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.consumeNextWith(value -> Assert.isTrue(value.length() == 200000))
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HttpHandler createHttpHandler() {
|
||||
return new FlushingHandler();
|
||||
@@ -71,21 +85,33 @@ public class FlushingIntegrationTests extends AbstractHttpHandlerIntegrationTest
|
||||
|
||||
@Override
|
||||
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
|
||||
Flux<Publisher<DataBuffer>> responseBody = Flux
|
||||
.intervalMillis(50)
|
||||
.map(l -> {
|
||||
byte[] data = ("data" + l).getBytes();
|
||||
DataBuffer buffer = response.bufferFactory().allocateBuffer(data.length);
|
||||
buffer.write(data);
|
||||
return buffer;
|
||||
})
|
||||
.take(2)
|
||||
.map(Flux::just);
|
||||
|
||||
responseBody = responseBody.concatWith(Flux.never());
|
||||
|
||||
return response.writeAndFlushWith(responseBody);
|
||||
String path = request.getURI().getPath();
|
||||
if (path.endsWith("write-and-flush")) {
|
||||
Flux<Publisher<DataBuffer>> responseBody = Flux
|
||||
.intervalMillis(50)
|
||||
.map(l -> toDataBuffer("data" + l, response.bufferFactory()))
|
||||
.take(2)
|
||||
.map(Flux::just);
|
||||
responseBody = responseBody.concatWith(Flux.never());
|
||||
return response.writeAndFlushWith(responseBody);
|
||||
}
|
||||
else if (path.endsWith("write-and-complete")){
|
||||
Flux<DataBuffer> responseBody = Flux
|
||||
.just("0123456789")
|
||||
.repeat(20000)
|
||||
.map(value -> toDataBuffer(value, response.bufferFactory()));
|
||||
return response.writeWith(responseBody);
|
||||
}
|
||||
return response.writeWith(Flux.empty());
|
||||
}
|
||||
|
||||
private DataBuffer toDataBuffer(String value, DataBufferFactory factory) {
|
||||
byte[] data = (value).getBytes(StandardCharsets.UTF_8);
|
||||
DataBuffer buffer = factory.allocateBuffer(data.length);
|
||||
buffer.write(data);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user