|
19 | 19 | import java.nio.ByteBuffer;
|
20 | 20 | import java.nio.charset.StandardCharsets;
|
21 | 21 | import java.util.ArrayList;
|
| 22 | +import java.util.Collections; |
22 | 23 | import java.util.List;
|
| 24 | +import java.util.Map; |
23 | 25 | import java.util.function.Consumer;
|
24 | 26 | import java.util.function.Supplier;
|
25 | 27 |
|
26 | 28 | import org.junit.jupiter.api.Test;
|
27 | 29 | import org.reactivestreams.Publisher;
|
28 | 30 | import reactor.core.publisher.Flux;
|
29 | 31 | import reactor.core.publisher.Mono;
|
| 32 | +import reactor.netty.channel.AbortedException; |
30 | 33 | import reactor.test.StepVerifier;
|
31 | 34 |
|
| 35 | +import org.springframework.core.ResolvableType; |
32 | 36 | import org.springframework.core.io.buffer.DataBuffer;
|
33 | 37 | import org.springframework.core.io.buffer.DefaultDataBuffer;
|
34 | 38 | import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
| 39 | +import org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory; |
35 | 40 | import org.springframework.http.HttpHeaders;
|
36 | 41 | import org.springframework.http.MediaType;
|
37 | 42 | import org.springframework.http.ResponseCookie;
|
| 43 | +import org.springframework.http.codec.EncoderHttpMessageWriter; |
| 44 | +import org.springframework.http.codec.HttpMessageWriter; |
| 45 | +import org.springframework.http.codec.json.Jackson2JsonEncoder; |
| 46 | +import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest; |
| 47 | +import org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse; |
38 | 48 |
|
39 | 49 | import static org.assertj.core.api.Assertions.assertThat;
|
40 | 50 |
|
@@ -176,6 +186,24 @@ void beforeCommitErrorShouldLeaveResponseNotCommitted() {
|
176 | 186 | });
|
177 | 187 | }
|
178 | 188 |
|
| 189 | + @Test // gh-26232 |
| 190 | + void monoResponseShouldNotLeakIfCancelled() { |
| 191 | + LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory(); |
| 192 | + MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); |
| 193 | + MockServerHttpResponse response = new MockServerHttpResponse(bufferFactory); |
| 194 | + response.setWriteHandler(flux -> { |
| 195 | + throw AbortedException.beforeSend(); |
| 196 | + }); |
| 197 | + |
| 198 | + HttpMessageWriter<Object> messageWriter = new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()); |
| 199 | + Mono<Void> result = messageWriter.write(Mono.just(Collections.singletonMap("foo", "bar")), |
| 200 | + ResolvableType.forClass(Mono.class), ResolvableType.forClass(Map.class), null, |
| 201 | + request, response, Collections.emptyMap()); |
| 202 | + |
| 203 | + StepVerifier.create(result).expectError(AbortedException.class).verify(); |
| 204 | + |
| 205 | + bufferFactory.checkForLeaks(); |
| 206 | + } |
179 | 207 |
|
180 | 208 | private DefaultDataBuffer wrap(String a) {
|
181 | 209 | return new DefaultDataBufferFactory().wrap(ByteBuffer.wrap(a.getBytes(StandardCharsets.UTF_8)));
|
|
0 commit comments