|
| 1 | +package org.runnect.server.scrap; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.Mockito.when; |
| 5 | + |
| 6 | +import java.util.Optional; |
| 7 | +import java.util.concurrent.CountDownLatch; |
| 8 | +import java.util.concurrent.ExecutorService; |
| 9 | +import java.util.concurrent.Executors; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | +import java.util.concurrent.atomic.AtomicReference; |
| 12 | +import javax.persistence.EntityManager; |
| 13 | +import javax.persistence.PersistenceContext; |
| 14 | +import org.junit.jupiter.api.AfterEach; |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.runnect.server.publicCourse.entity.PublicCourse; |
| 18 | +import org.runnect.server.publicCourse.repository.PublicCourseRepository; |
| 19 | +import org.runnect.server.scrap.dto.request.CreateAndDeleteScrapRequestDto; |
| 20 | +import org.runnect.server.scrap.service.ScrapService; |
| 21 | +import org.runnect.server.user.entity.RunnectUser; |
| 22 | +import org.runnect.server.user.entity.SocialType; |
| 23 | +import org.runnect.server.user.repository.UserRepository; |
| 24 | +import org.springframework.beans.BeanUtils; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.boot.test.context.SpringBootTest; |
| 27 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 28 | +import org.springframework.test.util.ReflectionTestUtils; |
| 29 | +import org.springframework.transaction.PlatformTransactionManager; |
| 30 | +import org.springframework.transaction.support.TransactionTemplate; |
| 31 | + |
| 32 | +/** |
| 33 | + * 스크랩 생성 로직(ScrapService.createAndDeleteScrap)이 "기존 스크랩 조회 → 없으면 새로 저장" |
| 34 | + * 순서로 동작하는데, 이 사이에 동시 요청(더블탭, 다중 기기)이 끼어들면 둘 다 "스크랩 없음"을 |
| 35 | + * 보고 각자 INSERT를 시도할 수 있다. (user_id, public_course_id) 유니크 제약이 있어 DB가 |
| 36 | + * 하나는 거부하는데, 수정 전에는 이 예외를 그대로 두어 500으로 샜다(실제 로컬 Postgres에 |
| 37 | + * 대해 재현해 org.springframework.dao.DataIntegrityViolationException이 그대로 전파됨을 |
| 38 | + * 확인한 뒤 커밋 로그에 남김). 수정 후에는 HealthService의 기존 처리 패턴과 동일하게 |
| 39 | + * ConflictException(409)으로 변환된다 — 이 테스트는 그 수정 후 동작을 검증한다. |
| 40 | + * |
| 41 | + * PublicCourseRepository는 @MockBean으로 대체한다 — 이 프로젝트의 로컬 Postgres/PostGIS |
| 42 | + * JDBC 드라이버 조합이 geometry(Course.path) 컬럼을 포함한 JOIN FETCH 결과를 추출할 때 |
| 43 | + * 알려진 결함이 있어(다른 통합 테스트에서도 동일 사유로 우회한 이력 있음), 이 테스트가 |
| 44 | + * 검증하려는 "스크랩 유니크 제약 경합"과 무관한 그 문제를 피하기 위함이다. Scrap/User |
| 45 | + * 리포지토리와 트랜잭션은 모두 실제 로컬 Postgres를 그대로 사용한다. |
| 46 | + */ |
| 47 | +@SpringBootTest |
| 48 | +class ScrapConcurrencyTest { |
| 49 | + |
| 50 | + private static final Long EXISTING_PUBLIC_COURSE_ID = 1L; |
| 51 | + |
| 52 | + @Autowired |
| 53 | + private ScrapService scrapService; |
| 54 | + |
| 55 | + @Autowired |
| 56 | + private UserRepository userRepository; |
| 57 | + |
| 58 | + @MockBean |
| 59 | + private PublicCourseRepository publicCourseRepository; |
| 60 | + |
| 61 | + @Autowired |
| 62 | + private PlatformTransactionManager transactionManager; |
| 63 | + |
| 64 | + @PersistenceContext |
| 65 | + private EntityManager entityManager; |
| 66 | + |
| 67 | + private Long testUserId; |
| 68 | + |
| 69 | + @BeforeEach |
| 70 | + void setUpPublicCourseStub() { |
| 71 | + PublicCourse publicCourse = PublicCourse.builder() |
| 72 | + .title("스텁 공개 코스") |
| 73 | + .description("동시성 테스트용 스텁") |
| 74 | + .build(); |
| 75 | + ReflectionTestUtils.setField(publicCourse, "id", EXISTING_PUBLIC_COURSE_ID); |
| 76 | + when(publicCourseRepository.findById(EXISTING_PUBLIC_COURSE_ID)).thenReturn(Optional.of(publicCourse)); |
| 77 | + } |
| 78 | + |
| 79 | + @AfterEach |
| 80 | + void tearDown() { |
| 81 | + if (testUserId == null) { |
| 82 | + return; |
| 83 | + } |
| 84 | + TransactionTemplate tx = new TransactionTemplate(transactionManager); |
| 85 | + tx.executeWithoutResult(status -> { |
| 86 | + entityManager.createQuery("DELETE FROM Scrap s WHERE s.runnectUser.id = :userId") |
| 87 | + .setParameter("userId", testUserId) |
| 88 | + .executeUpdate(); |
| 89 | + entityManager.createQuery("DELETE FROM UserStamp s WHERE s.runnectUser.id = :userId") |
| 90 | + .setParameter("userId", testUserId) |
| 91 | + .executeUpdate(); |
| 92 | + userRepository.deleteById(testUserId); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + private CreateAndDeleteScrapRequestDto scrapRequest(Long publicCourseId, boolean scrapTF) { |
| 97 | + CreateAndDeleteScrapRequestDto dto = BeanUtils.instantiateClass(CreateAndDeleteScrapRequestDto.class); |
| 98 | + ReflectionTestUtils.setField(dto, "publicCourseId", publicCourseId); |
| 99 | + ReflectionTestUtils.setField(dto, "scrapTF", scrapTF); |
| 100 | + return dto; |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void 동시에_같은_코스를_스크랩하면_한쪽은_ConflictException으로_처리된다() throws InterruptedException { |
| 105 | + TransactionTemplate tx = new TransactionTemplate(transactionManager); |
| 106 | + testUserId = tx.execute(status -> userRepository.save( |
| 107 | + RunnectUser.builder() |
| 108 | + .nickname("cc-scrap-race") |
| 109 | + .socialId("concurrency-test-social-id-scrap") |
| 110 | + .email("concurrency-test-scrap@runnect.test") |
| 111 | + .provider(SocialType.VISITOR) |
| 112 | + .build() |
| 113 | + ).getId()); |
| 114 | + |
| 115 | + int threadCount = 2; |
| 116 | + ExecutorService executor = Executors.newFixedThreadPool(threadCount); |
| 117 | + CountDownLatch readyLatch = new CountDownLatch(threadCount); |
| 118 | + CountDownLatch startLatch = new CountDownLatch(1); |
| 119 | + CountDownLatch doneLatch = new CountDownLatch(threadCount); |
| 120 | + AtomicReference<Throwable> capturedException = new AtomicReference<>(); |
| 121 | + |
| 122 | + for (int i = 0; i < threadCount; i++) { |
| 123 | + executor.submit(() -> { |
| 124 | + try { |
| 125 | + readyLatch.countDown(); |
| 126 | + startLatch.await(); |
| 127 | + scrapService.createAndDeleteScrap(testUserId, scrapRequest(EXISTING_PUBLIC_COURSE_ID, true)); |
| 128 | + } catch (Throwable e) { |
| 129 | + capturedException.compareAndSet(null, e); |
| 130 | + } finally { |
| 131 | + doneLatch.countDown(); |
| 132 | + } |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + readyLatch.await(); |
| 137 | + startLatch.countDown(); |
| 138 | + boolean completed = doneLatch.await(15, TimeUnit.SECONDS); |
| 139 | + executor.shutdown(); |
| 140 | + |
| 141 | + assertThat(completed).withFailMessage("스레드가 제한 시간 내에 끝나지 않음").isTrue(); |
| 142 | + |
| 143 | + Throwable exception = capturedException.get(); |
| 144 | + assertThat(exception) |
| 145 | + .withFailMessage("동시 스크랩 요청 중 하나가 실패할 것으로 예상했지만 둘 다 성공함") |
| 146 | + .isNotNull(); |
| 147 | + assertThat(exception) |
| 148 | + .withFailMessage( |
| 149 | + "수정 전에는 DataIntegrityViolationException이 그대로 샜음. 수정 후 예상: ConflictException(409). 실제: %s", |
| 150 | + exception |
| 151 | + ) |
| 152 | + .isInstanceOf(org.runnect.server.common.exception.ConflictException.class); |
| 153 | + } |
| 154 | +} |
0 commit comments