Skip to content

Commit 92e9874

Browse files
committed
tests reactor
1 parent 1017fc2 commit 92e9874

File tree

11 files changed

+258
-34
lines changed

11 files changed

+258
-34
lines changed

springboot-context-demo/scripts/prototype.sh

+12-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ function printHelp() {
1212
echo "commands :"
1313
echo " get getting bean all or one"
1414
echo " e.g : ${SCRIPT_NAME} get 1 or ${SCRIPT_NAME} get"
15-
echo " getctx getting bean all from context"
15+
echo " getctx getting prototype bean all from context"
1616
echo " e.g : ${SCRIPT_NAME} getctx"
17+
echo " getbeans getting beans all from context"
18+
echo " e.g : ${SCRIPT_NAME} getbeans"
1719
echo " create create prototype bean"
1820
echo " e.g : ${SCRIPT_NAME} create "
1921
echo " delete destroy prototype bean"
@@ -35,6 +37,11 @@ function getctx() {
3537
echo ""
3638
}
3739

40+
function getBeans() {
41+
curl -X GET -H "Content-Type: application/json; charset=utf-8" ${URL}/prototypes/beans
42+
echo ""
43+
}
44+
3845
function create() {
3946
curl -X POST -H "Content-Type: application/json; charset=utf-8" ${URL}/prototype
4047
echo ""
@@ -47,7 +54,7 @@ function delete() {
4754

4855
# command
4956
case "${1}" in
50-
get | getctx | create | delete )
57+
get | getctx | getbeans | create | delete )
5158
COMMAND=${1}
5259
;;
5360
* )
@@ -64,6 +71,9 @@ case "${COMMAND}" in
6471
getctx )
6572
getctx
6673
;;
74+
getbeans )
75+
getBeans
76+
;;
6777
create )
6878
create
6979
;;

springboot-context-demo/src/main/java/demo/proto/PrototypeBeanController.java

+24-14
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public ResponseEntity getBeans() {
4545
}
4646

