Skip to content

Commit

Permalink
fix: 분산락 동시성 문제 해결 (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimyu0218 authored Dec 15, 2024
1 parent b1d7dc2 commit f20a492
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public void joinMission(final Long memberId, final InvitationCode invitationCode
missionValidator.validateMaxPersonnel(mission);
missionMemberRepository.save(MissionMember.join(member, mission));

if (member.isPushActivated()) {
eventPublisher.publishEvent(new JoinMissionEvent(mission.getId(), member.getDeviceToken(), member.getNickname()));
} else {
sendJoinPushMessage(member, mission);

if (!member.isPushActivated()) {
cancelRetryPushMessage(member.getId());
}
}
Expand All @@ -81,6 +81,16 @@ private void validateAlreadyJoin(final Member member, final Mission mission) {
});
}

private void sendJoinPushMessage(final Member member, final Mission mission) {
Member hostMember = memberRepository.getMember(mission.getHostMemberId());

if (hostMember.isPushActivated() && !mission.isHostMember(member.getId())) {
eventPublisher.publishEvent(
new JoinMissionEvent(mission.getId(), hostMember.getDeviceToken(), member.getNickname())
);
}
}

public MissionsResponse findAllByMemberId(final Long memberId, final List<MissionStatus> filter) {
Member member = memberRepository.getMember(memberId);
List<MissionMember> missionMembers = missionMemberRepository.findAllWithMissionByMemberId(memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import org.aspectj.lang.reflect.MethodSignature;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(Ordered.HIGHEST_PRECEDENCE)
@RequiredArgsConstructor
@Slf4j
@Aspect
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/nexters/goalpanzi/domain/mission/Mission.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ public boolean isEndDate(final LocalDate today) {
return today.isEqual(missionEndDate.toLocalDate());
}

/**
* <b>미션 호스트인지 검증</b>
*
* @param memberId
* @return 미션 호스트(생성한 사람) 여부
*/
public boolean isHostMember(final Long memberId) {
return hostMemberId == memberId;
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package com.nexters.goalpanzi.application.mission;

import com.nexters.goalpanzi.application.firebase.TopicGenerator;
import com.nexters.goalpanzi.application.mission.event.JoinMissionEvent;
import com.nexters.goalpanzi.config.redis.RedisInitializer;
import com.nexters.goalpanzi.domain.member.Member;
import com.nexters.goalpanzi.domain.member.repository.MemberRepository;
import com.nexters.goalpanzi.domain.mission.InvitationCode;
import com.nexters.goalpanzi.domain.mission.Mission;
import com.nexters.goalpanzi.domain.mission.repository.MissionMemberRepository;
import com.nexters.goalpanzi.domain.mission.repository.MissionRepository;
import com.nexters.goalpanzi.domain.mission.repository.MissionRetryMessageRepository;
import com.nexters.goalpanzi.infrastructure.firebase.PushMessageSender;
import com.nexters.goalpanzi.infrastructure.firebase.TopicSubscriber;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.MockBeans;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.util.ReflectionTestUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static com.nexters.goalpanzi.domain.firebase.PushMessage.MISSION_CANCELLATION_WARNING;
import static com.nexters.goalpanzi.domain.firebase.PushMessage.MISSION_READY;
import static com.nexters.goalpanzi.fixture.MemberFixture.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

Expand All @@ -30,9 +37,7 @@
initializers = {RedisInitializer.class}
)
@MockBeans({
@MockBean(MemberRepository.class),
@MockBean(MissionRetryMessageRepository.class),
@MockBean(ApplicationEventPublisher.class)
@MockBean(MissionRetryMessageRepository.class)
})
class MissionMemberServiceTest {

Expand All @@ -48,6 +53,12 @@ class MissionMemberServiceTest {
@MockBean
private MissionRepository missionRepository;

@MockBean
private MemberRepository memberRepository;

@MockBean
private ApplicationEventPublisher eventPublisher;

@MockBean
private PushMessageSender pushMessageSender;

Expand All @@ -56,6 +67,70 @@ class MissionMemberServiceTest {

private static Long MISSION_ID = 1L;

@BeforeEach
void setUp() {
ReflectionTestUtils.setField(
missionMemberService, "eventPublisher", eventPublisher
);
}

@Test
void 호스트가_아닌_멤버가_미션에_참여했을_때_JoinMissionEvent를_발행한다() {
InvitationCode INVITATION_CODE = InvitationCode.generate();
Long HOST_ID = MEMBER_ID + 1;

Member mockMember = mock(Member.class);
when(mockMember.getId()).thenReturn(MEMBER_ID);
when(mockMember.getNickname()).thenReturn(NICKNAME_MEMBER_A);

Member mockHostMember = mock(Member.class);
when(mockHostMember.getId()).thenReturn(HOST_ID);
when(mockHostMember.getDeviceToken()).thenReturn(DEVICE_TOKEN);
when(mockHostMember.isPushActivated()).thenReturn(true);

Mission mockMission = mock(Mission.class);
when(mockMission.getId()).thenReturn(MISSION_ID);
when(mockMission.getHostMemberId()).thenReturn(HOST_ID);
when(mockMission.isHostMember(MEMBER_ID)).thenReturn(false);

when(memberRepository.getMember(mockMember.getId())).thenReturn(mockMember);
when(memberRepository.getMember(mockHostMember.getId())).thenReturn(mockHostMember);

when(missionRepository.findByInvitationCode(INVITATION_CODE)).thenReturn(Optional.of(mockMission));
when(missionMemberRepository.findByMemberIdAndMissionId(MEMBER_ID, MISSION_ID)).thenReturn(Optional.empty());

missionMemberService.joinMission(MEMBER_ID, INVITATION_CODE);

verify(eventPublisher).publishEvent(
eq(new JoinMissionEvent(MISSION_ID, DEVICE_TOKEN, NICKNAME_MEMBER_A))
);
}

@Test
void 호스트가_미션에_참여했을_때_JoinMissionEvent를_발행하지_않는다() {
InvitationCode INVITATION_CODE = InvitationCode.generate();

Member mockMember = mock(Member.class);
when(mockMember.getId()).thenReturn(MEMBER_ID);
when(mockMember.getNickname()).thenReturn(NICKNAME_HOST);
when(mockMember.getDeviceToken()).thenReturn(DEVICE_TOKEN);
when(mockMember.isPushActivated()).thenReturn(true);

Mission mockMission = mock(Mission.class);
when(mockMission.getId()).thenReturn(MISSION_ID);
when(mockMission.getHostMemberId()).thenReturn(MEMBER_ID);
when(mockMission.isHostMember(MEMBER_ID)).thenReturn(true);

when(memberRepository.getMember(mockMember.getId())).thenReturn(mockMember);

when(missionRepository.findByInvitationCode(INVITATION_CODE)).thenReturn(Optional.of(mockMission));
when(missionMemberRepository.findByMemberIdAndMissionId(MEMBER_ID, MISSION_ID)).thenReturn(Optional.empty());

missionMemberService.joinMission(MEMBER_ID, INVITATION_CODE);

verifyNoInteractions(eventPublisher);
}

@Test
void 최소_인원을_채워_미션이_곧_시작될_경우_MISSION_READY_푸시_알림을_보낸다() {
Mission mockMission = mock(Mission.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
class MissionMemberEventHandlerTest {

@Autowired
private ApplicationEventPublisher applicationEventPublisher;
private ApplicationEventPublisher eventPublisher;

@Autowired
private TransactionTemplate transactionTemplate;
Expand All @@ -50,7 +50,7 @@ class MissionMemberEventHandlerTest {
UpdateDeviceTokenEvent event = new UpdateDeviceTokenEvent(1L, null, "deviceToken");

transactionTemplate.execute(status -> {
applicationEventPublisher.publishEvent(event);
eventPublisher.publishEvent(event);
return null;
});

Expand All @@ -63,7 +63,7 @@ class MissionMemberEventHandlerTest {
UpdateDeviceTokenEvent event = new UpdateDeviceTokenEvent(1L, "deprecatedDeviceToken", "deviceToken");

transactionTemplate.execute(status -> {
applicationEventPublisher.publishEvent(event);
eventPublisher.publishEvent(event);
return null;
});

Expand All @@ -78,7 +78,7 @@ class MissionMemberEventHandlerTest {
UpdatePushActivationStatusEvent event = new UpdatePushActivationStatusEvent(1L, "deviceToken", true);

transactionTemplate.execute(status -> {
applicationEventPublisher.publishEvent(event);
eventPublisher.publishEvent(event);
return null;
});

Expand All @@ -91,7 +91,7 @@ class MissionMemberEventHandlerTest {
UpdatePushActivationStatusEvent event = new UpdatePushActivationStatusEvent(1L, "deviceToken", false);

transactionTemplate.execute(status -> {
applicationEventPublisher.publishEvent(event);
eventPublisher.publishEvent(event);
return null;
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.nexters.goalpanzi.config.redisson;

import com.nexters.goalpanzi.common.annotation.RedissonLock;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly = true)
public class RedissonLockTestBean {

@RedissonLock(waitTime = 1L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,24 @@ class MissionTest {
() -> assertThat(mission.isEndDate(endDate.minusDays(1).toLocalDate())).isFalse()
);
}

@Test
void 미션_호스트_여부를_검증한다() {
LocalDateTime now = LocalDateTime.now();
Mission mission = Mission.create(
MEMBER_ID,
DESCRIPTION,
now,
now.plusDays(30),
TimeOfDay.EVERYDAY,
List.of(DayOfWeek.FRIDAY),
BOARD_COUNT,
InvitationCode.generate()
);

assertAll(
() -> assertThat(mission.isHostMember(MEMBER_ID)).isTrue(),
() -> assertThat(mission.isHostMember(MEMBER_ID + 1)).isFalse()
);
}
}
9 changes: 8 additions & 1 deletion src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ cloud:
endpoint: https://kr.object.ncloudstorage.com

firebase:
enabled: false
enabled: false

logging:
level:
org:
springframework:
transaction:
interceptor: TRACE

0 comments on commit f20a492

Please sign in to comment.