Pessimistic Lock 전체 테스트 오류 문의
미해결
재고시스템으로 알아보는 동시성이슈 해결방법
안녕하세요. Pessimistic Lock 소스에서 각각의 테스트 하나하나는 통과하는데요. 테스트 코드 전체로 돌리면 에러가 나더라고요. public interface StockRepository extends JpaRepository<Stock, Long> { @Lock(LockModeType.PESSIMISTIC_WRITE) @Query("select s from Stock s where s.id = :id") Stock findByIdWithPessimisticLock(@Param("id") Long id); } StockServiceTest.java package com.example.stock.service; import com.example.stock.domain.Stock; import com.example.stock.repository.StockRepository; import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest class StockServiceTest { @Autowired private PessimisticLockStockService stockService; @Autowired private StockRepository stockRepository; @BeforeEach public void before() { stockRepository.saveAndFlush(new Stock(1L, 100L)); } @AfterEach public void after() { stockRepository.deleteAll(); } @Test @DisplayName("재고1개 감소 테스트") public void 재고감소() { stockService.decrease(1L, 1L); //100 - 1 = 99 Stock stock = stockRepository.findById(1L).orElseThrow(); assertThat(stock.getQuantity()).isEqualTo(99L); } @Test @DisplayName("동시에 100개 요청 테스트") public void 동시에_100개_요청() throws InterruptedException { int threadCount = 100; ExecutorService executorService = Executors.newFixedThreadPool(32); CountDownLatch countDownLatch = new CountDownLatch(threadCount); for (int i = 0; i < threadCount; i++) { executorService.submit(() -> { try { stockService.decrease(1L, 1L); } finally { countDownLatch.countDown(); } }); } countDownLatch.await(); Stock stock = stockRepository.findById(1L).orElseThrow(); assertThat(stock.getQuantity()).isEqualTo(0L); } } 이런 오류가 나는데 @BeforeEach에서 새로 등록하고 @AfterEach에서 delete를 해줘서 각각의 테스트에 영향이 없을거 같은데 왜 나는지 잘 모르겠어요 ㅠ github : https://github.com/nhs0912/stock
- java
- test
- junit
- pessimisticlock





