-
Notifications
You must be signed in to change notification settings - Fork 0
[Book][Fix] 카테고리 저장 동시성 이슈 해결 #200
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
20 changes: 20 additions & 0 deletions
20
src/main/java/book/book/book/initializer/BookCategoryInitializer.java
This file contains hidden or 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,20 @@ | ||
| package book.book.book.initializer; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.boot.CommandLineRunner; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class BookCategoryInitializer implements CommandLineRunner { | ||
|
|
||
| private final JdbcTemplate jdbcTemplate; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public void run(String... args) throws Exception { | ||
| jdbcTemplate.execute("INSERT IGNORE INTO book_category (id, name, parent_id) VALUES (0, 'ROOT', NULL)"); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
88 changes: 88 additions & 0 deletions
88
src/test/java/book/book/book/service/BookCategoryServiceConcurrencyTest.java
This file contains hidden or 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,88 @@ | ||
| package book.book.book.service; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import book.book.book.entity.BookCategory; | ||
| import book.book.book.repository.BookCategoryRepository; | ||
| import book.book.config.IntegrationTest; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.transaction.support.TransactionTemplate; | ||
|
|
||
| @IntegrationTest | ||
| class BookCategoryServiceConcurrencyTest { | ||
|
|
||
| @Autowired | ||
| private BookCategoryService bookCategoryService; | ||
|
|
||
| @Autowired | ||
| private BookCategoryRepository bookCategoryRepository; | ||
|
|
||
| @Autowired | ||
| private JdbcTemplate jdbcTemplate; | ||
|
|
||
| @Autowired | ||
| private TransactionTemplate transactionTemplate; | ||
|
|
||
| @Test | ||
| @DisplayName("5개의 스레드가 동시에 같은 계층 구조 카테고리를 생성해도 중복 없이 하나만 생성되어야 한다") | ||
| void concurrentCategoryCreationTest() throws InterruptedException { | ||
| // given | ||
| int threadCount = 5; | ||
| ExecutorService executorService = Executors.newFixedThreadPool(threadCount); | ||
| CountDownLatch latch = new CountDownLatch(threadCount); | ||
| String categoryPath = "국내도서>소설>한국소설"; | ||
|
|
||
| AtomicInteger successCount = new AtomicInteger(); | ||
| AtomicInteger failCount = new AtomicInteger(); | ||
|
|
||
| // when | ||
| for (int i = 0; i < threadCount; i++) { | ||
| executorService.submit(() -> { | ||
| try { | ||
| bookCategoryService.getOrCreateByFullPath(categoryPath); | ||
| successCount.incrementAndGet(); | ||
| } catch (Exception e) { | ||
| failCount.incrementAndGet(); | ||
| e.printStackTrace(); | ||
| } finally { | ||
| latch.countDown(); | ||
| } | ||
| }); | ||
| } | ||
| latch.await(); | ||
|
|
||
| // then | ||
| assertThat(failCount.get()).isEqualTo(0); | ||
| assertThat(successCount.get()).isEqualTo(threadCount); | ||
|
|
||
| // Check Hierarchy | ||
| // 1. ROOT (ID 0) | ||
| // 2. 국내도서 (Parent 0) | ||
| // 3. 소설 (Parent 국내도서) | ||
| // 4. 한국소설 (Parent 소설) | ||
| // Total 4 categories including ROOT. | ||
|
|
||
| assertThat(bookCategoryRepository.count()).isEqualTo(4); | ||
|
|
||
| transactionTemplate.execute(status -> { | ||
| BookCategory root = bookCategoryRepository.findByNameAndParent("ROOT", null) | ||
| .orElseThrow(() -> new AssertionError("ROOT category not found")); | ||
| BookCategory domestic = bookCategoryRepository.findByNameAndParent("국내도서", root) | ||
| .orElseThrow(() -> new AssertionError("국내도서 category not found")); | ||
| BookCategory novel = bookCategoryRepository.findByNameAndParent("소설", domestic) | ||
| .orElseThrow(() -> new AssertionError("소설 category not found")); | ||
| BookCategory leaf = bookCategoryRepository.findByNameAndParent("한국소설", novel) | ||
| .orElseThrow(() -> new AssertionError("한국소설 category not found")); | ||
|
|
||
| assertThat(leaf.getFullPath()).isEqualTo("국내도서>소설>한국소설"); | ||
| return null; | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.