Skip to content

Commit 111bc5f

Browse files
committed
fix: 동일한 유저가 서로 다른 주문을 동시에 수행하도록 테스트 수정
1 parent e6ed44e commit 111bc5f

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

apps/commerce-api/src/test/java/com/loopers/application/order/OrderConcurrencyTest.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,27 @@ void tearDown() {
5151
class PointConcurrency {
5252

5353
private User savedUser;
54-
private Product savedProduct;
54+
private List<Product> distinctProducts;
5555

5656
@BeforeEach
5757
void setUp() {
5858
User user = new User("testUser", "test@email.com", "1990-01-01", User.Gender.MALE,
5959
new Point(10000L));
6060
this.savedUser = userJpaRepository.saveAndFlush(user);
6161

62-
Product product = new Product(1l, "테스트상품", "상품 설명", new Money(1000L), 100);
63-
this.savedProduct = productJpaRepository.saveAndFlush(product);
62+
this.distinctProducts = IntStream.range(0, 10)
63+
.mapToObj(i -> {
64+
Product product = new Product(1l, "상품" + i, "설명", new Money(1000L), 100);
65+
return productJpaRepository.saveAndFlush(product);
66+
})
67+
.toList();
6468
}
6569

6670
@Test
6771
@DisplayName("동시에 10번 주문 시, 비관적 락으로 인해 포인트는 순차적으로 정확히 차감되어야 한다.")
6872
void point_deduction_concurrency_test() throws InterruptedException {
6973
// arrange
7074
int threadCount = 10;
71-
PlaceOrder command = new PlaceOrder(savedUser.getId(),
72-
List.of(new Item(savedProduct.getId(), 1)));
7375

7476
ExecutorService executorService = Executors.newFixedThreadPool(32);
7577
CountDownLatch latch = new CountDownLatch(threadCount);
@@ -79,8 +81,12 @@ void point_deduction_concurrency_test() throws InterruptedException {
7981

8082
// act
8183
for (int i = 0; i < threadCount; i++) {
84+
final int index = i;
8285
executorService.submit(() -> {
8386
try {
87+
Product targetProduct = distinctProducts.get(index);
88+
PlaceOrder command = new PlaceOrder(savedUser.getId(),
89+
List.of(new Item(targetProduct.getId(), 1)));
8490
orderFacade.placeOrder(command);
8591
successCount.getAndIncrement();
8692
} catch (Exception e) {

0 commit comments

Comments
 (0)