38
38
import io .netty .buffer .ByteBufAllocator ;
39
39
import io .netty .buffer .Unpooled ;
40
40
import io .netty .util .CharsetUtil ;
41
+ import io .netty .util .ReferenceCountUtil ;
41
42
import io .netty .util .ReferenceCounted ;
42
43
import io .rsocket .Payload ;
43
44
import io .rsocket .RSocket ;
71
72
import java .util .function .Function ;
72
73
import java .util .stream .Stream ;
73
74
import org .assertj .core .api .Assertions ;
75
+ import org .junit .jupiter .api .AfterEach ;
74
76
import org .junit .jupiter .api .BeforeEach ;
75
77
import org .junit .jupiter .api .Disabled ;
76
78
import org .junit .jupiter .api .Test ;
84
86
import org .reactivestreams .Subscription ;
85
87
import reactor .core .publisher .BaseSubscriber ;
86
88
import reactor .core .publisher .Flux ;
89
+ import reactor .core .publisher .Hooks ;
87
90
import reactor .core .publisher .Mono ;
88
91
import reactor .core .publisher .MonoProcessor ;
89
92
import reactor .core .publisher .UnicastProcessor ;
@@ -97,6 +100,8 @@ public class RSocketRequesterTest {
97
100
98
101
@ BeforeEach
99
102
public void setUp () throws Throwable {
103
+ Hooks .onNextDropped (ReferenceCountUtil ::safeRelease );
104
+ Hooks .onErrorDropped ((t ) -> {});
100
105
rule = new ClientSocketRule ();
101
106
rule .apply (
102
107
new Statement () {
@@ -107,6 +112,12 @@ public void evaluate() {}
107
112
.evaluate ();
108
113
}
109
114
115
+ @ AfterEach
116
+ public void tearDown () {
117
+ Hooks .resetOnErrorDropped ();
118
+ Hooks .resetOnNextDropped ();
119
+ }
120
+
110
121
@ Test
111
122
@ Timeout (2_000 )
112
123
public void testInvalidFrameOnStream0 () {
@@ -403,21 +414,8 @@ static Stream<BiFunction<RSocket, Payload, Publisher<?>>> prepareCalls() {
403
414
rule .assertHasNoLeaks ();
404
415
}
405
416
406
- @ Test
407
- @ Disabled ("Due to https://github.com/reactor/reactor-core/pull/2114" )
408
- @ SuppressWarnings ("unchecked" )
409
- public void checkNoLeaksOnRacingTest () {
410
-
411
- racingCases ()
412
- .forEach (
413
- a -> {
414
- ((Runnable ) a .get ()[0 ]).run ();
415
- checkNoLeaksOnRacing (
416
- (Function <ClientSocketRule , Publisher <Payload >>) a .get ()[1 ],
417
- (BiConsumer <AssertSubscriber <Payload >, ClientSocketRule >) a .get ()[2 ]);
418
- });
419
- }
420
-
417
+ @ ParameterizedTest
418
+ @ MethodSource ("racingCases" )
421
419
public void checkNoLeaksOnRacing (
422
420
Function <ClientSocketRule , Publisher <Payload >> initiator ,
423
421
BiConsumer <AssertSubscriber <Payload >, ClientSocketRule > runner ) {
@@ -437,7 +435,7 @@ public void evaluate() {}
437
435
}
438
436
439
437
Publisher <Payload > payloadP = initiator .apply (clientSocketRule );
440
- AssertSubscriber <Payload > assertSubscriber = AssertSubscriber .create ();
438
+ AssertSubscriber <Payload > assertSubscriber = AssertSubscriber .create (0 );
441
439
442
440
if (payloadP instanceof Flux ) {
443
441
((Flux <Payload >) payloadP ).doOnNext (Payload ::release ).subscribe (assertSubscriber );
@@ -450,14 +448,13 @@ public void evaluate() {}
450
448
Assertions .assertThat (clientSocketRule .connection .getSent ())
451
449
.allMatch (ReferenceCounted ::release );
452
450
453
- rule .assertHasNoLeaks ();
451
+ clientSocketRule .assertHasNoLeaks ();
454
452
}
455
453
}
456
454
457
455
private static Stream <Arguments > racingCases () {
458
456
return Stream .of (
459
457
Arguments .of (
460
- (Runnable ) () -> System .out .println ("RequestStream downstream cancellation case" ),
461
458
(Function <ClientSocketRule , Publisher <Payload >>)
462
459
(rule ) -> rule .socket .requestStream (EmptyPayload .INSTANCE ),
463
460
(BiConsumer <AssertSubscriber <Payload >, ClientSocketRule >)
@@ -467,6 +464,7 @@ private static Stream<Arguments> racingCases() {
467
464
metadata .writeCharSequence ("abc" , CharsetUtil .UTF_8 );
468
465
ByteBuf data = allocator .buffer ();
469
466
data .writeCharSequence ("def" , CharsetUtil .UTF_8 );
467
+ as .request (1 );
470
468
int streamId = rule .getStreamIdForRequestType (REQUEST_STREAM );
471
469
ByteBuf frame =
472
470
PayloadFrameFlyweight .encode (
@@ -475,7 +473,6 @@ private static Stream<Arguments> racingCases() {
475
473
RaceTestUtils .race (as ::cancel , () -> rule .connection .addToReceivedBuffer (frame ));
476
474
}),
477
475
Arguments .of (
478
- (Runnable ) () -> System .out .println ("RequestChannel downstream cancellation case" ),
479
476
(Function <ClientSocketRule , Publisher <Payload >>)
480
477
(rule ) -> rule .socket .requestChannel (Flux .just (EmptyPayload .INSTANCE )),
481
478
(BiConsumer <AssertSubscriber <Payload >, ClientSocketRule >)
@@ -485,6 +482,7 @@ private static Stream<Arguments> racingCases() {
485
482
metadata .writeCharSequence ("abc" , CharsetUtil .UTF_8 );
486
483
ByteBuf data = allocator .buffer ();
487
484
data .writeCharSequence ("def" , CharsetUtil .UTF_8 );
485
+ as .request (1 );
488
486
int streamId = rule .getStreamIdForRequestType (REQUEST_CHANNEL );
489
487
ByteBuf frame =
490
488
PayloadFrameFlyweight .encode (
@@ -493,79 +491,93 @@ private static Stream<Arguments> racingCases() {
493
491
RaceTestUtils .race (as ::cancel , () -> rule .connection .addToReceivedBuffer (frame ));
494
492
}),
495
493
Arguments .of (
496
- (Runnable ) () -> System .out .println ("RequestChannel upstream cancellation 1" ),
497
494
(Function <ClientSocketRule , Publisher <Payload >>)
498
495
(rule ) -> {
499
496
ByteBufAllocator allocator = rule .alloc ();
500
497
ByteBuf metadata = allocator .buffer ();
501
- metadata .writeCharSequence ("abc " , CharsetUtil .UTF_8 );
498
+ metadata .writeCharSequence ("metadata " , CharsetUtil .UTF_8 );
502
499
ByteBuf data = allocator .buffer ();
503
- data .writeCharSequence ("def" , CharsetUtil .UTF_8 );
504
- return rule .socket .requestChannel (
505
- Flux .just (ByteBufPayload .create (data , metadata )));
500
+ data .writeCharSequence ("data" , CharsetUtil .UTF_8 );
501
+ final Payload payload = ByteBufPayload .create (data , metadata );
502
+
503
+ return rule .socket .requestStream (payload );
506
504
},
507
505
(BiConsumer <AssertSubscriber <Payload >, ClientSocketRule >)
508
- (as , rule ) -> {
506
+ (as , rule ) -> RaceTestUtils .race (() -> as .request (1 ), as ::cancel )),
507
+ Arguments .of (
508
+ (Function <ClientSocketRule , Publisher <Payload >>)
509
+ (rule ) -> {
509
510
ByteBufAllocator allocator = rule .alloc ();
510
- int streamId = rule .getStreamIdForRequestType (REQUEST_CHANNEL );
511
- ByteBuf frame = CancelFrameFlyweight .encode (allocator , streamId );
512
-
513
- RaceTestUtils .race (
514
- () -> as .request (1 ), () -> rule .connection .addToReceivedBuffer (frame ));
515
- }),
511
+ return rule .socket .requestChannel (
512
+ Flux .generate (
513
+ () -> 1L ,
514
+ (index , sink ) -> {
515
+ ByteBuf metadata = allocator .buffer ();
516
+ metadata .writeCharSequence ("metadata" , CharsetUtil .UTF_8 );
517
+ ByteBuf data = allocator .buffer ();
518
+ data .writeCharSequence ("data" , CharsetUtil .UTF_8 );
519
+ final Payload payload = ByteBufPayload .create (data , metadata );
520
+ sink .next (payload );
521
+ sink .complete ();
522
+ return ++index ;
523
+ }));
524
+ },
525
+ (BiConsumer <AssertSubscriber <Payload >, ClientSocketRule >)
526
+ (as , rule ) -> RaceTestUtils .race (() -> as .request (1 ), as ::cancel )),
516
527
Arguments .of (
517
- (Runnable ) () -> System .out .println ("RequestChannel upstream cancellation 2" ),
518
528
(Function <ClientSocketRule , Publisher <Payload >>)
519
529
(rule ) ->
520
530
rule .socket .requestChannel (
521
531
Flux .generate (
522
532
() -> 1L ,
523
533
(index , sink ) -> {
524
- final Payload payload =
525
- ByteBufPayload .create ("d" + index , "m" + index );
534
+ ByteBuf data = rule .alloc ().buffer ();
535
+ data .writeCharSequence ("d" + index , CharsetUtil .UTF_8 );
536
+ ByteBuf metadata = rule .alloc ().buffer ();
537
+ metadata .writeCharSequence ("m" + index , CharsetUtil .UTF_8 );
538
+ final Payload payload = ByteBufPayload .create (data , metadata );
526
539
sink .next (payload );
527
540
return ++index ;
528
541
})),
529
542
(BiConsumer <AssertSubscriber <Payload >, ClientSocketRule >)
530
543
(as , rule ) -> {
531
544
ByteBufAllocator allocator = rule .alloc ();
545
+ as .request (1 );
532
546
int streamId = rule .getStreamIdForRequestType (REQUEST_CHANNEL );
533
547
ByteBuf frame = CancelFrameFlyweight .encode (allocator , streamId );
534
548
535
- as .request (1 );
536
-
537
549
RaceTestUtils .race (
538
550
() -> as .request (Long .MAX_VALUE ),
539
551
() -> rule .connection .addToReceivedBuffer (frame ));
540
552
}),
541
553
Arguments .of (
542
- (Runnable ) () -> System .out .println ("RequestChannel remote error" ),
543
554
(Function <ClientSocketRule , Publisher <Payload >>)
544
555
(rule ) ->
545
556
rule .socket .requestChannel (
546
557
Flux .generate (
547
558
() -> 1L ,
548
559
(index , sink ) -> {
549
- final Payload payload =
550
- ByteBufPayload .create ("d" + index , "m" + index );
560
+ ByteBuf data = rule .alloc ().buffer ();
561
+ data .writeCharSequence ("d" + index , CharsetUtil .UTF_8 );
562
+ ByteBuf metadata = rule .alloc ().buffer ();
563
+ metadata .writeCharSequence ("m" + index , CharsetUtil .UTF_8 );
564
+ final Payload payload = ByteBufPayload .create (data , metadata );
551
565
sink .next (payload );
552
566
return ++index ;
553
567
})),
554
568
(BiConsumer <AssertSubscriber <Payload >, ClientSocketRule >)
555
569
(as , rule ) -> {
556
570
ByteBufAllocator allocator = rule .alloc ();
571
+ as .request (1 );
557
572
int streamId = rule .getStreamIdForRequestType (REQUEST_CHANNEL );
558
573
ByteBuf frame =
559
574
ErrorFrameFlyweight .encode (allocator , streamId , new RuntimeException ("test" ));
560
575
561
- as .request (1 );
562
-
563
576
RaceTestUtils .race (
564
577
() -> as .request (Long .MAX_VALUE ),
565
578
() -> rule .connection .addToReceivedBuffer (frame ));
566
579
}),
567
580
Arguments .of (
568
- (Runnable ) () -> System .out .println ("RequestResponse downstream cancellation" ),
569
581
(Function <ClientSocketRule , Publisher <Payload >>)
570
582
(rule ) -> rule .socket .requestResponse (EmptyPayload .INSTANCE ),
571
583
(BiConsumer <AssertSubscriber <Payload >, ClientSocketRule >)
@@ -575,6 +587,7 @@ private static Stream<Arguments> racingCases() {
575
587
metadata .writeCharSequence ("abc" , CharsetUtil .UTF_8 );
576
588
ByteBuf data = allocator .buffer ();
577
589
data .writeCharSequence ("def" , CharsetUtil .UTF_8 );
590
+ as .request (Long .MAX_VALUE );
578
591
int streamId = rule .getStreamIdForRequestType (REQUEST_RESPONSE );
579
592
ByteBuf frame =
580
593
PayloadFrameFlyweight .encode (
0 commit comments