Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore: ๐Ÿ› Distributed Lock Failure Causing Duplicate Chat Room Joins #236

Merged
merged 4 commits into from
Feb 6, 2025
Merged
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 @@ -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;
}
}
}