55import org .reactivestreams .Subscription ;
66import reactor .core .publisher .ConnectableFlux ;
77import reactor .core .publisher .Flux ;
8- import reactor .core .publisher .FluxSink ;
98import reactor .core .scheduler .Schedulers ;
109
11- import java .time .Duration ;
12- import java .time .LocalDateTime ;
1310import java .util .ArrayList ;
1411import java .util .List ;
15- import java .util .stream .Stream ;
1612
17- import static java .time .Duration .ofSeconds ;
18- import static java .util .stream .Collectors .toList ;
1913import static org .assertj .core .api .Assertions .assertThat ;
2014
2115public class ReactorTest {
2216
2317 @ Test
24- public void givenFlux_whenSubscribing_shouldStream () throws InterruptedException {
18+ public void givenFlux_whenSubscribing_thenStream () throws InterruptedException {
2519
2620 List <Integer > elements = new ArrayList <>();
2721
2822 Flux .just (1 , 2 , 3 , 4 )
2923 .log ()
30- .map (i -> i * 2 )
24+ .map (i -> {
25+ System .out .println (i + ":" + Thread .currentThread ());
26+ return i * 2 ;
27+ })
3128 .subscribe (elements ::add );
3229
3330 assertThat (elements ).containsExactly (2 , 4 , 6 , 8 );
3431 }
3532
3633 @ Test
37- public void givenFlux_whenZipping_shouldCombine () {
34+ public void givenFlux_whenZipping_thenCombine () {
3835 List <String > elements = new ArrayList <>();
3936
4037 Flux .just (1 , 2 , 3 , 4 )
@@ -51,7 +48,7 @@ public void givenFlux_whenZipping_shouldCombine() {
5148 }
5249
5350 @ Test
54- public void givenFlux_whenApplyingBackPressure_shouldPushLessElements () throws InterruptedException {
51+ public void givenFlux_whenApplyingBackPressure_thenPushElementsInBatches () throws InterruptedException {
5552
5653 List <Integer > elements = new ArrayList <>();
5754
@@ -92,18 +89,34 @@ public void onComplete() {
9289 }
9390
9491 @ Test
95- public void givenFlux_whenInParalle_shouldSubscribeInDifferentThreads () throws InterruptedException {
96- List <Integer > elements = new ArrayList <>();
92+ public void givenFlux_whenInParallel_thenSubscribeInDifferentThreads () throws InterruptedException {
93+ List <String > threadNames = new ArrayList <>();
9794
9895 Flux .just (1 , 2 , 3 , 4 )
9996 .log ()
100- .map (i -> i * 2 )
97+ .map (i -> Thread . currentThread (). getName () )
10198 .subscribeOn (Schedulers .parallel ())
102- .subscribe (elements ::add );
99+ .subscribe (threadNames ::add );
103100
104101 Thread .sleep (1000 );
105102
106- assertThat (elements ).containsExactly (2 , 4 , 6 , 8 );
103+ assertThat (threadNames ).containsExactly ("parallel-1" , "parallel-1" , "parallel-1" , "parallel-1" );
104+ }
105+
106+ @ Test
107+ public void givenConnectableFlux_thenShouldStream_onConnect () {
108+
109+ List <Integer > elements = new ArrayList <>();
110+
111+ final ConnectableFlux <Integer > publish = Flux .just (1 , 2 , 3 , 4 ).publish ();
112+
113+ publish .subscribe (elements ::add );
114+
115+ assertThat (elements ).isEmpty ();
116+
117+ publish .connect ();
118+
119+ assertThat (elements ).containsExactly (1 , 2 , 3 , 4 );
107120 }
108121
109122}
0 commit comments