Skip to content

Commit cfc104a

Browse files
author
github-actions
committed
chore: Java 스타일 수정
1 parent 4ffea60 commit cfc104a

File tree

3 files changed

+99
-94
lines changed

3 files changed

+99
-94
lines changed

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

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@
3131
import io.f1.backend.domain.game.store.RoomRepository;
3232
import io.f1.backend.domain.quiz.app.QuizService;
3333
import io.f1.backend.domain.quiz.entity.Quiz;
34-
import java.util.List;
35-
import java.util.Map;
36-
import java.util.Optional;
37-
import java.util.concurrent.ConcurrentHashMap;
38-
import java.util.concurrent.atomic.AtomicLong;
34+
3935
import lombok.RequiredArgsConstructor;
4036
import lombok.extern.slf4j.Slf4j;
37+
4138
import org.hibernate.boot.model.naming.IllegalIdentifierException;
4239
import org.springframework.context.ApplicationEventPublisher;
4340
import org.springframework.stereotype.Service;
4441

42+
import java.util.List;
43+
import java.util.Map;
44+
import java.util.Optional;
45+
import java.util.concurrent.ConcurrentHashMap;
46+
import java.util.concurrent.atomic.AtomicLong;
47+
4548
@Slf4j
4649
@Service
4750
@RequiredArgsConstructor
@@ -69,7 +72,7 @@ public RoomCreateResponse saveRoom(RoomCreateRequest request) {
6972

7073
Room room = new Room(newId, roomSetting, gameSetting, host);
7174

72-
room.getUserIdSessionMap().put(host.id,PENDING_SESSION_ID);
75+
room.getUserIdSessionMap().put(host.id, PENDING_SESSION_ID);
7376

7477
roomRepository.saveRoom(room);
7578

@@ -85,7 +88,6 @@ public void enterRoom(RoomValidationRequest request) {
8588
Object lock = roomLocks.computeIfAbsent(roomId, k -> new Object());
8689

8790
synchronized (lock) {
88-
8991
Room room = findRoom(request.roomId());
9092

9193
if (room.getState().equals(RoomState.PLAYING)) {
@@ -99,7 +101,7 @@ public void enterRoom(RoomValidationRequest request) {
99101
}
100102

101103
if (room.getRoomSetting().locked()
102-
&& !room.getRoomSetting().password().equals(request.password())) {
104+
&& !room.getRoomSetting().password().equals(request.password())) {
103105
throw new IllegalArgumentException("401 비밀번호가 일치하지 않습니다.");
104106
}
105107

@@ -116,7 +118,7 @@ public RoomInitialData initializeRoomSocket(Long roomId, String sessionId) {
116118
Map<String, Player> playerSessionMap = room.getPlayerSessionMap();
117119
Map<Long, String> userIdSessionMap = room.getUserIdSessionMap();
118120

119-
if(room.isHost(player.getId())) {
121+
if (room.isHost(player.getId())) {
120122
player.toggleReady();
121123
}
122124

@@ -133,47 +135,46 @@ public RoomInitialData initializeRoomSocket(Long roomId, String sessionId) {
133135
Quiz quiz = quizService.getQuizById(quizId);
134136

135137
GameSettingResponse gameSettingResponse =
136-
toGameSettingResponse(room.getGameSetting(), quiz);
138+
toGameSettingResponse(room.getGameSetting(), quiz);
137139

138140
PlayerListResponse playerListResponse = toPlayerListResponse(room);
139141

140142
SystemNoticeResponse systemNoticeResponse = ofPlayerEvent(player, RoomEventType.ENTER);
141143

142144
return new RoomInitialData(
143-
getDestination(roomId),
144-
roomSettingResponse,
145-
gameSettingResponse,
146-
playerListResponse,
147-
systemNoticeResponse);
145+
getDestination(roomId),
146+
roomSettingResponse,
147+
gameSettingResponse,
148+
playerListResponse,
149+
systemNoticeResponse);
148150
}
149151

150152
public RoomExitData exitRoom(Long roomId, String sessionId) {
151153

152154
Object lock = roomLocks.computeIfAbsent(roomId, k -> new Object());
153155

154156
synchronized (lock) {
155-
156157
Room room = findRoom(roomId);
157158

158159
String destination = getDestination(roomId);
159160

160-
Player removePlayer = getRemovePlayer(room,sessionId);
161+
Player removePlayer = getRemovePlayer(room, sessionId);
161162

162163
/* 방 삭제 */
163-
if (isLastPlayer(room,sessionId)) {
164-
return removeRoom(room,destination);
164+
if (isLastPlayer(room, sessionId)) {
165+
return removeRoom(room, destination);
165166
}
166167

167168
/* 방장 변경 */
168169
if (room.isHost(removePlayer.getId())) {
169-
changeHost(room,sessionId);
170+
changeHost(room, sessionId);
170171
}
171172

172173
/* 플레이어 삭제 */
173-
removePlayer(room,sessionId,removePlayer);
174+
removePlayer(room, sessionId, removePlayer);
174175

175176
SystemNoticeResponse systemNoticeResponse =
176-
ofPlayerEvent(removePlayer, RoomEventType.EXIT);
177+
ofPlayerEvent(removePlayer, RoomEventType.EXIT);
177178

178179
PlayerListResponse playerListResponse = toPlayerListResponse(room);
179180

@@ -184,15 +185,15 @@ public RoomExitData exitRoom(Long roomId, String sessionId) {
184185
public RoomListResponse getAllRooms() {
185186
List<Room> rooms = roomRepository.findAll();
186187
List<RoomResponse> roomResponses =
187-
rooms.stream()
188-
.map(
189-
room -> {
190-
Long quizId = room.getGameSetting().getQuizId();
191-
Quiz quiz = quizService.getQuizById(quizId);
192-
193-
return toRoomResponse(room, quiz);
194-
})
195-
.toList();
188+
rooms.stream()
189+
.map(
190+
room -> {
191+
Long quizId = room.getGameSetting().getQuizId();
192+
Quiz quiz = quizService.getQuizById(quizId);
193+
194+
return toRoomResponse(room, quiz);
195+
})
196+
.toList();
196197
return new RoomListResponse(roomResponses);
197198
}
198199

@@ -215,8 +216,8 @@ private Player createPlayer() {
215216

216217
private Room findRoom(Long roomId) {
217218
return roomRepository
218-
.findRoom(roomId)
219-
.orElseThrow(() -> new IllegalArgumentException("404 존재하지 않는 방입니다."));
219+
.findRoom(roomId)
220+
.orElseThrow(() -> new IllegalArgumentException("404 존재하지 않는 방입니다."));
220221
}
221222

222223
private boolean isLastPlayer(Room room, String sessionId) {
@@ -235,15 +236,17 @@ private RoomExitData removeRoom(Room room, String destination) {
235236
private void changeHost(Room room, String hostSessionId) {
236237
Map<String, Player> playerSessionMap = room.getPlayerSessionMap();
237238

238-
Optional<String> nextHostSessionId = playerSessionMap.keySet().stream()
239-
.filter(key -> !key.equals(hostSessionId)).findFirst();
239+
Optional<String> nextHostSessionId =
240+
playerSessionMap.keySet().stream()
241+
.filter(key -> !key.equals(hostSessionId))
242+
.findFirst();
240243

241244
Player nextHost =
242-
playerSessionMap.get(
243-
nextHostSessionId.orElseThrow(
244-
() ->
245-
new IllegalArgumentException(
246-
"방장 교체 불가 - 404 해당 세션 플레이어는 존재하지않습니다.")));
245+
playerSessionMap.get(
246+
nextHostSessionId.orElseThrow(
247+
() ->
248+
new IllegalArgumentException(
249+
"방장 교체 불가 - 404 해당 세션 플레이어는 존재하지않습니다.")));
247250

248251
room.updateHost(nextHost);
249252
log.info("user_id:{} 방장 변경 완료 ", nextHost.getId());
@@ -253,5 +256,4 @@ private void removePlayer(Room room, String sessionId, Player removePlayer) {
253256
room.removeUserId(removePlayer.getId());
254257
room.removeSessionId(sessionId);
255258
}
256-
257259
}

backend/src/main/java/io/f1/backend/domain/game/model/Room.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ public Room(Long id, RoomSetting roomSetting, GameSetting gameSetting, Player ho
3838
this.host = host;
3939
}
4040

41-
public boolean isHost(Long id){
41+
public boolean isHost(Long id) {
4242
return this.host.getId().equals(id);
4343
}
4444

4545
public void updateHost(Player nextHost) {
4646
this.host = nextHost;
4747
}
4848

49-
public void removeUserId(Long id){
49+
public void removeUserId(Long id) {
5050
this.userIdSessionMap.remove(id);
5151
}
5252

53-
public void removeSessionId(String sessionId){
53+
public void removeSessionId(String sessionId) {
5454
this.playerSessionMap.remove(sessionId);
5555
}
5656
}

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

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@
1313
import io.f1.backend.domain.quiz.app.QuizService;
1414
import io.f1.backend.domain.user.entity.User;
1515
import io.f1.backend.global.util.SecurityUtils;
16-
import java.time.LocalDateTime;
17-
import java.util.ArrayList;
18-
import java.util.List;
19-
import java.util.Optional;
20-
import java.util.concurrent.CountDownLatch;
21-
import java.util.concurrent.ExecutorService;
22-
import java.util.concurrent.Executors;
16+
2317
import lombok.extern.slf4j.Slf4j;
18+
2419
import org.junit.jupiter.api.AfterEach;
2520
import org.junit.jupiter.api.BeforeEach;
2621
import org.junit.jupiter.api.DisplayName;
@@ -32,22 +27,26 @@
3227
import org.springframework.context.ApplicationEventPublisher;
3328
import org.springframework.security.core.context.SecurityContextHolder;
3429

30+
import java.time.LocalDateTime;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
import java.util.Optional;
34+
import java.util.concurrent.CountDownLatch;
35+
import java.util.concurrent.ExecutorService;
36+
import java.util.concurrent.Executors;
37+
3538
@Slf4j
3639
@ExtendWith(MockitoExtension.class)
3740
class RoomServiceTests {
3841

3942
private RoomService roomService;
4043

41-
@Mock
42-
private RoomRepository roomRepository;
43-
@Mock
44-
private QuizService quizService;
45-
@Mock
46-
private ApplicationEventPublisher eventPublisher;
47-
44+
@Mock private RoomRepository roomRepository;
45+
@Mock private QuizService quizService;
46+
@Mock private ApplicationEventPublisher eventPublisher;
4847

4948
@BeforeEach
50-
void setUp(){
49+
void setUp() {
5150
MockitoAnnotations.openMocks(this); // @Mock 어노테이션이 붙은 필드들을 초기화합니다.
5251
roomService = new RoomService(quizService, roomRepository, eventPublisher);
5352

@@ -80,23 +79,23 @@ void enterRoom_synchronized() throws Exception {
8079
for (int i = 1; i <= threadCount; i++) {
8180
User user = createUser(i);
8281

83-
executorService.submit(() -> {
84-
try {
85-
SecurityUtils.setAuthentication(user);
86-
roomService.enterRoom(roomValidationRequest);
87-
} catch (Exception e) {
88-
e.printStackTrace();
89-
} finally {
90-
SecurityContextHolder.clearContext();
91-
countDownLatch.countDown();
92-
}
93-
});
82+
executorService.submit(
83+
() -> {
84+
try {
85+
SecurityUtils.setAuthentication(user);
86+
roomService.enterRoom(roomValidationRequest);
87+
} catch (Exception e) {
88+
e.printStackTrace();
89+
} finally {
90+
SecurityContextHolder.clearContext();
91+
countDownLatch.countDown();
92+
}
93+
});
9494
}
9595
countDownLatch.await();
9696
assertThat(room.getUserIdSessionMap()).hasSize(room.getRoomSetting().maxUserCount());
9797
}
9898

99-
10099
@Test
101100
@DisplayName("exitRoom_동시성_테스트")
102101
void exitRoom_synchronized() throws Exception {
@@ -112,7 +111,7 @@ void exitRoom_synchronized() throws Exception {
112111
int threadCount = 10;
113112

114113
List<Player> players = new ArrayList<>();
115-
for (int i = 1; i <=threadCount; i++) {
114+
for (int i = 1; i <= threadCount; i++) {
116115
Long id = i + 1L;
117116
String nickname = "nickname " + i;
118117

@@ -122,12 +121,11 @@ void exitRoom_synchronized() throws Exception {
122121
Player host = players.getFirst();
123122
room.updateHost(host);
124123

125-
126-
for (int i = 1; i <=threadCount; i++) {
124+
for (int i = 1; i <= threadCount; i++) {
127125
String sessionId = "sessionId" + i;
128126
Player player = players.get(i - 1);
129-
room.getPlayerSessionMap().put(sessionId,player);
130-
room.getUserIdSessionMap().put(player.getId(),sessionId);
127+
room.getPlayerSessionMap().put(sessionId, player);
128+
room.getUserIdSessionMap().put(player.getId(), sessionId);
131129
}
132130

133131
log.info("room.getPlayerSessionMap().size() = {}", room.getPlayerSessionMap().size());
@@ -141,24 +139,31 @@ void exitRoom_synchronized() throws Exception {
141139
for (int i = 1; i <= threadCount; i++) {
142140
String sessionId = "sessionId" + i;
143141
User user = createUser(i);
144-
executorService.submit(() -> {
145-
try {
146-
SecurityUtils.setAuthentication(user);
147-
log.info("room.getHost().getId() = {}", room.getHost().getId());
148-
roomService.exitRoom(roomId, sessionId);
149-
} catch (Exception e) {
150-
e.printStackTrace();
151-
} finally {
152-
SecurityContextHolder.clearContext();
153-
countDownLatch.countDown();
154-
}
155-
});
142+
executorService.submit(
143+
() -> {
144+
try {
145+
SecurityUtils.setAuthentication(user);
146+
log.info("room.getHost().getId() = {}", room.getHost().getId());
147+
roomService.exitRoom(roomId, sessionId);
148+
} catch (Exception e) {
149+
e.printStackTrace();
150+
} finally {
151+
SecurityContextHolder.clearContext();
152+
countDownLatch.countDown();
153+
}
154+
});
156155
}
157156
countDownLatch.await();
158157
assertThat(room.getUserIdSessionMap()).hasSize(1);
159158
}
160159

161-
private Room createRoom(Long roomId, Long playerId, Long quizId, String password, int maxUserCount, boolean locked) {
160+
private Room createRoom(
161+
Long roomId,
162+
Long playerId,
163+
Long quizId,
164+
String password,
165+
int maxUserCount,
166+
boolean locked) {
162167
RoomSetting roomSetting = new RoomSetting("방제목", maxUserCount, locked, password);
163168
GameSetting gameSetting = new GameSetting(quizId, 10, 60);
164169
Player host = new Player(playerId, "nickname");
@@ -172,12 +177,10 @@ private User createUser(int i) {
172177
String providerId = "providerId" + i;
173178
LocalDateTime lastLogin = LocalDateTime.now();
174179

175-
User user = User.builder()
176-
.provider(provider)
177-
.provider(providerId).lastLogin(lastLogin).build();
180+
User user =
181+
User.builder().provider(provider).provider(providerId).lastLogin(lastLogin).build();
178182
user.setId(userId);
179183

180184
return user;
181185
}
182-
183-
}
186+
}

0 commit comments

Comments
 (0)