22
22
import org .junit .jupiter .api .BeforeEach ;
23
23
import org .junit .jupiter .api .DisplayName ;
24
24
import org .junit .jupiter .api .Test ;
25
+ import org .mockito .Mockito ;
25
26
import org .springframework .beans .factory .annotation .Autowired ;
26
27
import org .springframework .boot .test .mock .mockito .MockBean ;
28
+ import org .springframework .boot .test .mock .mockito .SpyBean ;
27
29
import org .springframework .context .annotation .Import ;
28
30
29
31
import java .time .LocalDateTime ;
30
32
import java .util .Map ;
31
33
import java .util .concurrent .CountDownLatch ;
32
34
import java .util .concurrent .ExecutorService ;
33
35
import java .util .concurrent .Executors ;
34
- import java .util .concurrent .atomic .AtomicInteger ;
35
36
36
- import static org .assertj .core .api .Assertions .assertThat ;
37
37
import static org .mockito .ArgumentMatchers .any ;
38
+ import static org .mockito .ArgumentMatchers .anyLong ;
38
39
import static org .mockito .Mockito .when ;
39
40
40
41
@ ServiceTest
@@ -59,16 +60,19 @@ class AutomaticMatchingExecutorTest {
59
60
@ MockBean
60
61
private PullRequestProvider pullRequestProvider ;
61
62
63
+ @ Autowired
64
+ private RoomMatchInfoRepository roomMatchInfoRepository ;
65
+
66
+ @ SpyBean
67
+ MatchingExecutor matchingExecutor ;
68
+
62
69
private Room room ;
63
- private Room emptyParticipantRoom ;
64
70
private Member pororo ;
65
71
private Member ash ;
66
72
private Member joysun ;
67
73
private Member movin ;
68
74
private Member ten ;
69
75
private Member cho ;
70
- @ Autowired
71
- private RoomMatchInfoRepository roomMatchInfoRepository ;
72
76
73
77
@ BeforeEach
74
78
void setUp () {
@@ -79,9 +83,9 @@ void setUp() {
79
83
ten = memberRepository .save (MemberFixture .MEMBER_TENTEN ());
80
84
cho = memberRepository .save (MemberFixture .MEMBER_CHOCO ());
81
85
82
- room = roomRepository .save (RoomFixture .ROOM_DOMAIN (pororo , LocalDateTime .now (). plusSeconds ( 3 )));
83
- roomMatchInfoRepository . save ( new RoomMatchInfo ( room . getId (), true ));
84
- emptyParticipantRoom = roomRepository .save (RoomFixture . ROOM_DOMAIN ( ash , LocalDateTime . now (). plusSeconds ( 3 ) ));
86
+ room = roomRepository .save (RoomFixture .ROOM_DOMAIN (pororo , LocalDateTime .now ()
87
+ . plusSeconds ( 3 ) ));
88
+ roomMatchInfoRepository .save (new RoomMatchInfo ( room . getId (), true ));
85
89
86
90
participationRepository .save (new Participation (room , pororo , MemberRole .BOTH , room .getMatchingSize ()));
87
91
participationRepository .save (new Participation (room , ash , MemberRole .BOTH , room .getMatchingSize ()));
@@ -121,17 +125,14 @@ private PullRequestInfo getPullRequestInfo(Member pororo, Member ash, Member joy
121
125
@ Test
122
126
@ DisplayName ("동시에 10개의 자동 매칭을 실행해도 PESSIMISTIC_WRITE 락을 통해 동시성을 제어할 수 있다." )
123
127
void startMatchingWithLock () throws InterruptedException {
124
- AutomaticMatching automaticMatching = automaticMatchingRepository .save (new AutomaticMatching (room .getId (), LocalDateTime .now ().plusDays (1 )));
128
+ AutomaticMatching automaticMatching = automaticMatchingRepository .save (new AutomaticMatching (room .getId (), LocalDateTime .now ()
129
+ .plusDays (1 )));
125
130
126
131
int threadCount = 10 ;
127
132
ExecutorService executorService = Executors .newFixedThreadPool (threadCount );
128
133
CountDownLatch latch = new CountDownLatch (threadCount );
129
- AtomicInteger successCount = new AtomicInteger (0 );
130
134
131
- when (pullRequestProvider .getUntilDeadline (any (), any ())).thenAnswer (ignore -> {
132
- successCount .incrementAndGet ();
133
- return getPullRequestInfo (pororo , ash , joysun , movin , ten , cho );
134
- });
135
+ when (pullRequestProvider .getUntilDeadline (any (), any ())).thenReturn (getPullRequestInfo (pororo , ash , joysun , movin , ten , cho ));
135
136
136
137
for (int i = 0 ; i < threadCount ; i ++) {
137
138
executorService .execute (() -> {
@@ -144,7 +145,6 @@ void startMatchingWithLock() throws InterruptedException {
144
145
}
145
146
146
147
latch .await ();
147
-
148
- assertThat (successCount .get ()).isEqualTo (1 );
148
+ Mockito .verify (matchingExecutor ,Mockito .times (1 )).match (anyLong ());
149
149
}
150
150
}
0 commit comments