diff --git a/ticket/src/test/java/com/aengdulab/ticket/service/MemberTicketServiceConcurrencyTest.java b/ticket/src/test/java/com/aengdulab/ticket/service/MemberTicketServiceConcurrencyTest.java index 1333ff9..81078ab 100644 --- a/ticket/src/test/java/com/aengdulab/ticket/service/MemberTicketServiceConcurrencyTest.java +++ b/ticket/src/test/java/com/aengdulab/ticket/service/MemberTicketServiceConcurrencyTest.java @@ -13,6 +13,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.IntStream; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -34,6 +35,13 @@ class MemberTicketServiceConcurrencyTest { @Autowired private TicketRepository ticketRepository; + @BeforeEach + void setUp() { + memberTicketRepository.deleteAllInBatch(); + memberRepository.deleteAllInBatch(); + ticketRepository.deleteAllInBatch(); + } + @Test void 동시에_여러_멤버_티켓을_발행할_때_티켓_재고가_올바르게_수정된다() throws InterruptedException { Ticket ticket = ticketRepository.save(new Ticket("목성행", 10L)); @@ -48,10 +56,10 @@ class MemberTicketServiceConcurrencyTest { for (Member member : members) { IntStream.range(0, MemberTicket.MEMBER_TICKET_COUNT_MAX) .forEach(ticketCount -> - executorService.submit(() -> { - memberTicketService.issue(member.getId(), ticket.getId()); - latch.countDown(); - }) + executorService.submit(() -> { + memberTicketService.issue(member.getId(), ticket.getId()); + latch.countDown(); + }) ); } } @@ -63,7 +71,11 @@ class MemberTicketServiceConcurrencyTest { assertThat(issuedTicketCount).isEqualTo(MemberTicket.MEMBER_TICKET_COUNT_MAX); } - assertThat(ticketRepository.findById(ticket.getId()).orElseThrow().getQuantity()).isEqualTo(0); + assertThat(getTicketQuantity(ticket)).isEqualTo(0); assertThat(ticket.getQuantity()).isEqualTo(0); } + + private Long getTicketQuantity(Ticket ticket) { + return ticketRepository.findById(ticket.getId()).orElseThrow().getQuantity(); + } } diff --git a/ticket/src/test/java/com/aengdulab/ticket/service/MemberTicketServiceTest.java b/ticket/src/test/java/com/aengdulab/ticket/service/MemberTicketServiceTest.java index 6d07d62..3fe91f9 100644 --- a/ticket/src/test/java/com/aengdulab/ticket/service/MemberTicketServiceTest.java +++ b/ticket/src/test/java/com/aengdulab/ticket/service/MemberTicketServiceTest.java @@ -8,6 +8,7 @@ import com.aengdulab.ticket.repository.MemberRepository; import com.aengdulab.ticket.repository.MemberTicketRepository; import com.aengdulab.ticket.repository.TicketRepository; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -29,6 +30,13 @@ class MemberTicketServiceTest { @Autowired private TicketRepository ticketRepository; + @BeforeEach + void setUp() { + memberTicketRepository.deleteAllInBatch(); + memberRepository.deleteAllInBatch(); + ticketRepository.deleteAllInBatch(); + } + @Test void 멤버_티켓_발행_제한_수를_초과하면_예외가_발생한다() { Member member = memberRepository.save(new Member("행성이")); @@ -50,5 +58,4 @@ class MemberTicketServiceTest { .isInstanceOf(IllegalArgumentException.class) .hasMessage("티켓 재고가 소진되었습니다."); } - }