From 85c39e0283e9df17211184575d42b8c250c0063e Mon Sep 17 00:00:00 2001 From: juha Date: Sun, 3 Nov 2024 19:29:03 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=20=EC=B4=88=EA=B8=B0=ED=99=94=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MemberTicketServiceConcurrencyTest.java | 22 ++++++++++++++----- .../service/MemberTicketServiceTest.java | 9 +++++++- 2 files changed, 25 insertions(+), 6 deletions(-) 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("티켓 재고가 소진되었습니다."); } - }