|
| 1 | +package com.loopers.application.like; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.loopers.domain.money.Money; |
| 6 | +import com.loopers.domain.product.Product; |
| 7 | +import com.loopers.domain.product.ProductRepository; |
| 8 | +import com.loopers.domain.user.User; |
| 9 | +import com.loopers.infrastructure.user.UserJpaRepository; |
| 10 | +import java.util.List; |
| 11 | +import java.util.concurrent.CountDownLatch; |
| 12 | +import java.util.concurrent.ExecutorService; |
| 13 | +import java.util.concurrent.Executors; |
| 14 | +import java.util.concurrent.atomic.AtomicInteger; |
| 15 | +import java.util.stream.IntStream; |
| 16 | +import org.junit.jupiter.api.AfterEach; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.DisplayName; |
| 19 | +import org.junit.jupiter.api.Nested; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.boot.test.context.SpringBootTest; |
| 23 | + |
| 24 | +@SpringBootTest |
| 25 | +public class LikeConcurrencyTest { |
| 26 | + @Autowired |
| 27 | + private LikeFacade likeFacade; |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private ProductRepository productRepository; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private UserJpaRepository userJpaRepository; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private com.loopers.utils.DatabaseCleanUp databaseCleanUp; |
| 37 | + |
| 38 | + @AfterEach |
| 39 | + void tearDown() { |
| 40 | + databaseCleanUp.truncateAllTables(); |
| 41 | + } |
| 42 | + |
| 43 | + @Nested |
| 44 | + @DisplayName("좋아요 증가 동시성 (비관적 락)") |
| 45 | + class LikeIncreaseConcurrency { |
| 46 | + |
| 47 | + private Product targetProduct; |
| 48 | + private List<User> users; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + void setUp() { |
| 52 | + Product product = new Product(1l, "인기상품", "설명", new Money(10000L), 100); |
| 53 | + this.targetProduct = productRepository.save(product); |
| 54 | + |
| 55 | + this.users = IntStream.range(0, 10) |
| 56 | + .mapToObj(i -> userJpaRepository.save( |
| 57 | + new User("user" + i, "user" + i + "@email.com", "2000-01-01", User.Gender.MALE) |
| 58 | + )) |
| 59 | + .toList(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + @DisplayName("동시에 10명이 좋아요를 누르면, 상품의 좋아요 개수는 정확히 10개가 되어야 한다.") |
| 64 | + void like_concurrency_test() throws InterruptedException { |
| 65 | + // arrange |
| 66 | + int threadCount = 10; |
| 67 | + ExecutorService executorService = Executors.newFixedThreadPool(32); |
| 68 | + CountDownLatch latch = new CountDownLatch(threadCount); |
| 69 | + |
| 70 | + AtomicInteger successCount = new AtomicInteger(); |
| 71 | + AtomicInteger failCount = new AtomicInteger(); |
| 72 | + |
| 73 | + // act |
| 74 | + for (int i = 0; i < threadCount; i++) { |
| 75 | + final int index = i; |
| 76 | + executorService.submit(() -> { |
| 77 | + try { |
| 78 | + likeFacade.like(users.get(index).getId(), targetProduct.getId()); |
| 79 | + successCount.getAndIncrement(); |
| 80 | + } catch (Exception e) { |
| 81 | + System.out.println("좋아요 실패: " + e.getMessage()); |
| 82 | + failCount.getAndIncrement(); |
| 83 | + } finally { |
| 84 | + latch.countDown(); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + latch.await(); |
| 90 | + |
| 91 | + Product resultProduct = productRepository.findById(targetProduct.getId()).orElseThrow(); |
| 92 | + |
| 93 | + System.out.println("좋아요 성공: " + successCount.get() + ", 실패: " + failCount.get()); |
| 94 | + |
| 95 | + // assert |
| 96 | + assertThat(successCount.get()).isEqualTo(threadCount); |
| 97 | + assertThat(resultProduct.getLikeCount()).isEqualTo(10); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Nested |
| 102 | + @DisplayName("좋아요 취소 동시성 (비관적 락)") |
| 103 | + class LikeDecreaseConcurrency { |
| 104 | + |
| 105 | + private Product targetProduct; |
| 106 | + private List<User> users; |
| 107 | + |
| 108 | + @BeforeEach |
| 109 | + void setUp() { |
| 110 | + Product product = new Product(1l, "인기상품", "설명", new Money(10000L), 100); |
| 111 | + this.targetProduct = productRepository.save(product); |
| 112 | + |
| 113 | + this.users = IntStream.range(0, 10) |
| 114 | + .mapToObj(i -> userJpaRepository.save( |
| 115 | + new User("user" + i, "user" + i + "@email.com", "2000-01-01", User.Gender.MALE) |
| 116 | + )) |
| 117 | + .toList(); |
| 118 | + |
| 119 | + for (User user : users) { |
| 120 | + likeFacade.like(user.getId(), targetProduct.getId()); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + @DisplayName("이미 좋아요를 누른 10명이 동시에 취소를 요청하면, 좋아요 개수는 0개가 되어야 한다.") |
| 126 | + void unlike_concurrency_test() throws InterruptedException { |
| 127 | + // arrange |
| 128 | + int threadCount = 10; |
| 129 | + |
| 130 | + Product initialProduct = productRepository.findById(targetProduct.getId()).orElseThrow(); |
| 131 | + assertThat(initialProduct.getLikeCount()).isEqualTo(10); |
| 132 | + |
| 133 | + ExecutorService executorService = Executors.newFixedThreadPool(32); |
| 134 | + CountDownLatch latch = new CountDownLatch(threadCount); |
| 135 | + |
| 136 | + AtomicInteger successCount = new AtomicInteger(); |
| 137 | + AtomicInteger failCount = new AtomicInteger(); |
| 138 | + |
| 139 | + // act |
| 140 | + for (int i = 0; i < threadCount; i++) { |
| 141 | + final int index = i; |
| 142 | + executorService.submit(() -> { |
| 143 | + try { |
| 144 | + likeFacade.unLike(users.get(index).getId(), targetProduct.getId()); |
| 145 | + successCount.getAndIncrement(); |
| 146 | + } catch (Exception e) { |
| 147 | + System.out.println("취소 실패: " + e.getMessage()); |
| 148 | + failCount.getAndIncrement(); |
| 149 | + } finally { |
| 150 | + latch.countDown(); |
| 151 | + } |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + latch.await(); |
| 156 | + |
| 157 | + |
| 158 | + Product resultProduct = productRepository.findById(targetProduct.getId()).orElseThrow(); |
| 159 | + |
| 160 | + System.out.println("취소 성공: " + successCount.get() + ", 실패: " + failCount.get()); |
| 161 | + |
| 162 | + // assert |
| 163 | + assertThat(successCount.get()).isEqualTo(threadCount); |
| 164 | + assertThat(resultProduct.getLikeCount()).isZero(); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments