Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,4 @@ public List<Room> findAll() {
public void removeRoom(Long roomId) {
roomMap.remove(roomId);
}

// 테스트 전용 메소드
public Room getRoomForTest(Long roomId) {
return roomMap.get(roomId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.core.context.SecurityContextHolder;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -53,6 +54,7 @@ class RoomServiceTests {
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this); // @Mock 어노테이션이 붙은 필드들을 초기화합니다.

roomService =
new RoomService(
timerService, quizService, roomRepository, eventPublisher, messageSender);
Expand Down Expand Up @@ -83,6 +85,7 @@ void enterRoom_synchronized() throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
RoomValidationRequest roomValidationRequest = new RoomValidationRequest(roomId, password);

for (int i = 1; i <= threadCount; i++) {
User user = createUser(i);

Expand Down Expand Up @@ -113,6 +116,7 @@ void exitRoom_synchronized() throws Exception {
String password = "123";
boolean locked = true;

/* 방 생성 */
Room room = createRoom(roomId, playerId, quizId, password, maxUserCount, locked);

int threadCount = 10;
Expand All @@ -128,6 +132,7 @@ void exitRoom_synchronized() throws Exception {
Player host = players.getFirst();
room.updateHost(host);

/* 방 입장 */
for (int i = 1; i <= threadCount; i++) {
String sessionId = "sessionId" + i;
Player player = players.get(i - 1);
Expand All @@ -141,6 +146,7 @@ void exitRoom_synchronized() throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
CountDownLatch countDownLatch = new CountDownLatch(threadCount);

/* 방 퇴장 테스트 */
for (int i = 1; i <= threadCount; i++) {
String sessionId = "sessionId" + i;
User user = createUser(i);
Expand Down Expand Up @@ -190,7 +196,14 @@ private User createUser(int i) {
.providerId(providerId)
.lastLogin(lastLogin)
.build();
user.setId(userId);

try {
Field idField = User.class.getDeclaredField("id");
idField.setAccessible(true);
idField.set(user, userId);
} catch (Exception e) {
throw new RuntimeException("ID 설정 실패", e);
}

return user;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void saveRoom_test() {

roomRepository.saveRoom(newRoom);

Room savedRoom = roomRepository.getRoomForTest(newId);
Room savedRoom = roomRepository.findRoom(newId).orElseThrow();

assertThat(savedRoom.getHost().getId()).isEqualTo(loginUser.get("id"));
assertThat(savedRoom.getHost().getNickname()).isEqualTo(loginUser.get("nickname"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.f1.backend.domain.game.websocket;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
Expand All @@ -24,6 +27,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -41,9 +45,7 @@ class SessionServiceTests {
private String sessionId1 = "session1";
private String sessionId2 = "session2";
private Long userId1 = 100L;
private Long userId2 = 200L;
private Long roomId1 = 1L;
private Long roomId2 = 2L;

@BeforeEach
void setUp() {
Expand Down Expand Up @@ -95,8 +97,7 @@ void handleUserDisconnect_shouldExitIfNotPlayingIfDisconnected() throws Interrup
sessionService.addRoomId(roomId1, sessionId1);
sessionService.addSession(sessionId1, userId1);

User user = new User("provider", "providerId", LocalDateTime.now());
user.setId(userId1);
User user = createUser(1);
UserPrincipal principal = new UserPrincipal(user, new HashMap<>());

// disconnect 호출
Expand Down Expand Up @@ -154,8 +155,7 @@ void handleUserDisconnect_shouldStoreOldSessionIdInLatestSession() {
sessionService.addSession(sessionId1, userId1); // 유저의 현재 활성 세션
sessionService.addRoomId(roomId1, sessionId1);

User user = new User("provider", "providerId", LocalDateTime.now());
user.setId(userId1);
User user = createUser(1);
UserPrincipal principal = new UserPrincipal(user, new HashMap<>());

// when
Expand Down Expand Up @@ -183,8 +183,7 @@ void handleUserDisconnect_reconnectWithin5Seconds_shouldCleanLatestSession()
sessionService.addSession(sessionId1, userId1); // 초기 세션
sessionService.addRoomId(roomId1, sessionId1);

User user = new User("provider", "providerId", LocalDateTime.now());
user.setId(userId1);
User user = createUser(1);
UserPrincipal principal = new UserPrincipal(user, new HashMap<>());

sessionService.handleUserDisconnect(
Expand Down Expand Up @@ -231,4 +230,28 @@ void handleUserDisconnect_reconnectWithin5Seconds_shouldCleanLatestSession()
assertTrue(sessionIdRoom.containsKey(sessionId2));
assertEquals(roomId1, sessionIdRoom.get(sessionId2));
}

private User createUser(int i) {
Long userId = i + 1L;
String provider = "provider +" + i;
String providerId = "providerId" + i;
LocalDateTime lastLogin = LocalDateTime.now();

User user =
User.builder()
.provider(provider)
.providerId(providerId)
.lastLogin(lastLogin)
.build();

try {
Field idField = User.class.getDeclaredField("id");
idField.setAccessible(true);
idField.set(user, userId);
} catch (Exception e) {
throw new RuntimeException("ID 설정 실패", e);
}

return user;
}
}