Skip to content

Commit

Permalink
test: the same user's multi requests thest
Browse files Browse the repository at this point in the history
  • Loading branch information
psychology50 committed Feb 6, 2025
1 parent bc80ada commit 0d1c7c0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import kr.co.pennyway.domain.domains.chatroom.exception.ChatRoomErrorCode;
import kr.co.pennyway.domain.domains.chatroom.exception.ChatRoomErrorException;
import kr.co.pennyway.domain.domains.member.domain.ChatMember;
import kr.co.pennyway.domain.domains.member.exception.ChatMemberErrorCode;
import kr.co.pennyway.domain.domains.member.exception.ChatMemberErrorException;
import kr.co.pennyway.domain.domains.user.domain.User;
import kr.co.pennyway.domain.domains.user.exception.UserErrorCode;
import kr.co.pennyway.domain.domains.user.exception.UserErrorException;
Expand Down Expand Up @@ -49,11 +47,6 @@ public class ChatMemberJoinService {
public Triple<ChatRoom, Integer, Long> execute(Long userId, Long chatRoomId, Integer password) {
ChatRoom chatRoom = chatRoomService.readChatRoom(chatRoomId).orElseThrow(() -> new ChatRoomErrorException(ChatRoomErrorCode.NOT_FOUND_CHAT_ROOM));

if (chatMemberService.isExists(chatRoomId, userId)) {
log.warn("이미 μ±„νŒ…λ°©μ— μ°Έμ—¬ν•œ μ‚¬μš©μžμž…λ‹ˆλ‹€. chatRoomId: {}, userId: {}", chatRoomId, userId);
throw new ChatMemberErrorException(ChatMemberErrorCode.ALREADY_JOINED);
}

Long currentMemberCount = chatMemberService.countActiveMembers(chatRoomId);
if (isFullRoom(currentMemberCount)) {
log.warn("μ±„νŒ…λ°©μ΄ 가득 μ°ΌμŠ΅λ‹ˆλ‹€. chatRoomId: {}", chatRoomId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import kr.co.pennyway.domain.domains.chatroom.domain.ChatRoom;
import kr.co.pennyway.domain.domains.chatroom.exception.ChatRoomErrorCode;
import kr.co.pennyway.domain.domains.chatroom.repository.ChatRoomRepository;
import kr.co.pennyway.domain.domains.member.exception.ChatMemberErrorCode;
import kr.co.pennyway.domain.domains.member.repository.ChatMemberRepository;
import kr.co.pennyway.domain.domains.user.domain.User;
import kr.co.pennyway.domain.domains.user.repository.UserRepository;
Expand Down Expand Up @@ -220,7 +221,44 @@ void successJoinPrivateRoomWithValidPassword() {

// then
assertEquals(HttpStatus.OK, response.getStatusCode());
}

@Test
@DisplayName("같은 μ‚¬μš©μžκ°€ ν•˜λ‚˜μ˜ μ±„νŒ…λ°©μ— λ™μ‹œμ— 100개의 μš”μ²­μ„ 보내면, 100개의 κ°€μž… μš”μ²­ 쀑 1개만 μ„±κ³΅ν•œλ‹€")
void concurrentJoinRequestsFromSameUser() throws InterruptedException {
// given
User admin = userRepository.save(UserFixture.GENERAL_USER.toUser());
ChatRoom chatRoom = chatRoomRepository.save(ChatRoomFixture.PUBLIC_CHAT_ROOM.toEntity(idGenerator.generate()));
chatMemberRepository.save(ChatMemberFixture.ADMIN.toEntity(admin, chatRoom));

User user = userRepository.save(UserFixture.GENERAL_USER.toUser());

// when
CountDownLatch latch = new CountDownLatch(100);
List<CompletableFuture<JoinResult>> futures = IntStream.range(0, 100)
.mapToObj(i -> CompletableFuture.supplyAsync(() -> {
try {
return JoinResult.from(
postJoining(user, chatRoom.getId(), new ChatMemberReq.Join(null))
);
} finally {
latch.countDown();
}
}))
.toList();

latch.await();

List<JoinResult> results = futures.stream()
.map(CompletableFuture::join)
.toList();

// then
assertAll(
() -> assertEquals(1, results.stream().filter(JoinResult::isSuccess).count()),
() -> assertEquals(99, results.stream().filter(JoinResult::isAlreadyJoinedError).count()),
() -> assertEquals(2, chatMemberRepository.countByChatRoomIdAndActive(chatRoom.getId()))
);
}

private ResponseEntity<?> postJoining(User user, Long chatRoomId, ChatMemberReq.Join request) {
Expand Down Expand Up @@ -282,5 +320,12 @@ public boolean isFullRoomError() {
}
return false;
}

public boolean isAlreadyJoinedError() {
if (!isSuccess && body instanceof ErrorResponse errorResponse) {
return errorResponse.getCode().equals(ChatMemberErrorCode.ALREADY_JOINED.causedBy().getCode());
}
return false;
}
}
}

0 comments on commit 0d1c7c0

Please sign in to comment.