4747
@GetMapping("/prototypes/ctx")
48-
public ResponseEntity getBeansFromCtx() {
48+
public ResponseEntity getPrototypeBeansFromCtx() {
4949
// 매번 새로운 PrototypeBean1 이 생성되어 Map::size() == 1 인 맵을 반환
5050
StringBuilder result = new StringBuilder("Getting PrototypeBeans1 from ctx\n");
5151

@@ -60,6 +60,25 @@ public ResponseEntity getBeansFromCtx() {
6060
return ResponseEntity.ok(result);
6161
}
6262

63+
64+
@GetMapping("/prototypes/beans")
65+
public ResponseEntity getBeansFromCtx() {
66+
StringBuilder result = new StringBuilder();
67+
68+
String[] beanNames = ctx.getBeanDefinitionNames();
69+
for (String beanName : beanNames) {
70+
Object bean = ctx.getBean(beanName);
71+
if (!bean.getClass().getName().startsWith("demo")) {
72+
continue;
73+
}
74+
String display = String.format("%-30s : %-40s [%s]", beanName, bean.getClass().getName(), bean.toString());
75+
result.append(display)
76+
.append("\n");
77+
}
78+
79+
return ResponseEntity.ok(result.toString());
80+
}
81+
6382
@GetMapping("/prototype/{id}")
6483
public ResponseEntity getBean(@PathVariable("id") Long id) {
6584
logger.info("Try to get bean id : {}", id);
@@ -98,20 +117,11 @@ public ResponseEntity destroyBean(@PathVariable("id") Long id) {
98117
return ResponseEntity.notFound().build();
99118
}
100119

101-
String result = null;
120+
bean = null;
102121

103-
try {
104-
result = bean.toString();
122+
System.gc();
123+
System.runFinalization();
105124

106-
if (bean instanceof DisposableBean) {
107-
logger.info("Can cast DisposableBean");
108-
DisposableBean disposable = (DisposableBean) bean;
109-
disposable.destroy();
110-
}
111-
} catch (Exception e) {
112-
result = e.getMessage();
113-
}
114-
115-
return ResponseEntity.ok(result);
125+
return ResponseEntity.ok().build();
116126
}
117127
}

springboot-reactor-demo/pom.xml

+8-7
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@
2424
<groupId>org.springframework.boot</groupId>
2525
<artifactId>spring-boot-starter-aop</artifactId>
2626
</dependency>
27-
<dependency>
27+
<!--<dependency>
2828
<groupId>org.springframework.boot</groupId>
2929
<artifactId>spring-boot-starter-web</artifactId>
30-
</dependency>
31-
<!--
30+
</dependency>-->
3231
<dependency>
3332
<groupId>org.springframework.boot</groupId>
3433
<artifactId>spring-boot-starter-webflux</artifactId>
35-
</dependency>-->
36-
<dependency>
37-
<groupId>io.projectreactor</groupId>
38-
<artifactId>reactor-bus</artifactId>
3934
</dependency>
4035

36+
<!-- https://mvnrepository.com/artifact/io.projectreactor/reactor-core -->
37+
<!--<dependency>
38+
<groupId>io.projectreactor</groupId>
39+
<artifactId>reactor-core</artifactId>
40+
<version>3.2.11.RELEASE</version>
41+
</dependency>-->
4142
<dependency>
4243
<groupId>org.springframework.boot</groupId>
4344
<artifactId>spring-boot-devtools</artifactId>
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package demo;
22

3-
import javax.servlet.http.HttpServletRequest;
43
import lombok.extern.slf4j.Slf4j;
54
import org.springframework.boot.SpringApplication;
65
import org.springframework.boot.autoconfigure.SpringBootApplication;
7-
import org.springframework.web.bind.annotation.GetMapping;
8-
import org.springframework.web.bind.annotation.PathVariable;
96
import org.springframework.web.bind.annotation.RestController;
10-
import org.springframework.web.servlet.HandlerMapping;
117

128
@Slf4j
139
@RestController
@@ -18,10 +14,10 @@ public static void main(String[] args) {
1814
SpringApplication.run(DemoApplication.class, args);
1915
}
2016

21-
@GetMapping("/main/{pathVariable}")
17+
/*@GetMapping("/main/{pathVariable}")
2218
public void temp(@PathVariable String pathVariable, HttpServletRequest request) {
2319
String attr = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
2420
logger.info("Request /main page. pathVariable : {}, attr : {} / uri : {} / url : {}"
2521
, pathVariable, attr, request.getRequestURI(), request.getRequestURL());
26-
}
22+
}*/
2723
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package demo.dto;
2+
3+
import lombok.Getter;
4+
import lombok.ToString;
5+
6+
/**
7+
*
8+
*/
9+
@Getter
10+
@ToString
11+
public class Pair<F, S> {
12+
13+
private F first;
14+
private S second;
15+
16+
public static <F, S> Pair<F, S> newInstance(F first, S second) {
17+
return new Pair<>(first, second);
18+
}
19+
20+
private Pair(F first, S second) {
21+
this.first = first;
22+
this.second = second;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package demo.publisher1;
2+
3+
import demo.dto.Pair;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.reactivestreams.Publisher;
6+
import reactor.core.publisher.FluxSink;
7+
import reactor.core.publisher.ReplayProcessor;
8+
9+
@Slf4j(topic = "publisher1.BlockProcessor")
10+
public class BlockProcessor {
11+
12+
// replay only last item
13+
private final ReplayProcessor<Pair<Long, String>> blockProcessor = ReplayProcessor.cacheLast();
14+
private final FluxSink<Pair<Long, String>> blockSink = blockProcessor.sink();
15+
16+
public Publisher<Pair<Long, String>> blockStream() {
17+
return blockProcessor;
18+
}
19+
20+
public void newBlock(Pair<Long, String> block) {
21+
logger.info("receive new block : {}", block);
22+
blockSink.next(block);
23+
}
24+
25+
public void error(Throwable throwable) {
26+
blockSink.error(throwable);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package demo.publisher1;
2+
3+
import demo.dto.Pair;
4+
import java.util.UUID;
5+
import java.util.concurrent.TimeUnit;
6+
import java.util.concurrent.atomic.AtomicLong;
7+
8+
/**
9+
*
10+
*/
11+
public class MockBlockchain implements Runnable {
12+
13+
private AtomicLong blockNumber = new AtomicLong(0L);
14+
private BlockProcessor blockProcessor;
15+
16+
public MockBlockchain(BlockProcessor blockProcessor) {
17+
this.blockProcessor = blockProcessor;
18+
}
19+
20+
@Override
21+
public void run() {
22+
try {
23+
while (!Thread.currentThread().isInterrupted()) {
24+
Long bestBlockNumber = blockNumber.getAndIncrement();
25+
26+
if (bestBlockNumber > 5) {
27+
blockProcessor.error(new Exception("Force exception"));
28+
} else {
29+
Pair<Long, String> newBlock = Pair.newInstance(bestBlockNumber, UUID.randomUUID().toString());
30+
blockProcessor.newBlock(newBlock);
31+
}
32+
33+
TimeUnit.SECONDS.sleep(2L);
34+
}
35+
} catch (Exception e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package demo.util;
2+
3+
import java.io.PrintStream;
4+
5+
/**
6+
*
7+
*/
8+
public class ThreadUtil {
9+
10+
private static String NEW_LINE = System.getProperty("line.separator");
11+
private static PrintStream PS = System.out;
12+
13+
public static String getStackTraceString(int cursor) {
14+
StackTraceElement[] elts = Thread.currentThread().getStackTrace();
15+
if (elts == null || elts.length == 1) {
16+
return "";
17+
}
18+
19+
StringBuilder sb = new StringBuilder();
20+
int start, size;
21+
if (cursor >= 0) {
22+
start = cursor + 2;
23+
size = elts.length;
24+
} else {
25+
start = 2;
26+
size = start - cursor + 1;
27+
}
28+
29+
return getStackTraceString(elts, start, size);
30+
}
31+
32+
public static String getStackTraceString(StackTraceElement[] se, int start, int size) {
33+
if (se == null) {
34+
return "";
35+
}
36+
37+
if (size < 0) {
38+
size = 0;
39+
}
40+
size = Math.min(size, se.length);
41+
if (start >= size) {
42+
return "";
43+
}
44+
45+
StringBuilder sb = new StringBuilder();
46+
for (int i = start; i < size; i++) {
47+
sb.append("\t").append(se[i].toString());
48+
if (i != size - 1) {
49+
sb.append(NEW_LINE);
50+
}
51+
}
52+
53+
return sb.toString();
54+
}
55+
56+
public static void printStackTrace() {
57+
PS.println(getStackTraceString(2));
58+
}
59+
60+
private ThreadUtil() {
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package demo.publisher1;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import demo.dto.Pair;
6+
import demo.util.ThreadUtil;
7+
import java.util.concurrent.CountDownLatch;
8+
import javax.swing.TransferHandler;
9+
import org.junit.Test;
10+
import reactor.core.Disposable;
11+
import reactor.core.publisher.Flux;
12+
13+
/**
14+
*
15+
*/
16+
public class Publisher1Tests {
17+
18+
CountDownLatch countDownLatch = new CountDownLatch(1);
19+
Long lastBlock;
20+
21+
@Test
22+
public void runTests() throws Exception {
23+
BlockProcessor blockProcessor = new BlockProcessor();
24+
Disposable disposable = Flux.from(blockProcessor.blockStream())
25+
.subscribe(this::insertBlock, this::onError);
26+
27+
Thread t = new Thread(new MockBlockchain(blockProcessor));
28+
t.setDaemon(true);
29+
t.start();
30+
31+
countDownLatch.await();
32+
System.out.println("## after complete.. :: " + disposable.isDisposed());
33+
34+
Flux.from(blockProcessor.blockStream())
35+
.subscribe(pair -> {
36+
assertThat(pair.getFirst()).isEqualTo(lastBlock);
37+
}
38+
);
39+
}
40+
41+
private void insertBlock(Pair<Long, String> newBlock) {
42+
System.out.println("## [Subscriber] Receive new block : " + newBlock);
43+
lastBlock = newBlock.getFirst();
44+
//ThreadUtil.printStackTrace();
45+
// countDownLatch.countDown();
46+
}
47+
48+
private void onError(Throwable throwable) {
49+
System.err.println("exception occur : " + throwable.getMessage());
50+
countDownLatch.countDown();
51+
}
52+
53+
}

springboot-reactor-demo/src/test/java/demo/reactor/BasicLearnTest.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@
77
import java.util.LinkedHashMap;
88
import java.util.List;
99
import java.util.Map;
10+
import java.util.function.Consumer;
1011
import org.junit.Test;
1112
import reactor.core.publisher.Flux;
1213
import reactor.core.publisher.GroupedFlux;
1314
import reactor.core.publisher.Mono;
1415

1516
/**
16-
* @GitHub : https://github.com/zacscoding
17+
*
1718
*/
1819
public class BasicLearnTest {
1920

2021
@Test
2122
public void flux() throws Exception {
2223
Flux<String> just = Flux.just("1", "2", "3");
24+
Consumer<String> consumer1 = (value) -> System.out.println("[Consumer1] " + value);
25+
Consumer<String> consumer2 = (value) -> System.out.println("[Consumer2] " + value);
26+
just.subscribe(consumer1);
27+
just.subscribe(consumer2);
2328
}
2429

2530
@Test

0 commit comments

Comments
 (0)