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

feat: implement dividend scheduler #11

Merged
merged 19 commits into from
Jan 29, 2024
Merged

feat: implement dividend scheduler #11

merged 19 commits into from
Jan 29, 2024

Conversation

chominho96
Copy link
Member

@chominho96 chominho96 commented Jan 26, 2024

Issue Number

close: #8

작업 내역

구현 내용 및 작업 했던 내역

  • QueryDSL 세팅
  • Dividend 엔티티 변경사항 반영
  • Dividend 스케쥴러 서비스 구현
  • Dividend 스케쥴러 서비스 테스트 작성

변경사항

  • 의존성 목록
  • domain 모듈에 QueryDSL 의존성 추가
  • batch 모듈에 WebFlux 의존성 추가
  • batch 모듈에 Await 의존성 추가 (스케쥴러 테스트 관련)

PR 특이 사항

PR을 볼 때 주의깊게 봐야하거나 말하고 싶은 점

  • FMP_API_KEY의 경우 github secret에 저장하고 github action에서 주입받아서 사용하는 방식으로 구현했습니다!
  • Dividend 스케쥴러의 경우 해당하는 Stock을 먼저 조회해야 하는 특성 상, Stock 스케쥴러 수행 이후에 수행되어야 할 것 같아요!
  • 따라서 현재 스케쥴러가 실행되어도 아무 데이터도 추가되지 않는게 정상입니다.
  • test 환경에서는 2초에 한번, 실제 환경에서는 뉴욕 시간 기준으로 00:00에 한번씩 수행되도록 설정했습니다.
  • 스케쥴러 테스트의 경우 일반적인 로직 테스트와 달리 성공 기준을 판단하기가 어려워서 어떻게 테스트해야할지 고민이 되더라구요.. 그래서 일정 시간 내에 주기적으로 메서드를 수행하는지를 기준으로 작성했어요! (수행 도중 exception이 발생하지 않는 것을 성공의 기준으로 잡았습니다..!!)

@chominho96 chominho96 added the enhancement New feature or request label Jan 26, 2024
@chominho96 chominho96 requested a review from songyi00 January 26, 2024 06:21
@chominho96 chominho96 self-assigned this Jan 26, 2024

@SpringBootTest
@TestPropertySource(properties = { "spring.config.location=classpath:application-test.yml" })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ActiveProfiles("test") 이렇게도 사용 가능해요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이 부분 처음 알았네요!! 더 간결해서 말씀해주신 방식대로 쓰는게 나을거 같아요!

@@ -2,8 +2,10 @@

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;

@SpringBootTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트 로직을 보면 @SpringBootTest 를 사용하지 않아도 될 것 같은데 단위테스트를 사용하는건 어떨까요~?
추가로 스케줄러 메서드 직접 호출해서 로직에 대한 테스트도 하면 좋을 것 같아요!!

Copy link
Member Author

@chominho96 chominho96 Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스케쥴러가 원하는 주기에 잘 동작하는지 확인하려면 @SpringBootTest 애노테이션을 붙여야 되더라구요...?? 그래서 붙였어요!
스케쥴러 메서드 로직 테스트도 추가하면 좋을 거 같아요! 추가해볼게요!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

흠 그렇다면 @Scheduled 어노테이션을 테스트하는 것 밖에 안될 것 같아서 의미가 있을지 고민이 되네용 😭
어떻게 생각하시나요?!!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사실 저도 그 부분이 고민되서 어떻게 짜야 할지 고민이 있었는데, 밑에 달아주신 코멘트와 종합해서 생각해보니, FmpClient 로직 테스트, FmpClient를 mock으로 생성하여 스케쥴러 로직 테스트로 짜면 좋을 거 같아요! 그렇게 수정해볼게요!!

*/
@Scheduled(cron = "${schedules.cron.dividend}", zone = "America/New_York")
public void run() {
WebClient client =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FMP API 호출 및 결과를 가져오는 부분은 따로 클래스로 빼서 역할을 나누는 건 어떨까요?
인터페이스(ex.FinancialCllient)를 따로 두고 그 아래에 FmpClient 를 만들면 SRP 를 좀 더 잘 지킬 수 있을 것 같아요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 그렇게 분리하면 테스트 작성도 더 용이할거 같네요! 나누는게 맞는거 같습니다!

@Value("${fmp.key}")
private String FMP_API_KEY;
private final String FMP_API_BASE_URL = "https://financialmodelingprep.com/api/v3";
private final String FMP_API_STOCK_DIVIDEND_CALENDAR_POSTFIX = "/stock_dividend_calendar";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URL 자체도 설정파일로 빼는 것은 어떤가요? 추후 API 도메인 또는 path가 바뀌었을 때 대처가 좀 더 쉬울 것 같아요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 그러네요 API 도메인이 바뀌는거까지는 생각하지 못했는데 그게 더 관리가 편할거 같네요!!

Copy link
Member

@songyi00 songyi00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

구현 고생하셨습니다 👍👍
코멘트 남겨놓았는데 함께 고민해보면 좋을 것 같아요!!

import static org.mockito.Mockito.verify;

@SpringBootTest
@Transactional
Copy link
Member

@songyi00 songyi00 Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트코드에서 @transactional을 추가하신 이유가 궁금해요! 👀

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트 코드에서 DB를 조작하는 코드를 수행할 때 @Transactional 애노테이션을 추가해야 테스트 코드가 수행되고 DB가 롤백되는 것으로 알고 있어요!

Copy link
Member

@songyi00 songyi00 Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 제 PR 에서 얘기하는걸로 하시죠! 👀
#12 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FmpClient의 경우 FMP API로부터 받은 응답에 대한 값을 제공하는 역할만 하고 스케줄러에서 스케줄링 로직을 구현하는 방식으로 책임을 나누는 것도 좋아보여요! (테스트시 FmpClient 만 모킹하고 스케줄러 로직에 대한 검증만 가능하도록..?)
추후 다른 OPEN API 로 갈아탈 가능성도 있기 때문에 아래와 같이 구현체만 바꾸는 것이 가능하도록 짜면 더 좋을 것 같아요 😸

FinancialClient (Interface)

  • FmpFinancialClient
  • OtherFinancialClient

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 그러네요 API가 바뀔 가능성은 생각하지 못했네요...!! 해당 방향으로 수정해볼게요!

Copy link
Member

@songyi00 songyi00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다 🙌🏻

@chominho96 chominho96 merged commit 34dfdc4 into develop Jan 29, 2024
1 check passed
@chominho96 chominho96 deleted the feat/#8 branch January 29, 2024 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants