forked from spring-templates/spring-concurrency-thread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncControllerTest.java
32 lines (27 loc) · 1.1 KB
/
AsyncControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.thread.concurrency.async;
import com.thread.concurrency.async.controller.AsyncController;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@SpringBootTest
public class AsyncControllerTest {
private static final Logger logger = LoggerFactory.getLogger(AsyncServiceTest.class);
@Autowired
private AsyncController asyncController;
@Test
public void invokeMultiAsyncMethod() throws InterruptedException {
List<CompletableFuture<String>> hellos = new ArrayList<>();
for(int i=0; i<10; i++){
hellos.add(asyncController.calculateRunTime(10, 1000));
}
// 모든 비동기 호출이 완료될 때까지 대기하고 결과를 리스트에 넣기
List<String> results = hellos.stream().map(CompletableFuture::join)
.toList();
results.forEach(logger::info);
}
}