Skip to content

Commit f2a37b5

Browse files
authored
Merge pull request #9 from Kimjipang/round04
Round04
2 parents 7dc1b19 + 5c2a54f commit f2a37b5

5 files changed

Lines changed: 62 additions & 2 deletions

File tree

apps/commerce-api/src/main/java/com/loopers/domain/like/LikeRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ public interface LikeRepository {
1010
void delete(Like like);
1111

1212
int countByProductId(Long productId);
13+
14+
int countByUserIdAndProductId(Long userId, Long productId);
1315
}

apps/commerce-api/src/main/java/com/loopers/infrastructure/like/LikeJpaRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ public interface LikeJpaRepository extends JpaRepository<Like, Long> {
99
Optional<Like> findByUserIdAndProductId(Long userId, Long productId);
1010

1111
int countByProductId(Long productId);
12+
13+
int countByUserIdAndProductId(Long userId, Long productId);
1214
}

apps/commerce-api/src/main/java/com/loopers/infrastructure/like/LikeRepositoryImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ public void delete(Like like) {
3131
public int countByProductId(Long productId) {
3232
return likeJpaRepository.countByProductId(productId);
3333
}
34+
35+
@Override
36+
public int countByUserIdAndProductId(Long userId, Long productId) {
37+
return likeJpaRepository.countByUserIdAndProductId(userId, productId);
38+
}
3439
}
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+
}

apps/commerce-api/src/test/java/com/loopers/domain/user/UserServiceIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void returnsUserEntity_when_user_exists() {
109109
userService.save(userEntity);
110110

111111
// act
112-
UserEntity foundUser = userService.getUserByLoginId(loginId);
112+
UserEntity foundUser = userService.findUserByLoginId(loginId);
113113

114114
// assert
115115
assertThat(foundUser).isNotNull();
@@ -127,7 +127,7 @@ void returnsNull_when_user_not_exists() {
127127

128128
// act
129129
final CoreException result = assertThrows(CoreException.class, () -> {
130-
userService.getUserByLoginId(loginId);
130+
userService.findUserByLoginId(loginId);
131131
});
132132

133133
// assert

0 commit comments

Comments
 (0)