-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
232 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
.../Tiketeer/domain/purchase/usecase/CreatePurchaseUseCaseOptimisticLockConcurrencyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package com.tiketeer.Tiketeer.domain.purchase.usecase; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Import; | ||
|
||
import com.tiketeer.Tiketeer.domain.member.Member; | ||
import com.tiketeer.Tiketeer.domain.member.repository.MemberRepository; | ||
import com.tiketeer.Tiketeer.domain.purchase.service.PurchaseCrudService; | ||
import com.tiketeer.Tiketeer.domain.role.constant.RoleEnum; | ||
import com.tiketeer.Tiketeer.domain.ticket.repository.TicketRepository; | ||
import com.tiketeer.Tiketeer.domain.ticket.service.concurrency.TicketConcurrencyService; | ||
import com.tiketeer.Tiketeer.domain.ticket.service.concurrency.TicketOptimisticLockConcurrencyService; | ||
import com.tiketeer.Tiketeer.domain.ticketing.Ticketing; | ||
import com.tiketeer.Tiketeer.domain.ticketing.repository.TicketingRepository; | ||
import com.tiketeer.Tiketeer.domain.ticketing.service.TicketingStockService; | ||
import com.tiketeer.Tiketeer.testhelper.TestHelper; | ||
import com.tiketeer.Tiketeer.testhelper.Transaction; | ||
|
||
@Import({TestHelper.class, CreatePurchaseConcurrencyTest.class, Transaction.class}) | ||
@SpringBootTest | ||
class CreatePurchaseUseCaseOptimisticLockConcurrencyTest { | ||
|
||
@Autowired | ||
private TestHelper testHelper; | ||
@Autowired | ||
private TicketRepository ticketRepository; | ||
@Autowired | ||
private TicketingRepository ticketingRepository; | ||
@Autowired | ||
private TicketingStockService ticketingStockService; | ||
@Autowired | ||
private MemberRepository memberRepository; | ||
@Autowired | ||
private CreatePurchaseConcurrencyTest createPurchaseConcurrencyTest; | ||
@Autowired | ||
private Transaction transaction; | ||
|
||
@BeforeEach | ||
void initTable() { | ||
testHelper.initDB(); | ||
} | ||
|
||
@AfterEach | ||
void cleanTable() { | ||
testHelper.cleanDB(); | ||
} | ||
|
||
@Test | ||
@DisplayName("20개의 티켓 생성 > 40명의 구매자가 경쟁 > 20명 구매 성공, 20명 구매 실패") | ||
void createPurchaseWithConcurrency() throws InterruptedException { | ||
//given | ||
var ticketStock = 20; | ||
var seller = testHelper.createMember("[email protected]", RoleEnum.SELLER); | ||
var ticketing = createTicketing(seller, ticketStock); | ||
|
||
int threadNums = 40; | ||
var buyers = createBuyers(threadNums); | ||
|
||
createPurchaseConcurrencyTest.makeConcurrency(threadNums, buyers, ticketing); | ||
|
||
//then | ||
transaction.invoke(() -> { | ||
var tickets = ticketRepository.findAllByPurchase(null); | ||
assertThat(tickets.size()).isEqualTo(0); | ||
|
||
var allMembers = memberRepository.findAll(); | ||
|
||
//assert all ticket owners are unique | ||
var purchasedTickets = ticketRepository.findAllByPurchaseIsNotNull(); | ||
assertThat(purchasedTickets.size()).isEqualTo(ticketStock); | ||
|
||
var ticketOwnerIdList = purchasedTickets | ||
.stream() | ||
.map(ticket -> ticket.getPurchase().getMember().getId()).toList(); | ||
|
||
Set<UUID> ticketOwnerIdSet = new HashSet<>(ticketOwnerIdList); | ||
assertThat(ticketOwnerIdSet.size()).isEqualTo(ticketOwnerIdList.size()); | ||
|
||
//assert one purchase per member | ||
var ticketingSuccessMembers = allMembers.stream() | ||
.filter(member -> member.getPurchases().size() == 1) | ||
.toList(); | ||
|
||
assertThat(ticketingSuccessMembers.size()).isEqualTo(ticketStock); | ||
return null; | ||
}); | ||
} | ||
|
||
private Ticketing createTicketing(Member member, int stock) { | ||
var now = LocalDateTime.now(); | ||
var ticketing = ticketingRepository.save(Ticketing.builder() | ||
.member(member) | ||
.title("제목") | ||
.price(10000) | ||
.location("서울") | ||
.category("바자회") | ||
.runningMinutes(100) | ||
.saleStart(now.minusMonths(6)) | ||
.saleEnd(now.plusMonths(6)) | ||
.eventTime(now.plusYears(1)) | ||
.build()); | ||
ticketingStockService.createStock(ticketing.getId(), stock); | ||
return ticketing; | ||
} | ||
|
||
private List<Member> createBuyers(int buyerNum) { | ||
List<Member> buyers = new ArrayList<>(); | ||
for (int i = 0; i < buyerNum; i++) { | ||
var memberEmail = "buyer" + i + "@test.com"; | ||
Member buyer = testHelper.createMember(memberEmail, 10000, RoleEnum.BUYER); | ||
buyers.add(buyer); | ||
} | ||
return buyers; | ||
} | ||
|
||
@TestConfiguration | ||
static class TestConfig { | ||
@Bean | ||
public TicketConcurrencyService ticketConcurrencyService( | ||
TicketRepository ticketRepository, | ||
PurchaseCrudService purchaseCrudService) { | ||
return new TicketOptimisticLockConcurrencyService(ticketRepository, purchaseCrudService); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.