Skip to content

Commit d8c8410

Browse files
committed
Test: 스레드 개수 및 충전/사용 금액을 변수로 추출하여 동시성 테스트 개선
- threadcount 변수를 도입하여 스레드 풀의 크기를 유연하게 조정할 수 있도록 수정 - amountPerThread 변수를 사용하여 각 스레드에서 충전/사용할 금액을 변수화 - 하드코딩 된 값을 제거하여 assertion 실수를 방지하고 테스트 코드의 가독성과 유지보수성을 향상시킴
1 parent 5c09508 commit d8c8410

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package io.hhplus.tdd.point
22

3-
import org.junit.jupiter.api.Assertions.*
3+
import org.junit.jupiter.api.Assertions.assertEquals
44
import org.junit.jupiter.api.BeforeEach
55
import org.junit.jupiter.api.Test
6-
import org.springframework.boot.test.context.SpringBootTest
76
import org.springframework.beans.factory.annotation.Autowired
7+
import org.springframework.boot.test.context.SpringBootTest
88
import java.util.concurrent.Executors
99
import java.util.concurrent.TimeUnit
1010

@@ -15,6 +15,8 @@ class PointServiceConcurrencyTest {
1515
private lateinit var pointService: PointService
1616

1717
private val userId = 1L
18+
private val threadCount = 10 // 스레드 개수를 변수로 선언
19+
private val amountPerThread = 10L // 각 스레드마다 충전/사용할 금액을 변수로 선언
1820

1921
// 각 테스트 전에 초기 포인트를 설정하는 메소드
2022
@BeforeEach
@@ -25,16 +27,16 @@ class PointServiceConcurrencyTest {
2527

2628
@Test
2729
fun `동시 충전 테스트`() {
28-
// 10개의 스레드를 생성하여 동시에 충전 요청을 보내기 위해 ThreadPool을 생성
29-
val executor = Executors.newFixedThreadPool(10)
30+
// ThreadPool을 생성
31+
val executor = Executors.newFixedThreadPool(threadCount)
3032

3133
// 초기 포인트를 가져옴
32-
val initialPoints = pointService.getUserPoint(1L).point
34+
val initialPoints = pointService.getUserPoint(userId).point
3335

34-
// 10개의 스레드가 동시에 10포인트씩 충전
35-
repeat(10) {
36+
// 스레드마다 설정된 금액을 동시에 충전
37+
repeat(threadCount) {
3638
executor.submit {
37-
pointService.charge(1L, 10)
39+
pointService.charge(userId, amountPerThread)
3840
}
3941
}
4042

@@ -43,23 +45,23 @@ class PointServiceConcurrencyTest {
4345
executor.awaitTermination(1, TimeUnit.MINUTES)
4446

4547
// 최종 포인트를 가져와서 10번의 충전 후 예상되는 포인트 값과 일치하는지 확인
46-
val finalPoints = pointService.getUserPoint(1L).point
47-
// 10개의 요청이 모두 완료된 후의 최종 포인트 값이 올바른지 확인
48-
assertEquals(initialPoints + 100, finalPoints) // 10개의 요청, 각각 10포인트 충전
48+
val finalPoints = pointService.getUserPoint(userId).point
49+
// 예상 포인트 값이 정확한지 검증
50+
assertEquals(initialPoints + (threadCount * amountPerThread), finalPoints)
4951
}
5052

5153
@Test
5254
fun `동시 사용 테스트`() {
53-
// 10개의 스레드를 생성하여 동시에 사용 요청을 보내기 위해 ThreadPool을 생성
54-
val executor = Executors.newFixedThreadPool(10)
55+
// ThreadPool을 생성
56+
val executor = Executors.newFixedThreadPool(threadCount)
5557

5658
// 초기 포인트를 가져옴
57-
val initialPoints = pointService.getUserPoint(1L).point
59+
val initialPoints = pointService.getUserPoint(userId).point
5860

59-
// 10개의 스레드가 동시에 10포인트씩 사용
60-
repeat(10) {
61+
// 스레드마다 설정된 금액을 동시에 사용
62+
repeat(threadCount) {
6163
executor.submit {
62-
pointService.use(1L, 10)
64+
pointService.use(userId, amountPerThread)
6365
}
6466
}
6567

@@ -68,8 +70,8 @@ class PointServiceConcurrencyTest {
6870
executor.awaitTermination(1, TimeUnit.MINUTES)
6971

7072
// 최종 포인트를 가져와서 10번의 사용 후 예상되는 포인트 값과 일치하는지 확인
71-
val finalPoints = pointService.getUserPoint(1L).point
72-
// 10개의 요청이 모두 완료된 후의 최종 포인트 값이 올바른지 확인
73-
assertEquals(initialPoints - 100, finalPoints) // 10개의 요청, 각각 10포인트 사용
73+
val finalPoints = pointService.getUserPoint(userId).point
74+
// 예상 포인트 값이 정확한지 검증
75+
assertEquals(initialPoints - (threadCount * amountPerThread), finalPoints)
7476
}
7577
}

0 commit comments

Comments
 (0)