Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 테스트 데이터 초기화 설정 #8

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -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();
})
);
}
}
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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("행성이"));
Expand All @@ -50,5 +58,4 @@ class MemberTicketServiceTest {
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("티켓 재고가 소진되었습니다.");
}

}