|
1 | | -package org.depromeet.spot.application; |
2 | | - |
3 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | | - |
5 | | -import java.util.concurrent.CountDownLatch; |
6 | | -import java.util.concurrent.ExecutorService; |
7 | | -import java.util.concurrent.Executors; |
8 | | -import java.util.concurrent.atomic.AtomicLong; |
9 | | - |
10 | | -import org.depromeet.spot.domain.member.Level; |
11 | | -import org.depromeet.spot.domain.member.Member; |
12 | | -import org.depromeet.spot.domain.member.enums.MemberRole; |
13 | | -import org.depromeet.spot.domain.member.enums.SnsProvider; |
14 | | -import org.depromeet.spot.domain.review.Review; |
15 | | -import org.depromeet.spot.usecase.port.in.review.ReadReviewUsecase; |
16 | | -import org.depromeet.spot.usecase.port.out.member.LevelRepository; |
17 | | -import org.depromeet.spot.usecase.port.out.member.MemberRepository; |
18 | | -import org.depromeet.spot.usecase.service.review.like.ReviewLikeService; |
19 | | -import org.junit.jupiter.api.BeforeEach; |
20 | | -import org.junit.jupiter.api.Test; |
21 | | -import org.springframework.beans.factory.annotation.Autowired; |
22 | | -import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; |
23 | | -import org.springframework.boot.test.context.SpringBootTest; |
24 | | -import org.springframework.test.context.ActiveProfiles; |
25 | | -import org.springframework.test.context.TestPropertySource; |
26 | | -import org.springframework.test.context.jdbc.Sql; |
27 | | -import org.springframework.test.context.jdbc.Sql.ExecutionPhase; |
28 | | -import org.springframework.test.context.jdbc.SqlGroup; |
29 | | -import org.springframework.transaction.annotation.Transactional; |
30 | | -import org.testcontainers.junit.jupiter.Testcontainers; |
31 | | - |
32 | | -import lombok.extern.slf4j.Slf4j; |
33 | | - |
34 | | -@Slf4j |
35 | | -@SpringBootTest |
36 | | -@Testcontainers |
37 | | -@ActiveProfiles("test") |
38 | | -@TestPropertySource("classpath:application-test.yml") |
39 | | -@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) |
40 | | -@SqlGroup({ |
41 | | - @Sql( |
42 | | - value = "/sql/delete-data-after-review-like.sql", |
43 | | - executionPhase = ExecutionPhase.AFTER_TEST_METHOD), |
44 | | - @Sql( |
45 | | - value = "/sql/review-like-service-data.sql", |
46 | | - executionPhase = ExecutionPhase.BEFORE_TEST_METHOD), |
47 | | -}) |
48 | | -class ReviewLikeServiceTest { |
49 | | - |
50 | | - @Autowired private ReviewLikeService reviewLikeService; |
51 | | - |
52 | | - @Autowired private ReadReviewUsecase readReviewUsecase; |
53 | | - |
54 | | - @Autowired private MemberRepository memberRepository; |
55 | | - |
56 | | - @Autowired private LevelRepository levelRepository; |
57 | | - |
58 | | - private static final int NUMBER_OF_THREAD = 100; |
59 | | - |
60 | | - @BeforeEach |
61 | | - @Transactional |
62 | | - void init() { |
63 | | - Level level = levelRepository.findByValue(0); |
64 | | - AtomicLong memberIdGenerator = new AtomicLong(1); |
65 | | - |
66 | | - for (int i = 0; i < NUMBER_OF_THREAD; i++) { |
67 | | - long memberId = memberIdGenerator.getAndIncrement(); |
68 | | - memberRepository.save( |
69 | | - Member.builder() |
70 | | - .id(memberId) |
71 | | - .snsProvider(SnsProvider.KAKAO) |
72 | | - .teamId(1L) |
73 | | - .role(MemberRole.ROLE_ADMIN) |
74 | | - .idToken("idToken" + memberId) |
75 | | - .nickname(String.valueOf(memberId)) |
76 | | - .phoneNumber(String.valueOf(memberId)) |
77 | | - .email("email" + memberId) |
78 | | - .build(), |
79 | | - level); |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - @Test |
84 | | - void 멀티_스레드_환경에서_리뷰_공감_수를_정상적으로_증가시킬_수_있다() throws InterruptedException { |
85 | | - // given |
86 | | - final long reviewId = 1L; |
87 | | - AtomicLong memberIdGenerator = new AtomicLong(1); |
88 | | - final ExecutorService executorService = Executors.newFixedThreadPool(NUMBER_OF_THREAD); |
89 | | - final CountDownLatch latch = new CountDownLatch(NUMBER_OF_THREAD); |
90 | | - |
91 | | - // when |
92 | | - for (int i = 0; i < NUMBER_OF_THREAD; i++) { |
93 | | - long memberId = memberIdGenerator.getAndIncrement(); |
94 | | - executorService.execute( |
95 | | - () -> { |
96 | | - try { |
97 | | - reviewLikeService.toggleLike(memberId, reviewId); |
98 | | - System.out.println( |
99 | | - "Thread " + Thread.currentThread().getId() + " - 성공"); |
100 | | - } catch (Throwable e) { |
101 | | - System.out.println( |
102 | | - "Thread " |
103 | | - + Thread.currentThread().getId() |
104 | | - + " - 실패" |
105 | | - + e.getClass().getName()); |
106 | | - e.printStackTrace(); |
107 | | - } finally { |
108 | | - latch.countDown(); |
109 | | - } |
110 | | - }); |
111 | | - } |
112 | | - latch.await(); |
113 | | - executorService.shutdown(); |
114 | | - |
115 | | - // then |
116 | | - Review review = readReviewUsecase.findById(reviewId); |
117 | | - assertEquals(100, review.getLikesCount()); |
118 | | - } |
119 | | -} |
| 1 | +// package org.depromeet.spot.application; |
| 2 | +// |
| 3 | +// import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +// |
| 5 | +// import java.util.concurrent.CountDownLatch; |
| 6 | +// import java.util.concurrent.ExecutorService; |
| 7 | +// import java.util.concurrent.Executors; |
| 8 | +// import java.util.concurrent.atomic.AtomicLong; |
| 9 | +// |
| 10 | +// import org.depromeet.spot.domain.member.Level; |
| 11 | +// import org.depromeet.spot.domain.member.Member; |
| 12 | +// import org.depromeet.spot.domain.member.enums.MemberRole; |
| 13 | +// import org.depromeet.spot.domain.member.enums.SnsProvider; |
| 14 | +// import org.depromeet.spot.domain.review.Review; |
| 15 | +// import org.depromeet.spot.usecase.port.in.review.ReadReviewUsecase; |
| 16 | +// import org.depromeet.spot.usecase.port.out.member.LevelRepository; |
| 17 | +// import org.depromeet.spot.usecase.port.out.member.MemberRepository; |
| 18 | +// import org.depromeet.spot.usecase.service.review.like.ReviewLikeService; |
| 19 | +// import org.junit.jupiter.api.BeforeEach; |
| 20 | +// import org.junit.jupiter.api.Test; |
| 21 | +// import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +// import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; |
| 23 | +// import org.springframework.boot.test.context.SpringBootTest; |
| 24 | +// import org.springframework.test.context.ActiveProfiles; |
| 25 | +// import org.springframework.test.context.TestPropertySource; |
| 26 | +// import org.springframework.test.context.jdbc.Sql; |
| 27 | +// import org.springframework.test.context.jdbc.Sql.ExecutionPhase; |
| 28 | +// import org.springframework.test.context.jdbc.SqlGroup; |
| 29 | +// import org.springframework.transaction.annotation.Transactional; |
| 30 | +// import org.testcontainers.junit.jupiter.Testcontainers; |
| 31 | +// |
| 32 | +// import lombok.extern.slf4j.Slf4j; |
| 33 | +// |
| 34 | +// @Slf4j |
| 35 | +// @SpringBootTest |
| 36 | +// @Testcontainers |
| 37 | +// @ActiveProfiles("test") |
| 38 | +// @TestPropertySource("classpath:application-test.yml") |
| 39 | +// @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) |
| 40 | +// @SqlGroup({ |
| 41 | +// @Sql( |
| 42 | +// value = "/sql/delete-data-after-review-like.sql", |
| 43 | +// executionPhase = ExecutionPhase.AFTER_TEST_METHOD), |
| 44 | +// @Sql( |
| 45 | +// value = "/sql/review-like-service-data.sql", |
| 46 | +// executionPhase = ExecutionPhase.BEFORE_TEST_METHOD), |
| 47 | +// }) |
| 48 | +// class ReviewLikeServiceTest { |
| 49 | +// |
| 50 | +// @Autowired private ReviewLikeService reviewLikeService; |
| 51 | +// |
| 52 | +// @Autowired private ReadReviewUsecase readReviewUsecase; |
| 53 | +// |
| 54 | +// @Autowired private MemberRepository memberRepository; |
| 55 | +// |
| 56 | +// @Autowired private LevelRepository levelRepository; |
| 57 | +// |
| 58 | +// private static final int NUMBER_OF_THREAD = 100; |
| 59 | +// |
| 60 | +// @BeforeEach |
| 61 | +// @Transactional |
| 62 | +// void init() { |
| 63 | +// Level level = levelRepository.findByValue(0); |
| 64 | +// AtomicLong memberIdGenerator = new AtomicLong(1); |
| 65 | +// |
| 66 | +// for (int i = 0; i < NUMBER_OF_THREAD; i++) { |
| 67 | +// long memberId = memberIdGenerator.getAndIncrement(); |
| 68 | +// memberRepository.save( |
| 69 | +// Member.builder() |
| 70 | +// .id(memberId) |
| 71 | +// .snsProvider(SnsProvider.KAKAO) |
| 72 | +// .teamId(1L) |
| 73 | +// .role(MemberRole.ROLE_ADMIN) |
| 74 | +// .idToken("idToken" + memberId) |
| 75 | +// .nickname(String.valueOf(memberId)) |
| 76 | +// .phoneNumber(String.valueOf(memberId)) |
| 77 | +// .email("email" + memberId) |
| 78 | +// .build(), |
| 79 | +// level); |
| 80 | +// } |
| 81 | +// } |
| 82 | +// |
| 83 | +// @Test |
| 84 | +// void 멀티_스레드_환경에서_리뷰_공감_수를_정상적으로_증가시킬_수_있다() throws InterruptedException { |
| 85 | +// // given |
| 86 | +// final long reviewId = 1L; |
| 87 | +// AtomicLong memberIdGenerator = new AtomicLong(1); |
| 88 | +// final ExecutorService executorService = Executors.newFixedThreadPool(NUMBER_OF_THREAD); |
| 89 | +// final CountDownLatch latch = new CountDownLatch(NUMBER_OF_THREAD); |
| 90 | +// |
| 91 | +// // when |
| 92 | +// for (int i = 0; i < NUMBER_OF_THREAD; i++) { |
| 93 | +// long memberId = memberIdGenerator.getAndIncrement(); |
| 94 | +// executorService.execute( |
| 95 | +// () -> { |
| 96 | +// try { |
| 97 | +// reviewLikeService.toggleLike(memberId, reviewId); |
| 98 | +// System.out.println( |
| 99 | +// "Thread " + Thread.currentThread().getId() + " - 성공"); |
| 100 | +// } catch (Throwable e) { |
| 101 | +// System.out.println( |
| 102 | +// "Thread " |
| 103 | +// + Thread.currentThread().getId() |
| 104 | +// + " - 실패" |
| 105 | +// + e.getClass().getName()); |
| 106 | +// e.printStackTrace(); |
| 107 | +// } finally { |
| 108 | +// latch.countDown(); |
| 109 | +// } |
| 110 | +// }); |
| 111 | +// } |
| 112 | +// latch.await(); |
| 113 | +// executorService.shutdown(); |
| 114 | +// |
| 115 | +// // then |
| 116 | +// Review review = readReviewUsecase.findById(reviewId); |
| 117 | +// assertEquals(100, review.getLikesCount()); |
| 118 | +// } |
| 119 | +// } |
0 commit comments