1
1
package io.hhplus.tdd.point
2
2
3
- import org.junit.jupiter.api.Assertions.*
3
+ import org.junit.jupiter.api.Assertions.assertEquals
4
4
import org.junit.jupiter.api.BeforeEach
5
5
import org.junit.jupiter.api.Test
6
- import org.springframework.boot.test.context.SpringBootTest
7
6
import org.springframework.beans.factory.annotation.Autowired
7
+ import org.springframework.boot.test.context.SpringBootTest
8
8
import java.util.concurrent.Executors
9
9
import java.util.concurrent.TimeUnit
10
10
@@ -15,6 +15,8 @@ class PointServiceConcurrencyTest {
15
15
private lateinit var pointService: PointService
16
16
17
17
private val userId = 1L
18
+ private val threadCount = 10 // 스레드 개수를 변수로 선언
19
+ private val amountPerThread = 10L // 각 스레드마다 충전/사용할 금액을 변수로 선언
18
20
19
21
// 각 테스트 전에 초기 포인트를 설정하는 메소드
20
22
@BeforeEach
@@ -25,16 +27,16 @@ class PointServiceConcurrencyTest {
25
27
26
28
@Test
27
29
fun `동시 충전 테스트` () {
28
- // 10개의 스레드를 생성하여 동시에 충전 요청을 보내기 위해 ThreadPool을 생성
29
- val executor = Executors .newFixedThreadPool(10 )
30
+ // ThreadPool을 생성
31
+ val executor = Executors .newFixedThreadPool(threadCount )
30
32
31
33
// 초기 포인트를 가져옴
32
- val initialPoints = pointService.getUserPoint(1L ).point
34
+ val initialPoints = pointService.getUserPoint(userId ).point
33
35
34
- // 10개의 스레드가 동시에 10포인트씩 충전
35
- repeat(10 ) {
36
+ // 스레드마다 설정된 금액을 동시에 충전
37
+ repeat(threadCount ) {
36
38
executor.submit {
37
- pointService.charge(1L , 10 )
39
+ pointService.charge(userId, amountPerThread )
38
40
}
39
41
}
40
42
@@ -43,23 +45,23 @@ class PointServiceConcurrencyTest {
43
45
executor.awaitTermination(1 , TimeUnit .MINUTES )
44
46
45
47
// 최종 포인트를 가져와서 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)
49
51
}
50
52
51
53
@Test
52
54
fun `동시 사용 테스트` () {
53
- // 10개의 스레드를 생성하여 동시에 사용 요청을 보내기 위해 ThreadPool을 생성
54
- val executor = Executors .newFixedThreadPool(10 )
55
+ // ThreadPool을 생성
56
+ val executor = Executors .newFixedThreadPool(threadCount )
55
57
56
58
// 초기 포인트를 가져옴
57
- val initialPoints = pointService.getUserPoint(1L ).point
59
+ val initialPoints = pointService.getUserPoint(userId ).point
58
60
59
- // 10개의 스레드가 동시에 10포인트씩 사용
60
- repeat(10 ) {
61
+ // 스레드마다 설정된 금액을 동시에 사용
62
+ repeat(threadCount ) {
61
63
executor.submit {
62
- pointService.use(1L , 10 )
64
+ pointService.use(userId, amountPerThread )
63
65
}
64
66
}
65
67
@@ -68,8 +70,8 @@ class PointServiceConcurrencyTest {
68
70
executor.awaitTermination(1 , TimeUnit .MINUTES )
69
71
70
72
// 최종 포인트를 가져와서 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)
74
76
}
75
77
}
0 commit comments