Skip to content

Commit 065640b

Browse files
committed
Merge branch 'mogronalol-BAEL-343'
2 parents f1b4ac4 + 13b639c commit 065640b

3 files changed

Lines changed: 182 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<module>querydsl</module>
9595

9696
<!-- <module>raml</module> -->
97+
<module>reactor-core</module>
9798
<module>redis</module>
9899
<module>rest-assured</module>
99100
<module>rest-testing</module>

‎reactor-core/pom.xml‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.baeldung</groupId>
6+
<artifactId>reactor-core</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>org.apache.maven.plugins</groupId>
13+
<artifactId>maven-compiler-plugin</artifactId>
14+
<configuration>
15+
<source>1.8</source>
16+
<target>1.8</target>
17+
</configuration>
18+
</plugin>
19+
</plugins>
20+
</build>
21+
22+
<dependencies>
23+
24+
<dependency>
25+
<groupId>io.projectreactor</groupId>
26+
<artifactId>reactor-core</artifactId>
27+
<version>${reactor-core.version}</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>junit</groupId>
32+
<artifactId>junit</artifactId>
33+
<version>${junit.version}</version>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.assertj</groupId>
39+
<artifactId>assertj-core</artifactId>
40+
<version>${assertj.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>ch.qos.logback</groupId>
46+
<artifactId>logback-classic</artifactId>
47+
<version>${logback.version}</version>
48+
</dependency>
49+
50+
</dependencies>
51+
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+
59+
</project>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.baeldung.reactor;
2+
3+
import org.junit.Test;
4+
import org.reactivestreams.Subscriber;
5+
import org.reactivestreams.Subscription;
6+
import reactor.core.publisher.ConnectableFlux;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.scheduler.Schedulers;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
public class ReactorTest {
16+
17+
@Test
18+
public void givenFlux_whenSubscribing_thenStream() throws InterruptedException {
19+
20+
List<Integer> elements = new ArrayList<>();
21+
22+
Flux.just(1, 2, 3, 4)
23+
.log()
24+
.map(i -> {
25+
System.out.println(i + ":" + Thread.currentThread());
26+
return i * 2;
27+
})
28+
.subscribe(elements::add);
29+
30+
assertThat(elements).containsExactly(2, 4, 6, 8);
31+
}
32+
33+
@Test
34+
public void givenFlux_whenZipping_thenCombine() {
35+
List<String> elements = new ArrayList<>();
36+
37+
Flux.just(1, 2, 3, 4)
38+
.log()
39+
.map(i -> i * 2)
40+
.zipWith(Flux.range(0, Integer.MAX_VALUE).log(), (two, one) -> String.format("First Flux: %d, Second Flux: %d", one, two))
41+
.subscribe(elements::add);
42+
43+
assertThat(elements).containsExactly(
44+
"First Flux: 0, Second Flux: 2",
45+
"First Flux: 1, Second Flux: 4",
46+
"First Flux: 2, Second Flux: 6",
47+
"First Flux: 3, Second Flux: 8");
48+
}
49+
50+
@Test
51+
public void givenFlux_whenApplyingBackPressure_thenPushElementsInBatches() throws InterruptedException {
52+
53+
List<Integer> elements = new ArrayList<>();
54+
55+
Flux.just(1, 2, 3, 4)
56+
.log()
57+
.map(i -> i * 2)
58+
.onBackpressureBuffer()
59+
.subscribe(new Subscriber<Integer>() {
60+
private Subscription s;
61+
int onNextAmount;
62+
63+
@Override
64+
public void onSubscribe(final Subscription s) {
65+
this.s = s;
66+
s.request(2);
67+
}
68+
69+
@Override
70+
public void onNext(final Integer integer) {
71+
elements.add(integer);
72+
onNextAmount++;
73+
if (onNextAmount % 2 == 0) {
74+
s.request(2);
75+
}
76+
}
77+
78+
@Override
79+
public void onError(final Throwable t) {
80+
}
81+
82+
@Override
83+
public void onComplete() {
84+
int ham = 2;
85+
}
86+
});
87+
88+
assertThat(elements).containsExactly(2, 4, 6, 8);
89+
}
90+
91+
@Test
92+
public void givenFlux_whenInParallel_thenSubscribeInDifferentThreads() throws InterruptedException {
93+
List<String> threadNames = new ArrayList<>();
94+
95+
Flux.just(1, 2, 3, 4)
96+
.log()
97+
.map(i -> Thread.currentThread().getName())
98+
.subscribeOn(Schedulers.parallel())
99+
.subscribe(threadNames::add);
100+
101+
Thread.sleep(1000);
102+
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);
120+
}
121+
122+
}

0 commit comments

Comments
 (0)