|
1 | 1 | /* |
2 | | - * Copyright (c) 2019-2021 VMware, Inc. or its affiliates, All Rights Reserved. |
| 2 | + * Copyright (c) 2019-2023 VMware, Inc. or its affiliates, All Rights Reserved. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
22 | 22 | import java.util.List; |
23 | 23 | import java.util.Queue; |
24 | 24 | import java.util.concurrent.ArrayBlockingQueue; |
| 25 | +import java.util.concurrent.atomic.AtomicBoolean; |
25 | 26 | import java.util.function.Function; |
26 | 27 | import java.util.stream.Collectors; |
27 | 28 |
|
|
32 | 33 | import io.netty.handler.timeout.WriteTimeoutHandler; |
33 | 34 | import io.netty.util.ReferenceCountUtil; |
34 | 35 | import io.netty.util.ReferenceCounted; |
| 36 | +import org.assertj.core.api.Assertions; |
35 | 37 | import org.junit.jupiter.params.ParameterizedTest; |
36 | 38 | import org.junit.jupiter.params.provider.ValueSource; |
37 | 39 | import org.reactivestreams.Subscription; |
| 40 | +import reactor.core.CoreSubscriber; |
38 | 41 | import reactor.core.Exceptions; |
| 42 | +import reactor.core.Fuseable; |
39 | 43 | import reactor.core.publisher.BaseSubscriber; |
40 | 44 | import reactor.core.publisher.Flux; |
41 | 45 | import reactor.core.publisher.Hooks; |
42 | 46 | import reactor.core.publisher.Mono; |
| 47 | +import reactor.core.publisher.Operators; |
43 | 48 | import reactor.core.publisher.Sinks; |
44 | 49 | import reactor.test.StepVerifier; |
45 | 50 | import reactor.test.publisher.TestPublisher; |
@@ -259,6 +264,84 @@ void shouldNotLeakIfFusedOnRacingCancelAndOnNext(boolean flushOnEach) { |
259 | 264 | } |
260 | 265 | } |
261 | 266 |
|
| 267 | + |
| 268 | + @ParameterizedTest |
| 269 | + @ValueSource(booleans = {true, false}) |
| 270 | + void shouldCallQueueClearToNotifyTermination(boolean flushOnEach) { |
| 271 | + //use an extra handler |
| 272 | + EmbeddedChannel channel = new EmbeddedChannel(true, true, new ChannelHandlerAdapter() {}); |
| 273 | + AtomicBoolean cleared = new AtomicBoolean(); |
| 274 | + |
| 275 | + Sinks.Many<ByteBuf> source = Sinks.many().unicast().onBackpressureBuffer(); |
| 276 | + MonoSendMany<ByteBuf, ByteBuf> m = |
| 277 | + MonoSendMany.byteBufSource(source.asFlux().transform(Operators.<ByteBuf, ByteBuf>lift((__, |
| 278 | + downstream) -> new CoreSubscriber<ByteBuf>() { |
| 279 | + @Override |
| 280 | + public void onSubscribe(Subscription s) { |
| 281 | + downstream.onSubscribe(new Fuseable.QueueSubscription<ByteBuf>() { |
| 282 | + @Override |
| 283 | + public void request(long n) { |
| 284 | + s.request(n); |
| 285 | + } |
| 286 | + |
| 287 | + @Override |
| 288 | + public void cancel() { |
| 289 | + s.cancel(); |
| 290 | + } |
| 291 | + |
| 292 | + @Override |
| 293 | + public int size() { |
| 294 | + return ((Fuseable.QueueSubscription<ByteBuf>) s).size(); |
| 295 | + } |
| 296 | + |
| 297 | + @Override |
| 298 | + public boolean isEmpty() { |
| 299 | + return ((Fuseable.QueueSubscription<ByteBuf>) s).isEmpty(); |
| 300 | + } |
| 301 | + |
| 302 | + @Override |
| 303 | + public void clear() { |
| 304 | + cleared.set(true); |
| 305 | + ((Fuseable.QueueSubscription<ByteBuf>) s).clear(); |
| 306 | + } |
| 307 | + |
| 308 | + @Override |
| 309 | + public ByteBuf poll() { |
| 310 | + return ((Fuseable.QueueSubscription<ByteBuf>) s).poll(); |
| 311 | + } |
| 312 | + |
| 313 | + @Override |
| 314 | + public int requestFusion(int requestedMode) { |
| 315 | + return ((Fuseable.QueueSubscription<ByteBuf>) s).requestFusion(requestedMode); |
| 316 | + } |
| 317 | + }); |
| 318 | + } |
| 319 | + |
| 320 | + @Override |
| 321 | + public void onNext(ByteBuf buf) { |
| 322 | + downstream.onNext(buf); |
| 323 | + } |
| 324 | + |
| 325 | + @Override |
| 326 | + public void onError(Throwable t) { |
| 327 | + downstream.onError(t); |
| 328 | + } |
| 329 | + |
| 330 | + @Override |
| 331 | + public void onComplete() { |
| 332 | + downstream.onComplete(); |
| 333 | + } |
| 334 | + })), channel, b -> flushOnEach); |
| 335 | + m.subscribe(); |
| 336 | + Queue<Object> messages = channel.outboundMessages(); |
| 337 | + |
| 338 | + source.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST); |
| 339 | + |
| 340 | + channel.flush(); |
| 341 | + messages.forEach(ReferenceCountUtil::release); |
| 342 | + Assertions.assertThat(cleared).isTrue(); |
| 343 | + } |
| 344 | + |
262 | 345 | static void wait(WeakReference<Subscription> ref) { |
263 | 346 | int duration = 5_000; |
264 | 347 | int spins = duration / 100; |
|
0 commit comments