-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
10 changed files
with
197 additions
and
4 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
4 changes: 3 additions & 1 deletion
4
src/main/java/jshop/domain/wallet/repository/WalletRepository.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 |
---|---|---|
@@ -1,13 +1,15 @@ | ||
package jshop.domain.wallet.repository; | ||
|
||
import jakarta.persistence.LockModeType; | ||
import java.util.Optional; | ||
import jshop.domain.wallet.entity.Wallet; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Lock; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
public interface WalletRepository extends JpaRepository<Wallet, Long> { | ||
|
||
@Query("select u.wallet from User u where u.id = :userId") | ||
Optional<Wallet> findWalletByUserId(@Param("userId") Long userId); | ||
} |
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
146 changes: 146 additions & 0 deletions
146
src/test/java/jshop/integration/domain/user/UserControllerSyncTest.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,146 @@ | ||
package jshop.integration.domain.user; | ||
|
||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Stream; | ||
import jshop.domain.cart.repository.CartRepository; | ||
import jshop.domain.user.entity.User; | ||
import jshop.domain.user.repository.UserRepository; | ||
import jshop.domain.user.service.UserService; | ||
import jshop.domain.wallet.entity.Wallet; | ||
import jshop.domain.wallet.repository.WalletRepository; | ||
import jshop.global.common.ErrorCode; | ||
import jshop.utils.dto.UserDtoUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
|
||
@EnableWebMvc | ||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@DisplayName("[통합 테스트] UserController - Sync") | ||
public class UserControllerSyncTest { | ||
|
||
private Long userId; | ||
private String userToken; | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private CartRepository cartRepository; | ||
|
||
@Autowired | ||
private WalletRepository walletRepository; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@PersistenceContext | ||
private EntityManager em; | ||
|
||
|
||
@BeforeEach | ||
public void init() throws Exception { | ||
userId = userService.joinUser(UserDtoUtils.getJoinUserRequestDto()); | ||
ResultActions login = mockMvc.perform( | ||
post("/api/login").contentType(MediaType.APPLICATION_JSON).content(UserDtoUtils.getLoginJsonStr())); | ||
userToken = login.andReturn().getResponse().getHeader("Authorization"); | ||
} | ||
|
||
@Nested | ||
@DisplayName("사용자 잔고 변경 낙관적락 동시성 테스트") | ||
class UpdateUserWallet { | ||
|
||
@BeforeEach | ||
public void init() { | ||
User user = userService.getUser(userId); | ||
Wallet wallet = user.getWallet(); | ||
wallet.deposit(1000L); | ||
walletRepository.save(wallet); | ||
} | ||
|
||
@Test | ||
@DisplayName("잔고 변경 요청이 동시에 들어와도 재시도를 통해 동시성 문제 해결") | ||
public void updateBalance_success() throws Exception { | ||
ExecutorService executors = Executors.newFixedThreadPool(10); | ||
|
||
for (int i = 0; i < 3; i++) { | ||
executors.submit(() -> { | ||
String request = """ | ||
{ "amount" : 100, "type" : "DEPOSIT"} | ||
"""; | ||
try { | ||
mockMvc.perform(patch("/api/users/balance") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(request) | ||
.header("Authorization", userToken)); | ||
} catch (Exception e) { | ||
|
||
} | ||
}); | ||
} | ||
|
||
executors.shutdown(); | ||
executors.awaitTermination(1, TimeUnit.MINUTES); | ||
|
||
User user = userService.getUser(userId); | ||
Wallet wallet = user.getWallet(); | ||
assertThat(wallet.getBalance()).isEqualTo(1300L); | ||
} | ||
|
||
@Test | ||
@DisplayName("너무 많은 재시도를 시도 하면 예외를 발생시킴") | ||
public void updateBalance_many_retry() throws Exception { | ||
ExecutorService executors = Executors.newFixedThreadPool(10); | ||
|
||
for (int i = 0; i < 10; i++) { | ||
executors.submit(() -> { | ||
String request = """ | ||
{ "amount" : 100, "type" : "DEPOSIT"} | ||
"""; | ||
try { | ||
mockMvc.perform(patch("/api/users/balance") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(request) | ||
.header("Authorization", userToken)); | ||
} catch (Exception e) { | ||
|
||
} | ||
}); | ||
} | ||
|
||
executors.shutdown(); | ||
executors.awaitTermination(1, TimeUnit.MINUTES); | ||
|
||
User user = userService.getUser(userId); | ||
Wallet wallet = user.getWallet(); | ||
assertThat(wallet.getBalance()).isNotEqualTo(2000L); | ||
} | ||
} | ||
} |