Skip to content

Commit 4c08863

Browse files
committed
Add test case for writeFunction error signal
See gh-22720
1 parent 9bd0ec3 commit 4c08863

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

spring-web/src/test/java/org/springframework/http/server/reactive/ChannelSendOperatorTests.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.http.server.reactive;
1818

1919
import java.nio.charset.StandardCharsets;
20+
import java.time.Duration;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.List;
@@ -33,6 +34,7 @@
3334
import reactor.core.publisher.Flux;
3435
import reactor.core.publisher.Mono;
3536
import reactor.core.publisher.Signal;
37+
import reactor.test.StepVerifier;
3638

3739
import org.springframework.core.io.buffer.DataBuffer;
3840
import org.springframework.core.io.buffer.LeakAwareDataBufferFactory;
@@ -156,7 +158,12 @@ public void cancelWhileItemCached() {
156158
}
157159

158160
@Test // gh-22720
159-
public void errorWhileItemCached() {
161+
public void errorFromWriteSourceWhileItemCached() {
162+
163+
// 1. First item received
164+
// 2. writeFunction applied and writeCompletionBarrier subscribed to it
165+
// 3. Write Publisher fails right after that and before request(n) from server
166+
160167
NettyDataBufferFactory delegate = new NettyDataBufferFactory(ByteBufAllocator.DEFAULT);
161168
LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory(delegate);
162169
ZeroDemandSubscriber writeSubscriber = new ZeroDemandSubscriber();
@@ -186,6 +193,30 @@ public void errorWhileItemCached() {
186193
bufferFactory.checkForLeaks();
187194
}
188195

196+
@Test // gh-22720
197+
public void errorFromWriteFunctionWhileItemCached() {
198+
199+
// 1. First item received
200+
// 2. writeFunction applied and writeCompletionBarrier subscribed to it
201+
// 3. writeFunction fails, e.g. to flush status and headers, before request(n) from server
202+
203+
NettyDataBufferFactory delegate = new NettyDataBufferFactory(ByteBufAllocator.DEFAULT);
204+
LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory(delegate);
205+
206+
ChannelSendOperator<DataBuffer> operator = new ChannelSendOperator<>(
207+
Flux.create(sink -> {
208+
DataBuffer dataBuffer = bufferFactory.allocateBuffer();
209+
dataBuffer.write("foo", StandardCharsets.UTF_8);
210+
sink.next(dataBuffer);
211+
}),
212+
publisher -> {
213+
publisher.subscribe(new ZeroDemandSubscriber());
214+
return Mono.error(new IllegalStateException("err"));
215+
});
216+
217+
StepVerifier.create(operator).expectErrorMessage("err").verify(Duration.ofSeconds(5));
218+
bufferFactory.checkForLeaks();
219+
}
189220

190221
private <T> Mono<Void> sendOperator(Publisher<String> source){
191222
return new ChannelSendOperator<>(source, writer::send);

0 commit comments

Comments
 (0)