Skip to content

Commit 13b639c

Browse files
committed
Renamed tests and added connectableFlux test
1 parent 6bbb4cf commit 13b639c

2 files changed

Lines changed: 39 additions & 19 deletions

File tree

‎reactor-core/pom.xml‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,36 @@
2424
<dependency>
2525
<groupId>io.projectreactor</groupId>
2626
<artifactId>reactor-core</artifactId>
27-
<version>3.0.4.RELEASE</version>
27+
<version>${reactor-core.version}</version>
2828
</dependency>
2929

3030
<dependency>
3131
<groupId>junit</groupId>
3232
<artifactId>junit</artifactId>
33-
<version>4.12</version>
33+
<version>${junit.version}</version>
3434
<scope>test</scope>
3535
</dependency>
3636

3737
<dependency>
3838
<groupId>org.assertj</groupId>
3939
<artifactId>assertj-core</artifactId>
40-
<version>3.6.1</version>
40+
<version>${assertj.version}</version>
4141
<scope>test</scope>
4242
</dependency>
4343

4444
<dependency>
4545
<groupId>ch.qos.logback</groupId>
4646
<artifactId>logback-classic</artifactId>
47-
<version>1.1.3</version>
47+
<version>${logback.version}</version>
4848
</dependency>
4949

5050
</dependencies>
5151

52+
<properties>
53+
<reactor-core.version>3.0.4.RELEASE</reactor-core.version>
54+
<junit.version>4.12</junit.version>
55+
<assertj.version>3.6.1</assertj.version>
56+
<logback.version>1.1.3</logback.version>
57+
</properties>
58+
5259
</project>

‎reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,33 @@
55
import org.reactivestreams.Subscription;
66
import reactor.core.publisher.ConnectableFlux;
77
import reactor.core.publisher.Flux;
8-
import reactor.core.publisher.FluxSink;
98
import reactor.core.scheduler.Schedulers;
109

11-
import java.time.Duration;
12-
import java.time.LocalDateTime;
1310
import java.util.ArrayList;
1411
import 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;
1913
import static org.assertj.core.api.Assertions.assertThat;
2014

2115
public 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

Comments
 (0)