Skip to content

Commit

Permalink
[REFACTOR] 인자로 현재 시각을 받도록 리팩토링 #249
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin409 committed Nov 26, 2024
1 parent 615d944 commit cc665a5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public Long register(final Long leagueId, final GameRequestDto.Register requestD
}

@Transactional
public void updateGameStatusToFinish() {
LocalDateTime cutoffTime = LocalDateTime.now().minusHours(5);
public void updateGameStatusToFinish(LocalDateTime now) {
LocalDateTime cutoffTime = now.minusHours(5);
List<Game> games = gameRepository.findGamesOlderThanFiveHours(cutoffTime);
games.forEach(game -> game.updateState(GameState.FINISHED));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sports.server.command.game.application;

import java.time.LocalDateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

Expand All @@ -14,6 +15,6 @@ public GameStatusScheduler(GameService gameService) {

@Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul")
public void scheduleUpdateGameStatusToFinish() {
gameService.updateGameStatusToFinish();
gameService.updateGameStatusToFinish(LocalDateTime.now());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Optional;
import org.apache.catalina.Manager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -71,7 +71,11 @@ void setUp() {
this.idOfTeam2 = 2L;
this.requestDto = new GameRequestDto.Register(nameOfGame, 16, "전반전", "SCHEDULED", LocalDateTime.now(),
idOfTeam1, idOfTeam2, null);
when(clock.instant()).thenReturn(Instant.parse("2024-11-26T00:00:00Z"));
LocalDateTime fixedNow = LocalDateTime.of(2024, 11, 26, 0, 0, 0, 0);
Clock fixedClock = Clock.fixed(fixedNow.atZone(ZoneId.systemDefault()).toInstant(), ZoneId.systemDefault());

when(clock.instant()).thenReturn(fixedClock.instant());
when(clock.getZone()).thenReturn(fixedClock.getZone());
}

@Nested
Expand Down Expand Up @@ -226,7 +230,7 @@ class UpdateGameStatusToFinishTest {
@DisplayName("정상적으로 시작한지 5시간이 지난 게임의 상태가 FINISHED로 변경된다")
void updateGamesOlderThanFiveHoursToFinished() {
// given
LocalDateTime now = LocalDateTime.now();
LocalDateTime now = LocalDateTime.now(clock);

Sport sport = entityUtils.getEntity(1L, Sport.class);
League league = entityUtils.getEntity(1L, League.class);
Expand All @@ -245,7 +249,7 @@ void updateGamesOlderThanFiveHoursToFinished() {
gameFixtureRepository.save(oldGame2);

// when
gameService.updateGameStatusToFinish();
gameService.updateGameStatusToFinish(now);

// then
assertAll(
Expand Down

0 comments on commit cc665a5

Please sign in to comment.