1616import io .f1 .backend .domain .user .entity .User ;
1717import io .f1 .backend .global .util .SecurityUtils ;
1818
19+ import java .lang .reflect .Field ;
1920import lombok .extern .slf4j .Slf4j ;
2021
2122import org .junit .jupiter .api .AfterEach ;
@@ -44,18 +45,24 @@ class RoomServiceTests {
4445
4546 private RoomService roomService ;
4647
47- @ Mock private RoomRepository roomRepository ;
48- @ Mock private QuizService quizService ;
49- @ Mock private TimerService timerService ;
50- @ Mock private ApplicationEventPublisher eventPublisher ;
51- @ Mock private MessageSender messageSender ;
48+ @ Mock
49+ private RoomRepository roomRepository ;
50+ @ Mock
51+ private QuizService quizService ;
52+ @ Mock
53+ private TimerService timerService ;
54+ @ Mock
55+ private ApplicationEventPublisher eventPublisher ;
56+ @ Mock
57+ private MessageSender messageSender ;
5258
5359 @ BeforeEach
5460 void setUp () {
5561 MockitoAnnotations .openMocks (this ); // @Mock 어노테이션이 붙은 필드들을 초기화합니다.
62+
5663 roomService =
57- new RoomService (
58- timerService , quizService , roomRepository , eventPublisher , messageSender );
64+ new RoomService (
65+ timerService , quizService , roomRepository , eventPublisher , messageSender );
5966
6067 SecurityContextHolder .clearContext ();
6168 }
@@ -83,21 +90,22 @@ void enterRoom_synchronized() throws Exception {
8390 ExecutorService executorService = Executors .newFixedThreadPool (threadCount );
8491 CountDownLatch countDownLatch = new CountDownLatch (threadCount );
8592 RoomValidationRequest roomValidationRequest = new RoomValidationRequest (roomId , password );
93+
8694 for (int i = 1 ; i <= threadCount ; i ++) {
8795 User user = createUser (i );
8896
8997 executorService .submit (
90- () -> {
91- try {
92- SecurityUtils .setAuthentication (user );
93- roomService .enterRoom (roomValidationRequest );
94- } catch (Exception e ) {
95- e .printStackTrace ();
96- } finally {
97- SecurityContextHolder .clearContext ();
98- countDownLatch .countDown ();
99- }
100- });
98+ () -> {
99+ try {
100+ SecurityUtils .setAuthentication (user );
101+ roomService .enterRoom (roomValidationRequest );
102+ } catch (Exception e ) {
103+ e .printStackTrace ();
104+ } finally {
105+ SecurityContextHolder .clearContext ();
106+ countDownLatch .countDown ();
107+ }
108+ });
101109 }
102110 countDownLatch .await ();
103111 assertThat (room .getCurrentUserCnt ()).isEqualTo (room .getRoomSetting ().maxUserCount ());
@@ -113,6 +121,7 @@ void exitRoom_synchronized() throws Exception {
113121 String password = "123" ;
114122 boolean locked = true ;
115123
124+ /* 방 생성 */
116125 Room room = createRoom (roomId , playerId , quizId , password , maxUserCount , locked );
117126
118127 int threadCount = 10 ;
@@ -128,6 +137,7 @@ void exitRoom_synchronized() throws Exception {
128137 Player host = players .getFirst ();
129138 room .updateHost (host );
130139
140+ /* 방 입장 */
131141 for (int i = 1 ; i <= threadCount ; i ++) {
132142 String sessionId = "sessionId" + i ;
133143 Player player = players .get (i - 1 );
@@ -141,36 +151,37 @@ void exitRoom_synchronized() throws Exception {
141151 ExecutorService executorService = Executors .newFixedThreadPool (threadCount );
142152 CountDownLatch countDownLatch = new CountDownLatch (threadCount );
143153
154+ /* 방 퇴장 테스트 */
144155 for (int i = 1 ; i <= threadCount ; i ++) {
145156 String sessionId = "sessionId" + i ;
146157 User user = createUser (i );
147158 executorService .submit (
148- () -> {
149- try {
150- UserPrincipal principal =
151- new UserPrincipal (user , Collections .emptyMap ());
152- SecurityUtils .setAuthentication (user );
153- log .info ("room.getHost().getId() = {}" , room .getHost ().getId ());
154- roomService .exitRoom (roomId , sessionId , principal );
155- } catch (Exception e ) {
156- e .printStackTrace ();
157- } finally {
158- SecurityContextHolder .clearContext ();
159- countDownLatch .countDown ();
160- }
161- });
159+ () -> {
160+ try {
161+ UserPrincipal principal =
162+ new UserPrincipal (user , Collections .emptyMap ());
163+ SecurityUtils .setAuthentication (user );
164+ log .info ("room.getHost().getId() = {}" , room .getHost ().getId ());
165+ roomService .exitRoom (roomId , sessionId , principal );
166+ } catch (Exception e ) {
167+ e .printStackTrace ();
168+ } finally {
169+ SecurityContextHolder .clearContext ();
170+ countDownLatch .countDown ();
171+ }
172+ });
162173 }
163174 countDownLatch .await ();
164175 verify (roomRepository ).removeRoom (roomId );
165176 }
166177
167178 private Room createRoom (
168- Long roomId ,
169- Long playerId ,
170- Long quizId ,
171- String password ,
172- int maxUserCount ,
173- boolean locked ) {
179+ Long roomId ,
180+ Long playerId ,
181+ Long quizId ,
182+ String password ,
183+ int maxUserCount ,
184+ boolean locked ) {
174185 RoomSetting roomSetting = new RoomSetting ("방제목" , maxUserCount , locked , password );
175186 GameSetting gameSetting = new GameSetting (quizId , 10 , 60 );
176187 Player host = new Player (playerId , "nickname" );
@@ -185,12 +196,19 @@ private User createUser(int i) {
185196 LocalDateTime lastLogin = LocalDateTime .now ();
186197
187198 User user =
188- User .builder ()
189- .provider (provider )
190- .providerId (providerId )
191- .lastLogin (lastLogin )
192- .build ();
193- user .setId (userId );
199+ User .builder ()
200+ .provider (provider )
201+ .providerId (providerId )
202+ .lastLogin (lastLogin )
203+ .build ();
204+
205+ try {
206+ Field idField = User .class .getDeclaredField ("id" );
207+ idField .setAccessible (true );
208+ idField .set (user , userId );
209+ } catch (Exception e ) {
210+ throw new RuntimeException ("ID 설정 실패" , e );
211+ }
194212
195213 return user ;
196214 }
0 commit comments