-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from team-offonoff/concurrency/count/optimistic
feat: 투표와 댓글 추가 시 낙관적 락 사용한다
- Loading branch information
Showing
12 changed files
with
230 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/life/offonoff/ab/exception/CommentConcurrencyException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package life.offonoff.ab.exception; | ||
|
||
public class CommentConcurrencyException extends ConcurrencyViolationException { | ||
private static final String HINT = "토픽의 댓글수 업데이트 중 낙관적 락 실패"; | ||
@Override | ||
public String getHint() { | ||
return HINT; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/life/offonoff/ab/exception/ConcurrencyViolationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package life.offonoff.ab.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public abstract class ConcurrencyViolationException extends AbException{ | ||
private static final AbCode AB_CODE = AbCode.CONCURRENCY_VIOLATION; | ||
private static final String MESSAGE = "서버 문제가 발생했습니다. 다시 시도해주세요."; | ||
|
||
public ConcurrencyViolationException() { | ||
super(MESSAGE); | ||
} | ||
|
||
@Override | ||
public int getHttpStatusCode() { | ||
return HttpStatus.CONFLICT.value(); | ||
} | ||
|
||
@Override | ||
public AbCode getAbCode() { | ||
return AB_CODE; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/life/offonoff/ab/exception/VoteConcurrencyException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package life.offonoff.ab.exception; | ||
|
||
public class VoteConcurrencyException extends ConcurrencyViolationException { | ||
private static final String HINT = "투표 시 동시성 문제 발생"; | ||
|
||
@Override | ||
public String getHint() { | ||
return HINT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/test/java/life/offonoff/ab/application/service/TopicServiceConcurrencyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package life.offonoff.ab.application.service; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.transaction.Transactional; | ||
import life.offonoff.ab.application.service.member.MemberService; | ||
import life.offonoff.ab.application.service.request.VoteRequest; | ||
import life.offonoff.ab.application.testutil.AbCleaner; | ||
import life.offonoff.ab.domain.member.Member; | ||
import life.offonoff.ab.domain.topic.Topic; | ||
import life.offonoff.ab.domain.topic.TopicSide; | ||
import life.offonoff.ab.domain.topic.choice.Choice; | ||
import life.offonoff.ab.domain.topic.choice.ChoiceOption; | ||
import life.offonoff.ab.exception.VoteConcurrencyException; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.transaction.TestTransaction; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.IntStream; | ||
|
||
import static life.offonoff.ab.domain.TestEntityUtil.createRandomMember; | ||
import static life.offonoff.ab.domain.TestEntityUtil.createRandomTopicByMemberWithChoices; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@Transactional | ||
@SpringBootTest | ||
public class TopicServiceConcurrencyTest { | ||
|
||
@Autowired | ||
private MemberService memberService; | ||
|
||
@Autowired | ||
private TopicService topicService; | ||
|
||
@Autowired | ||
private EntityManager em; | ||
|
||
@Autowired | ||
private AbCleaner cleaner; | ||
|
||
@AfterEach | ||
void tearDown() { | ||
cleaner.cleanTables(); | ||
} | ||
|
||
@Test | ||
void voteForTopicByMember() throws InterruptedException { | ||
// given | ||
final int COUNT = 10; | ||
final ExecutorService executorService = Executors.newFixedThreadPool(COUNT); | ||
|
||
// 토픽 생성 | ||
Member topicAuthor = createRandomMember(); | ||
em.persist(topicAuthor); | ||
Topic topic = createRandomTopicByMemberWithChoices( | ||
topicAuthor, TopicSide.TOPIC_A, ChoiceOption.CHOICE_A, ChoiceOption.CHOICE_B); | ||
em.persist(topic); | ||
|
||
// 투표자 COUNT만큼 생성 | ||
List<Member> voters = IntStream.range(0, COUNT) | ||
.mapToObj(__ -> { | ||
Member voter = createRandomMember(); | ||
em.persist(voter); | ||
return voter; | ||
}) | ||
.toList(); | ||
|
||
TestTransaction.flagForCommit(); | ||
TestTransaction.end(); | ||
TestTransaction.start(); | ||
|
||
// when | ||
long votedAt = System.currentTimeMillis() / 1000; | ||
final CountDownLatch latch = new CountDownLatch(COUNT); | ||
AtomicInteger failureCounter = new AtomicInteger(0); | ||
voters.forEach(voter -> { | ||
executorService.execute(() -> { | ||
try { | ||
topicService.voteForTopicByMember( | ||
topic.getId(), voter.getId(), new VoteRequest(ChoiceOption.CHOICE_A, votedAt)); | ||
} catch (VoteConcurrencyException e) { | ||
failureCounter.incrementAndGet(); | ||
} | ||
latch.countDown(); | ||
}); | ||
}); | ||
latch.await(); | ||
|
||
// then | ||
Topic updatedTopic = em.find(Topic.class, topic.getId()); | ||
Choice votedChoice = updatedTopic.getChoices().stream().filter(c -> c.getChoiceOption().equals(ChoiceOption.CHOICE_A)).findAny().get(); | ||
// 투표수는 COUNT와 동일해야함 | ||
int successfulVoteCount = COUNT - failureCounter.get(); | ||
assertThat(updatedTopic.getVoteCount()).isEqualTo(successfulVoteCount); | ||
assertThat(votedChoice.getVoteCount()).isEqualTo(successfulVoteCount); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/test/java/life/offonoff/ab/application/testutil/AbCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package life.offonoff.ab.application.testutil; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class AbCleaner { | ||
private final JdbcTemplate jdbcTemplate; | ||
private List<String> truncateQueries; | ||
|
||
@PostConstruct | ||
public void loadTruncateQueries(){ | ||
truncateQueries = jdbcTemplate.queryForList( | ||
"SELECT Concat('TRUNCATE TABLE ', TABLE_NAME, ';') AS q FROM INFORMATION_SCHEMA.TABLES " + | ||
"WHERE TABLE_SCHEMA IN ('PUBLIC', 'localab', 'ab')", | ||
String.class); | ||
} | ||
|
||
@Transactional | ||
public void cleanTables() { | ||
jdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0"); | ||
truncateQueries.forEach(jdbcTemplate::execute); | ||
jdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters