Skip to content

Commit 2bc8f6e

Browse files
committed
♻️ 충돌해결
2 parents f25c170 + 860328e commit 2bc8f6e

File tree

2 files changed

+63
-65
lines changed

2 files changed

+63
-65
lines changed

backend/src/main/java/io/f1/backend/domain/game/app/RoomService.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@
3838
import io.f1.backend.domain.user.dto.UserPrincipal;
3939
import io.f1.backend.global.exception.CustomException;
4040
import io.f1.backend.global.exception.errorcode.RoomErrorCode;
41+
42+
import lombok.RequiredArgsConstructor;
43+
import lombok.extern.slf4j.Slf4j;
44+
45+
import org.springframework.context.ApplicationEventPublisher;
46+
import org.springframework.stereotype.Service;
47+
4148
import java.util.List;
4249
import java.util.Map;
4350
import java.util.Optional;
4451
import java.util.concurrent.ConcurrentHashMap;
4552
import java.util.concurrent.atomic.AtomicLong;
46-
import lombok.RequiredArgsConstructor;
47-
import lombok.extern.slf4j.Slf4j;
48-
import org.springframework.context.ApplicationEventPublisher;
49-
import org.springframework.stereotype.Service;
5053

5154
@Slf4j
5255
@Service
@@ -113,8 +116,7 @@ public void enterRoom(RoomValidationRequest request) {
113116
}
114117
}
115118

