-
Notifications
You must be signed in to change notification settings - Fork 0
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
리뷰 수정 #15
Open
Com-Sun
wants to merge
5
commits into
main
Choose a base branch
from
fix/review
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
리뷰 수정 #15
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5fc3567
fix: 리뷰 수정 - 로직 생성자로 변환, scheduled 서비스로 커맨드 병합
computerhermit 9f654d8
fix: 리뷰 수정 - 조회수 영속화 로직 변경
computerhermit a2987e1
test: 테스트코드 추가
computerhermit b632707
fix: 리뷰수정 및 테스트코드 추가
computerhermit f9783c1
fix: spotless 적용
computerhermit 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,38 +3,41 @@ | |
import com.flab.ccinside.api.trendingpost.application.port.ViewPostEvent; | ||
import com.flab.ccinside.api.trendingpost.application.port.in.PostSystemUsecase; | ||
import com.flab.ccinside.api.trendingpost.application.port.in.UpdateViewCountCommand; | ||
import jakarta.annotation.PostConstruct; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import lombok.RequiredArgsConstructor; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Queue; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class InMemoryMessageQueue { | ||
|
||
private final PostSystemUsecase postSystemUsecase; | ||
private final BlockingQueue<ViewPostEvent> queue; | ||
private final ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
public InMemoryMessageQueue( | ||
PostSystemUsecase postSystemUsecase, | ||
Queue<ViewPostEvent> queue, | ||
ScheduledExecutorService executorService) { | ||
|
||
@PostConstruct | ||
public void init() { | ||
executorService.submit( | ||
executorService.scheduleWithFixedDelay( | ||
() -> { | ||
try { | ||
while (true) { | ||
var event = queue.take(); | ||
List<UpdateViewCountCommand> commands = new ArrayList<>(); | ||
ViewPostEvent event; | ||
while ((event = queue.poll()) != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. queue가 계속 차 있는 경우에는 무한 루프에 빠지게 될 것 같네요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 확인해주셔서 감사합니다. |
||
log.info("View post event consumed. postId: {}", event.postId()); | ||
var command = new UpdateViewCountCommand(event.postId()); | ||
postSystemUsecase.updateViewCount(command); | ||
commands.add(command); | ||
} | ||
} catch (InterruptedException e) { | ||
log.error("consume error: {}", e.getMessage()); | ||
throw new RuntimeException(e); | ||
postSystemUsecase.persistViewCountsInBatch(commands); | ||
} catch (Exception e) { | ||
log.error("error: {}", e.getMessage()); | ||
} | ||
}); | ||
}, | ||
10, | ||
100, | ||
TimeUnit.MILLISECONDS); | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
...om/flab/ccinside/api/trendingpost/adapter/out/post/persistence/PostRepositorySupport.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,8 @@ | ||
package com.flab.ccinside.api.trendingpost.adapter.out.post.persistence; | ||
|
||
import java.util.List; | ||
|
||
public interface PostRepositorySupport { | ||
|
||
void saveAllByBatch(List<PostEntity> posts); | ||
} |
21 changes: 21 additions & 0 deletions
21
...lab/ccinside/api/trendingpost/adapter/out/post/persistence/PostRepositorySupportImpl.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,21 @@ | ||
package com.flab.ccinside.api.trendingpost.adapter.out.post.persistence; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class PostRepositorySupportImpl implements PostRepositorySupport { | ||
|
||
@PersistenceContext private final EntityManager em; | ||
|
||
@Override | ||
public void saveAllByBatch(List<PostEntity> posts) { | ||
posts.forEach(em::merge); | ||
em.flush(); | ||
em.clear(); | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
...c/main/java/com/flab/ccinside/api/trendingpost/application/port/in/PostSystemUsecase.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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
package com.flab.ccinside.api.trendingpost.application.port.in; | ||
|
||
import java.util.List; | ||
|
||
public interface PostSystemUsecase { | ||
|
||
void updateViewCount(UpdateViewCountCommand command); | ||
|
||
void persistViewCountsInBatch(List<UpdateViewCountCommand> commands); | ||
} |
3 changes: 3 additions & 0 deletions
3
...ava/com/flab/ccinside/api/trendingpost/application/port/out/post/PersistPostViewPort.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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package com.flab.ccinside.api.trendingpost.application.port.out.post; | ||
|
||
import com.flab.ccinside.api.trendingpost.domain.Post; | ||
import java.util.List; | ||
|
||
public interface PersistPostViewPort { | ||
|
||
void modify(Post post); | ||
|
||
void modifyInBatch(List<Post> posts); | ||
} |
14 changes: 10 additions & 4 deletions
14
api/src/main/java/com/flab/ccinside/api/trendingpost/config/InMemoryQueueConfig.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 |
---|---|---|
@@ -1,16 +1,22 @@ | ||
package com.flab.ccinside.api.trendingpost.config; | ||
|
||
import com.flab.ccinside.api.trendingpost.application.port.ViewPostEvent; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class InMemoryQueueConfig { | ||
@Bean | ||
public Queue<ViewPostEvent> queue() { | ||
return new ConcurrentLinkedQueue<>(); | ||
} | ||
|
||
@Bean | ||
public BlockingQueue<ViewPostEvent> queue() { | ||
return new LinkedBlockingQueue<>(); | ||
public ScheduledExecutorService executorService() { | ||
return Executors.newScheduledThreadPool(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
44 changes: 44 additions & 0 deletions
44
...ovy/com/flab/ccinside/api/trendingpost/adapter/in/message/InMemoryMessageQueueSpec.groovy
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,44 @@ | ||
package com.flab.ccinside.api.trendingpost.adapter.in.message | ||
|
||
import com.flab.ccinside.api.trendingpost.application.port.ViewPostEvent | ||
import com.flab.ccinside.api.trendingpost.application.port.in.PostSystemUsecase | ||
import com.flab.ccinside.api.trendingpost.application.port.in.UpdateViewCountCommand | ||
import com.flab.ccinside.api.trendingpost.application.port.out.PostId | ||
import com.flab.ccinside.api.trendingpost.config.InMemoryQueueConfig | ||
import java.util.concurrent.ScheduledExecutorService | ||
import java.util.concurrent.TimeUnit | ||
import org.spockframework.spring.SpringBean | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.test.context.ContextConfiguration | ||
import spock.lang.Specification | ||
|
||
@ContextConfiguration(classes = [InMemoryMessageQueue, InMemoryQueueConfig]) | ||
class InMemoryMessageQueueSpec extends Specification { | ||
|
||
@Autowired | ||
Queue<ViewPostEvent> queue | ||
|
||
@SpringBean | ||
ScheduledExecutorService executorService = Mock() | ||
|
||
@SpringBean | ||
PostSystemUsecase postSystemUsecase = Mock() | ||
|
||
def "인메모리 메시지큐 이벤트 소비 및 행위 검증- 정상"() { | ||
given: | ||
def event = new ViewPostEvent(PostId.from(1L)) | ||
|
||
postSystemUsecase.updateViewCount(_) >> {} | ||
postSystemUsecase.persistViewCountsInBatch(_) >> {} | ||
1 * executorService.scheduleWithFixedDelay(_, _, _, _) >> { Runnable runnable, long l1, long l2, TimeUnit unit -> runnable.run()} | ||
|
||
queue.add(event) | ||
|
||
when: | ||
def inMemoryMessageQueue = new InMemoryMessageQueue(postSystemUsecase, queue, executorService) | ||
|
||
then: | ||
1 * postSystemUsecase.updateViewCount({it == new UpdateViewCountCommand(PostId.from(1L))}) | ||
1 * postSystemUsecase.persistViewCountsInBatch({it == List.of(new UpdateViewCountCommand(PostId.from(1L)))}) | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
executorService가 로직을 제대로 실행한다는 것은 어떻게 테스트 할 수 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
말씀해주신대로, 비동기 프로세스 생성에 대한 부분과, 비즈니스 로직 처리에대한 부분을 분리하여 각각 테스트하도록 수정했습니다.
감사합니다 :)