Skip to content

Commit cae5121

Browse files
committed
♻️ test 중복 코드 정리
1 parent 39738fc commit cae5121

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

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

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,11 @@ void enterRoom_synchronized() throws Exception {
6565
Long roomId = 1L;
6666
Long quizId = 1L;
6767
Long playerId = 1L;
68+
int maxUserCount = 5;
6869
String password = "123";
70+
boolean locked = true;
6971

70-
RoomSetting roomSetting = new RoomSetting("방제목", 5, true, password);
71-
GameSetting gameSetting = new GameSetting(quizId, 10, 60);
72-
Player host = new Player(playerId, "닉네임");
73-
74-
Room room = new Room(roomId, roomSetting, gameSetting, host);
72+
Room room = createRoom(roomId, playerId, quizId, password, maxUserCount, locked);
7573

7674
when(roomRepository.findRoom(roomId)).thenReturn(Optional.of(room));
7775

@@ -80,23 +78,14 @@ void enterRoom_synchronized() throws Exception {
8078
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
8179
RoomValidationRequest roomValidationRequest = new RoomValidationRequest(roomId, password);
8280
for (int i = 1; i <= threadCount; i++) {
83-
Long userId = i + 1L;
84-
String provider = "provider +" + i;
85-
String providerId = "providerId" + i;
86-
LocalDateTime lastLogin = LocalDateTime.now();
81+
User user = createUser(i);
82+
8783
executorService.submit(() -> {
8884
try {
89-
User user = User.builder()
90-
.provider(provider)
91-
.provider(providerId).lastLogin(lastLogin).build();
92-
93-
user.setId(userId);
94-
9585
SecurityUtils.setAuthentication(user);
96-
9786
roomService.enterRoom(roomValidationRequest);
9887
} catch (Exception e) {
99-
//e.printStackTrace();
88+
e.printStackTrace();
10089
} finally {
10190
SecurityContextHolder.clearContext();
10291
countDownLatch.countDown();
@@ -113,12 +102,14 @@ void enterRoom_synchronized() throws Exception {
113102
void exitRoom_synchronized() throws Exception {
114103
Long roomId = 1L;
115104
Long quizId = 1L;
116-
int threadCount = 10;
117-
105+
Long playerId = 1L;
106+
int maxUserCount = 5;
118107
String password = "123";
108+
boolean locked = true;
119109

120-
RoomSetting roomSetting = new RoomSetting("방제목", 5, true, password);
121-
GameSetting gameSetting = new GameSetting(quizId, 10, 60);
110+
Room room = createRoom(roomId, playerId, quizId, password, maxUserCount, locked);
111+
112+
int threadCount = 10;
122113

123114
List<Player> players = new ArrayList<>();
124115
for (int i = 1; i <=threadCount; i++) {
@@ -127,10 +118,10 @@ void exitRoom_synchronized() throws Exception {
127118

128119
Player player = new Player(id, nickname);
129120
players.add(player);
130-
131121
}
132122
Player host = players.getFirst();
133-
Room room = new Room(roomId, roomSetting, gameSetting, host);
123+
room.updateHost(host);
124+
134125

135126
for (int i = 1; i <=threadCount; i++) {
136127
String sessionId = "sessionId" + i;
@@ -148,20 +139,11 @@ void exitRoom_synchronized() throws Exception {
148139
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
149140

150141
for (int i = 1; i <= threadCount; i++) {
151-
Long userId = i + 1L;
152142
String sessionId = "sessionId" + i;
153-
String provider = "provider +" + i;
154-
String providerId = "providerId" + i;
155-
LocalDateTime lastLogin = LocalDateTime.now();
143+
User user = createUser(i);
156144
executorService.submit(() -> {
157145
try {
158-
User user = User.builder()
159-
.provider(provider)
160-
.provider(providerId).lastLogin(lastLogin).build();
161-
user.setId(userId);
162146
SecurityUtils.setAuthentication(user);
163-
164-
log.info("userId = {}", userId);
165147
log.info("room.getHost().getId() = {}", room.getHost().getId());
166148
roomService.exitRoom(roomId, sessionId);
167149
} catch (Exception e) {
@@ -176,6 +158,26 @@ void exitRoom_synchronized() throws Exception {
176158
assertThat(room.getUserIdSessionMap()).hasSize(1);
177159
}
178160

161+
private Room createRoom(Long roomId, Long playerId, Long quizId, String password, int maxUserCount, boolean locked) {
162+
RoomSetting roomSetting = new RoomSetting("방제목", maxUserCount, locked, password);
163+
GameSetting gameSetting = new GameSetting(quizId, 10, 60);
164+
Player host = new Player(playerId, "nickname");
165+
166+
return new Room(roomId, roomSetting, gameSetting, host);
167+
}
168+
169+
private User createUser(int i) {
170+
Long userId = i + 1L;
171+
String provider = "provider +" + i;
172+
String providerId = "providerId" + i;
173+
LocalDateTime lastLogin = LocalDateTime.now();
179174

175+
User user = User.builder()
176+
.provider(provider)
177+
.provider(providerId).lastLogin(lastLogin).build();
178+
user.setId(userId);
179+
180+
return user;
181+
}
180182

181183
}

0 commit comments

Comments
 (0)