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

[LIME-131] 이벤트 비동기 처리를 통해 투표 로직과 레디스 로직 분리 #66

Merged
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 @@ -3,8 +3,10 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableAsync
@SpringBootApplication
@EnableScheduling
@ConfigurationPropertiesScan({"com.programmers.lime.global.config.security.jwt", "com.programmers.lime.domains.auth"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.List;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import com.programmers.lime.common.cursor.CursorPageParameters;
Expand All @@ -26,6 +27,10 @@
import com.programmers.lime.error.BusinessException;
import com.programmers.lime.error.EntityNotFoundException;
import com.programmers.lime.error.ErrorCode;
import com.programmers.lime.global.event.ranking.vote.RankingAddEvent;
import com.programmers.lime.global.event.ranking.vote.RankingDecreasePopularityEvent;
import com.programmers.lime.global.event.ranking.vote.RankingDeleteEvent;
import com.programmers.lime.global.event.ranking.vote.RankingUpdateEvent;
import com.programmers.lime.global.util.MemberUtils;
import com.programmers.lime.redis.vote.VoteRankingInfo;
import com.programmers.lime.redis.vote.VoteRedisManager;
Expand All @@ -44,14 +49,15 @@ public class VoteService {
private final MemberUtils memberUtils;
private final ItemReader itemReader;
private final VoteRedisManager voteRedisManager;
private final ApplicationEventPublisher eventPublisher;

public Long createVote(final VoteCreateServiceRequest request) {
final Long memberId = memberUtils.getCurrentMemberId();

validateItemIds(request.item1Id(), request.item2Id());
final Vote vote = voteAppender.append(memberId, request.toImplRequest());

final VoteRankingInfo rankingInfo = getVoteRedis(vote);
voteRedisManager.addRanking(vote.getHobby().toString(), rankingInfo);
final Vote vote = voteAppender.append(memberId, request.toImplRequest());
eventPublisher.publishEvent(new RankingAddEvent(String.valueOf(vote.getHobby()), getVoteRedis(vote)));

return vote.getId();
}
Expand Down Expand Up @@ -84,11 +90,7 @@ private void participate(
voter -> voteManager.reParticipate(itemId, voter),
() -> {
voteManager.participate(vote, memberId, itemId);
voteRedisManager.updateRanking(
vote.getHobby().toString(),
vote.isVoting(),
getVoteRedis(vote)
);
eventPublisher.publishEvent(new RankingUpdateEvent(String.valueOf(vote.getHobby()), vote.isVoting(), getVoteRedis(vote)));
}
);
}
Expand All @@ -98,8 +100,7 @@ public void cancelVote(final Long voteId) {
final Vote vote = voteReader.read(voteId);

voteManager.cancel(vote, memberId);

voteRedisManager.decreasePopularity(vote.getHobby().toString(), getVoteRedis(vote));
eventPublisher.publishEvent(new RankingDecreasePopularityEvent(String.valueOf(vote.getHobby()), getVoteRedis(vote)));
}

public void deleteVote(final Long voteId) {
Expand All @@ -111,8 +112,7 @@ public void deleteVote(final Long voteId) {
}

voteRemover.remove(vote);

voteRedisManager.remove(vote.getHobby().toString(), getVoteRedis(vote));
eventPublisher.publishEvent(new RankingDeleteEvent(String.valueOf(vote.getHobby()), getVoteRedis(vote)));
}

public VoteGetServiceResponse getVote(final Long voteId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.programmers.lime.global.event.ranking.vote;

import com.programmers.lime.redis.vote.VoteRankingInfo;

public record RankingAddEvent(
String hobby,
VoteRankingInfo voteRankingInfo
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.programmers.lime.global.event.ranking.vote;

import com.programmers.lime.redis.vote.VoteRankingInfo;

public record RankingDecreasePopularityEvent(
String hobby,
VoteRankingInfo voteRankingInfo
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.programmers.lime.global.event.ranking.vote;

import com.programmers.lime.redis.vote.VoteRankingInfo;

public record RankingDeleteEvent(
String hobby,
VoteRankingInfo voteRankingInfo
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.programmers.lime.global.event.ranking.vote;

import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.programmers.lime.redis.vote.VoteRedisManager;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class RankingEventListener {

private final VoteRedisManager voteRedisManager;

@Async
@EventListener
public void addRanking(final RankingAddEvent event) {
voteRedisManager.addRanking(event.hobby(), event.voteRankingInfo());
}

@Async
@EventListener
public void updateRanking(final RankingUpdateEvent event) {
voteRedisManager.updateRanking(event.hobby(), event.isVoting(), event.voteRankingInfo());
}

@Async
@EventListener
public void decreasePopularity(final RankingDecreasePopularityEvent event) {
voteRedisManager.updatePopularity(event.hobby(), event.voteRankingInfo(), -1);
}

@Async
@EventListener
public void deleteRanking(final RankingDeleteEvent event) {
voteRedisManager.deleteRanking(event.hobby(), event.voteRankingInfo());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.programmers.lime.global.event.ranking.vote;

import com.programmers.lime.redis.vote.VoteRankingInfo;

public record RankingUpdateEvent(
String hobby,
boolean isVoting,
VoteRankingInfo voteRankingInfo
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import com.programmers.lime.error.EntityNotFoundException;
import com.programmers.lime.error.ErrorCode;
import com.programmers.lime.global.util.MemberUtils;
import com.programmers.lime.redis.vote.VoteRankingInfo;
import com.programmers.lime.redis.vote.VoteRedisManager;

class VoteServiceTest extends IntegrationTest {

Expand All @@ -59,9 +57,6 @@ class VoteServiceTest extends IntegrationTest {
@MockBean
private MemberUtils memberUtils;

@MockBean
private VoteRedisManager voteRedisManager;

private Item item1;
private Item item2;
private Vote vote;
Expand Down Expand Up @@ -94,26 +89,11 @@ void createVoteTest() {
given(memberUtils.getCurrentMemberId())
.willReturn(1L);

willDoNothing()
.given(voteRedisManager)
.addRanking(anyString(), any(VoteRankingInfo.class));

// when
final Long result = voteService.createVote(request);

// then
assertThat(result).isNotNull();

// verify
then(voteRedisManager).should(times(1))
.addRanking(
String.valueOf(Hobby.BASKETBALL),
VoteRankingInfo.builder()
.id(Long.MAX_VALUE - result)
.item1Image(item1.getImage())
.item2Image(item2.getImage())
.build()
);
}

@Test
Expand All @@ -136,9 +116,6 @@ void createVoteWithSameItemTest() {
assertThatThrownBy(() -> voteService.createVote(request))
.isInstanceOf(BusinessException.class)
.hasFieldOrPropertyWithValue("errorCode", ErrorCode.VOTE_ITEM_DUPLICATED);

// verify
then(voteRedisManager).shouldHaveNoInteractions();
}

@Test
Expand All @@ -162,9 +139,6 @@ void createVoteWithNotExistItemTest() {
assertThatThrownBy(() -> voteService.createVote(request))
.isInstanceOf(EntityNotFoundException.class)
.hasFieldOrPropertyWithValue("errorCode", ErrorCode.ITEM_NOT_FOUND);

// verify
then(voteRedisManager).shouldHaveNoInteractions();
}
}

Expand All @@ -180,27 +154,11 @@ void participateVoteTest() {
given(memberUtils.getCurrentMemberId())
.willReturn(1L);

willDoNothing()
.given(voteRedisManager)
.updateRanking(anyString(), eq(true), any(VoteRankingInfo.class));

// when
voteService.participateVote(voteId, itemId);

// then
assertThat(vote.getVoters()).hasSize(1);

// verify
then(voteRedisManager).should(times(1))
.updateRanking(
String.valueOf(vote.getHobby()),
true,
VoteRankingInfo.builder()
.id(Long.MAX_VALUE - voteId)
.item1Image(item1.getImage())
.item2Image(item2.getImage())
.build()
);
}

@Test
Expand All @@ -221,9 +179,6 @@ void reParticipateVoteTest() {
// then
assertThat(vote.getVoters()).hasSize(1);
assertThat(voter.getItemId()).isEqualTo(reSelectedItemId);

// verify
then(voteRedisManager).shouldHaveNoInteractions();
}

@Test
Expand All @@ -239,9 +194,6 @@ void participateVoteWithClosedVoteTest() {
assertThatThrownBy(() -> voteService.participateVote(voteId, 1L))
.isInstanceOf(BusinessException.class)
.hasFieldOrPropertyWithValue("errorCode", ErrorCode.VOTE_CANNOT_PARTICIPATE);

// verify
then(voteRedisManager).shouldHaveNoInteractions();
}

@Test
Expand All @@ -257,9 +209,6 @@ void participateVoteWithNotExistItemTest() {
assertThatThrownBy(() -> voteService.participateVote(voteId, notExistItemId))
.isInstanceOf(BusinessException.class)
.hasFieldOrPropertyWithValue("errorCode", ErrorCode.VOTE_NOT_CONTAIN_ITEM);

// verify
then(voteRedisManager).shouldHaveNoInteractions();
}
}

Expand All @@ -273,26 +222,11 @@ void cancelVoteTest() {
given(memberUtils.getCurrentMemberId())
.willReturn(memberId);

willDoNothing()
.given(voteRedisManager)
.decreasePopularity(anyString(), any(VoteRankingInfo.class));

// when
voteService.cancelVote(voteId);

// then
assertThat(vote.getVoters()).isEmpty();

// verify
then(voteRedisManager).should(times(1))
.decreasePopularity(
String.valueOf(vote.getHobby()),
VoteRankingInfo.builder()
.id(Long.MAX_VALUE - voteId)
.item1Image(item1.getImage())
.item2Image(item2.getImage())
.build()
);
}

@Nested
Expand All @@ -306,28 +240,13 @@ void deleteVoteTest() {
given(memberUtils.getCurrentMemberId())
.willReturn(memberId);

willDoNothing()
.given(voteRedisManager)
.remove(anyString(), any(VoteRankingInfo.class));

// when
voteService.deleteVote(voteId);

// then
assertThatThrownBy(() -> voteReader.read(voteId)) // 삭제된 투표 조회 시 EntityNotFoundException 발생
.isInstanceOf(EntityNotFoundException.class)
.hasFieldOrPropertyWithValue("errorCode", ErrorCode.VOTE_NOT_FOUND);

// verify
then(voteRedisManager).should(times(1))
.remove(
String.valueOf(vote.getHobby()),
VoteRankingInfo.builder()
.id(Long.MAX_VALUE - voteId)
.item1Image(item1.getImage())
.item2Image(item2.getImage())
.build()
);
}

@Test
Expand All @@ -343,9 +262,6 @@ void deleteVoteWithNotOwnerTest() {
assertThatThrownBy(() -> voteService.deleteVote(voteId))
.isInstanceOf(BusinessException.class)
.hasFieldOrPropertyWithValue("errorCode", ErrorCode.VOTE_NOT_OWNER);

// verify
then(voteRedisManager).shouldHaveNoInteractions();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MemberBuilder {

public static Member build(final Long memberId) {
final SocialInfo socialInfo = new SocialInfo(
"357935205",
357935205L,
"[email protected]",
"1.png",
SocialType.NAVER,
Expand Down
Loading
Loading