116-
public void initializeRoomSocket(
117-
Long roomId, String sessionId, UserPrincipal principal) {
119+
public void initializeRoomSocket(Long roomId, String sessionId, UserPrincipal principal) {
118120

119121
Room room = findRoom(roomId);
120122

@@ -149,14 +151,10 @@ public void initializeRoomSocket(
149151

150152
String destination = getDestination(roomId);
151153

152-
messageSender.send(
153-
destination, MessageType.ROOM_SETTING, roomSettingResponse);
154-
messageSender.send(
155-
destination, MessageType.GAME_SETTING, gameSettingResponse);
156-
messageSender.send(
157-
destination, MessageType.PLAYER_LIST, playerListResponse);
158-
messageSender.send(
159-
destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
154+
messageSender.send(destination, MessageType.ROOM_SETTING, roomSettingResponse);
155+
messageSender.send(destination, MessageType.GAME_SETTING, gameSettingResponse);
156+
messageSender.send(destination, MessageType.PLAYER_LIST, playerListResponse);
157+
messageSender.send(destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
160158
}
161159

162160
public void exitRoom(Long roomId, String sessionId, UserPrincipal principal) {
@@ -189,11 +187,8 @@ public void exitRoom(Long roomId, String sessionId, UserPrincipal principal) {
189187

190188
String destination = getDestination(roomId);
191189

192-
messageSender.send(
193-
destination, MessageType.PLAYER_LIST, playerListResponse);
194-
messageSender.send(
195-
destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
196-
190+
messageSender.send(destination, MessageType.PLAYER_LIST, playerListResponse);
191+
messageSender.send(destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
197192
}
198193
}
199194

@@ -210,7 +205,6 @@ public void handlePlayerReady(Long roomId, String sessionId) {
210205
String destination = getDestination(roomId);
211206

212207
messageSender.send(destination, MessageType.PLAYER_LIST, toPlayerListResponse(room));
213-
214208
}
215209

216210
public RoomListResponse getAllRooms() {
@@ -248,11 +242,13 @@ public void chat(Long roomId, String sessionId, ChatMessage chatMessage) {
248242
room.increasePlayerCorrectCount(sessionId);
249243

250244
messageSender.send(
251-
destination, MessageType.QUESTION_RESULT,
245+
destination,
246+
MessageType.QUESTION_RESULT,
252247
toQuestionResultResponse(currentQuestion.getId(), chatMessage, answer));
253248
messageSender.send(destination, MessageType.RANK_UPDATE, toRankUpdateResponse(room));
254249
messageSender.send(
255-
destination, MessageType.SYSTEM_NOTICE,
250+
destination,
251+
MessageType.SYSTEM_NOTICE,
256252
ofPlayerEvent(chatMessage.nickname(), RoomEventType.ENTER));
257253
}
258254
}
@@ -314,7 +310,6 @@ private void removePlayer(Room room, String sessionId, Player removePlayer) {
314310
room.removeSessionId(sessionId);
315311
}
316312

317-
318313
private String getDestination(Long roomId) {
319314
return "/sub/room/" + roomId;
320315
}

backend/src/test/java/io/f1/backend/domain/game/app/RoomServiceTests.java

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.f1.backend.domain.game.app;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.mockito.Mockito.doNothing;
54
import static org.mockito.Mockito.when;
65

76
import io.f1.backend.domain.game.dto.request.RoomValidationRequest;
@@ -44,15 +43,19 @@ class RoomServiceTests {
4443

4544
private RoomService roomService;
4645

47-
@Mock private RoomRepository roomRepository;
48-
@Mock private QuizService quizService;
49-
@Mock private ApplicationEventPublisher eventPublisher;
50-
@Mock private MessageSender messageSender;
46+
@Mock
47+
private RoomRepository roomRepository;
48+
@Mock
49+
private QuizService quizService;
50+
@Mock
51+
private ApplicationEventPublisher eventPublisher;
52+
@Mock
53+
private MessageSender messageSender;
5154

5255
@BeforeEach
5356
void setUp() {
5457
MockitoAnnotations.openMocks(this); // @Mock 어노테이션이 붙은 필드들을 초기화합니다.
55-
roomService = new RoomService(quizService, roomRepository, eventPublisher,messageSender);
58+
roomService = new RoomService(quizService, roomRepository, eventPublisher, messageSender);
5659

5760
SecurityContextHolder.clearContext();
5861
}
@@ -84,17 +87,17 @@ void enterRoom_synchronized() throws Exception {
8487
User user = createUser(i);
8588

8689
executorService.submit(
87-
() -> {
88-
try {
89-
SecurityUtils.setAuthentication(user);
90-
roomService.enterRoom(roomValidationRequest);
91-
} catch (Exception e) {
92-
e.printStackTrace();
93-
} finally {
94-
SecurityContextHolder.clearContext();
95-
countDownLatch.countDown();
96-
}
97-
});
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+
});
98101
}
99102
countDownLatch.await();
100103
assertThat(room.getUserIdSessionMap()).hasSize(room.getRoomSetting().maxUserCount());
@@ -143,32 +146,32 @@ void exitRoom_synchronized() throws Exception {
143146
String sessionId = "sessionId" + i;
144147
User user = createUser(i);
145148
executorService.submit(
146-
() -> {
147-
try {
148-
UserPrincipal principal =
149-
new UserPrincipal(user, Collections.emptyMap());
150-
SecurityUtils.setAuthentication(user);
151-
log.info("room.getHost().getId() = {}", room.getHost().getId());
152-
roomService.exitRoom(roomId, sessionId, principal);
153-
} catch (Exception e) {
154-
e.printStackTrace();
155-
} finally {
156-
SecurityContextHolder.clearContext();
157-
countDownLatch.countDown();
158-
}
159-
});
149+
() -> {
150+
try {
151+
UserPrincipal principal =
152+
new UserPrincipal(user, Collections.emptyMap());
153+
SecurityUtils.setAuthentication(user);
154+
log.info("room.getHost().getId() = {}", room.getHost().getId());
155+
roomService.exitRoom(roomId, sessionId, principal);
156+
} catch (Exception e) {
157+
e.printStackTrace();
158+
} finally {
159+
SecurityContextHolder.clearContext();
160+
countDownLatch.countDown();
161+
}
162+
});
160163
}
161164
countDownLatch.await();
162165
assertThat(room.getUserIdSessionMap()).hasSize(1);
163166
}
164167

165168
private Room createRoom(
166-
Long roomId,
167-
Long playerId,
168-
Long quizId,
169-
String password,
170-
int maxUserCount,
171-
boolean locked) {
169+
Long roomId,
170+
Long playerId,
171+
Long quizId,
172+
String password,
173+
int maxUserCount,
174+
boolean locked) {
172175
RoomSetting roomSetting = new RoomSetting("방제목", maxUserCount, locked, password);
173176
GameSetting gameSetting = new GameSetting(quizId, 10, 60);
174177
Player host = new Player(playerId, "nickname");
@@ -183,11 +186,11 @@ private User createUser(int i) {
183186
LocalDateTime lastLogin = LocalDateTime.now();
184187

185188
User user =
186-
User.builder()
187-
.provider(provider)
188-
.providerId(providerId)
189-
.lastLogin(lastLogin)
190-
.build();
189+
User.builder()
190+
.provider(provider)
191+
.providerId(providerId)
192+
.lastLogin(lastLogin)
193+
.build();
191194
user.setId(userId);
192195

193196
return user;

0 commit comments

Comments
 (0)