Skip to content

Commit afcc411

Browse files
committed
feat: 좋아요 등록 동시성 테스트
1 parent 7dc1b19 commit afcc411

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.loopers.domain.like;
2+
3+
import com.loopers.application.like.LikeFacade;
4+
import com.loopers.interfaces.api.like.LikeV1Dto;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
import java.util.concurrent.CountDownLatch;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
@SpringBootTest
17+
@Transactional
18+
public class LikeConcurrencyIntegrationTest {
19+
20+
@Autowired
21+
private LikeFacade likeFacade;
22+
23+
@Autowired
24+
private LikeRepository likeRepository;
25+
26+
@Test
27+
void 동시에_100개의_좋아요요청이_들어오면_좋아요는_1개만_생성된다() throws Exception {
28+
Long userId = 1L;
29+
Long productId = 1L;
30+
31+
int threadCount = 100;
32+
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
33+
CountDownLatch latch = new CountDownLatch(threadCount);
34+
35+
for (int i = 0; i < threadCount; i++) {
36+
executor.submit(() -> {
37+
try {
38+
likeFacade.doLike(new LikeV1Dto.LikeRequest(userId, productId));
39+
} finally {
40+
latch.countDown();
41+
}
42+
});
43+
}
44+
45+
// 모든 스레드 작업이 종료될 때까지 대기
46+
latch.await();
47+
48+
long count = likeRepository.countByUserIdAndProductId(userId, productId);
49+
assertThat(count).isEqualTo(1);
50+
}
51+
}

0 commit comments

Comments
 (0)