-
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.
Merge pull request #6 from Tiketeer/feat/DEV-197
[DEV-197] OLock CreatePurchase EP 작성
- Loading branch information
Showing
11 changed files
with
166 additions
and
24 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
49 changes: 49 additions & 0 deletions
49
...ava/com/tiketeer/Tiketeer/domain/purchase/controller/dto/PostPurchaseOLockRequestDto.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,49 @@ | ||
package com.tiketeer.Tiketeer.domain.purchase.controller.dto; | ||
|
||
import java.util.UUID; | ||
|
||
import com.tiketeer.Tiketeer.domain.purchase.usecase.dto.CreatePurchaseOLockCommandDto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@ToString | ||
@NoArgsConstructor(force = true) | ||
public class PostPurchaseOLockRequestDto { | ||
@NotNull | ||
private final UUID ticketingId; | ||
|
||
@NotNull | ||
private final Integer count; | ||
@NotBlank | ||
private final String email; | ||
@NotNull | ||
private final Long backoff; | ||
@NotNull | ||
private final Integer maxAttempts; | ||
|
||
@Builder | ||
public PostPurchaseOLockRequestDto(@NotNull UUID ticketingId, | ||
@NotNull Integer count, @NotBlank String email, @NotNull Long backoff, @NotNull Integer maxAttempts) { | ||
this.ticketingId = ticketingId; | ||
this.count = count; | ||
this.email = email; | ||
this.backoff = backoff; | ||
this.maxAttempts = maxAttempts; | ||
} | ||
|
||
public CreatePurchaseOLockCommandDto convertToDto() { | ||
return CreatePurchaseOLockCommandDto.builder() | ||
.memberEmail(email) | ||
.ticketingId(ticketingId) | ||
.count(count) | ||
.backoff(backoff) | ||
.maxAttempts(maxAttempts) | ||
.build(); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
...java/com/tiketeer/Tiketeer/domain/purchase/usecase/dto/CreatePurchaseOLockCommandDto.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,33 @@ | ||
package com.tiketeer.Tiketeer.domain.purchase.usecase.dto; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.UUID; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@ToString | ||
public class CreatePurchaseOLockCommandDto { | ||
private final String memberEmail; | ||
private final UUID ticketingId; | ||
private final Integer count; | ||
private final Long backoff; | ||
private final Integer maxAttempts; | ||
private LocalDateTime commandCreatedAt = LocalDateTime.now(); | ||
|
||
@Builder | ||
public CreatePurchaseOLockCommandDto( | ||
String memberEmail, UUID ticketingId, Integer count, Long backoff, Integer maxAttempts, | ||
LocalDateTime commandCreatedAt) { | ||
this.memberEmail = memberEmail; | ||
this.ticketingId = ticketingId; | ||
this.count = count; | ||
this.backoff = backoff; | ||
this.maxAttempts = maxAttempts; | ||
if (commandCreatedAt != null) { | ||
this.commandCreatedAt = commandCreatedAt; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.tiketeer.Tiketeer.domain.purchase.PurchaseTestHelper; | ||
import com.tiketeer.Tiketeer.domain.purchase.controller.dto.PostPurchaseDLockRequestDto; | ||
import com.tiketeer.Tiketeer.domain.purchase.controller.dto.PostPurchaseOLockRequestDto; | ||
import com.tiketeer.Tiketeer.domain.purchase.controller.dto.PostPurchasePLockRequestDto; | ||
import com.tiketeer.Tiketeer.domain.purchase.controller.dto.PostPurchaseResponseDto; | ||
import com.tiketeer.Tiketeer.domain.purchase.repository.PurchaseRepository; | ||
|
@@ -130,4 +131,43 @@ void postPurchaseWithDLockSuccess() throws Exception { | |
}); | ||
} | ||
|
||
@Test | ||
@DisplayName("정상 컨디션 > 낙관적 락 구매 생성 요청 > 성공") | ||
void postPurchaseWithOLockSuccess() throws Exception { | ||
// given | ||
var email = "[email protected]"; | ||
var seller = testHelper.createMember(email); | ||
var ticketing = ticketingTestHelper.createTicketing(seller.getId(), 0, 1000, 5); | ||
|
||
var buyerEmail = "[email protected]"; | ||
testHelper.createMember(buyerEmail, 100000); | ||
var buyCnt = 2; | ||
var req = PostPurchaseOLockRequestDto.builder() | ||
.ticketingId(ticketing.getId()) | ||
.email(buyerEmail) | ||
.count(buyCnt) | ||
.backoff(100L) | ||
.maxAttempts(100) | ||
.build(); | ||
|
||
// when | ||
mockMvc.perform( | ||
post("/api/purchases/o-lock") | ||
.contextPath("/api") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.content(objectMapper.writeValueAsString(req)) | ||
).andExpect(status().isCreated()) | ||
.andDo(response -> { | ||
var result = testHelper.getDeserializedApiResponse(response.getResponse().getContentAsString(), | ||
PostPurchaseResponseDto.class).getData(); | ||
|
||
// then | ||
Assertions.assertThat(result.getPurchaseId()).isNotNull(); | ||
var purchase = purchaseRepository.findById(result.getPurchaseId()); | ||
Assertions.assertThat(purchase.isPresent()).isTrue(); | ||
Assertions.assertThat(ticketRepository.findAllByPurchase(purchase.get()).size()).isEqualTo(buyCnt); | ||
}); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
import org.springframework.context.annotation.Import; | ||
|
||
import com.tiketeer.Tiketeer.domain.member.repository.MemberRepository; | ||
import com.tiketeer.Tiketeer.domain.purchase.usecase.dto.CreatePurchasePLockCommandDto; | ||
import com.tiketeer.Tiketeer.domain.purchase.usecase.dto.CreatePurchaseOLockCommandDto; | ||
import com.tiketeer.Tiketeer.domain.ticket.repository.TicketRepository; | ||
import com.tiketeer.Tiketeer.testhelper.TestHelper; | ||
import com.tiketeer.Tiketeer.testhelper.Transaction; | ||
|
@@ -51,18 +51,21 @@ void cleanTable() { | |
@DisplayName("10개의 티켓 생성 > 20명의 구매자가 경쟁 > 10명 구매 성공, 10명 구매 실패") | ||
void createPurchaseWithConcurrency() throws InterruptedException { | ||
//given | ||
var seller = testHelper.createMember("[email protected]"); | ||
var ticketing = createPurchaseConcurrencyTest.createTicketing(seller, 10); | ||
var ticketStock = 10; | ||
var seller = testHelper.createMember("[email protected]"); | ||
var ticketing = createPurchaseConcurrencyTest.createTicketing(seller, ticketStock); | ||
|
||
int threadNums = 20; | ||
var buyers = createPurchaseConcurrencyTest.createBuyers(threadNums); | ||
|
||
createPurchaseConcurrencyTest.makeConcurrency(threadNums, buyers, ticketing, | ||
(email) -> createPurchaseOLockUseCase.createPurchase( | ||
CreatePurchasePLockCommandDto.builder() | ||
CreatePurchaseOLockCommandDto.builder() | ||
.ticketingId(ticketing.getId()) | ||
.memberEmail(email) | ||
.count(1) | ||
.backoff(300L) | ||
.maxAttempts(100) | ||
.build())); | ||
|
||
//then | ||
|
@@ -72,7 +75,10 @@ void createPurchaseWithConcurrency() throws InterruptedException { | |
var allMembers = memberRepository.findAll(); | ||
|
||
//assert all ticket owners are unique | ||
var ticketOwnerIdList = tickets | ||
var purchasedTickets = ticketRepository.findAllByPurchaseIsNotNull(); | ||
assertThat(purchasedTickets.size()).isEqualTo(ticketStock); | ||
|
||
var ticketOwnerIdList = purchasedTickets | ||
.stream() | ||
.map(ticket -> ticket.getPurchase().getMember().getId()).toList(); | ||
|
||
|
@@ -84,7 +90,7 @@ void createPurchaseWithConcurrency() throws InterruptedException { | |
.filter(member -> member.getPurchases().size() == 1) | ||
.toList(); | ||
|
||
assertThat(ticketingSuccessMembers.size()).isEqualTo(10); | ||
assertThat(ticketingSuccessMembers.size()).isEqualTo(ticketStock); | ||
return null; | ||
}); | ||
} | ||
|
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