Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/com/coreoz/wisp/schedule/FixedHourSchedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ long durationUntilNextExecutionInMillis(long currentTimeInMillis, Long lastExecu
}

private ZonedDateTime nextExecutionDateTime(ZonedDateTime currentDateTime, boolean nextExecutionShouldBeNextDay) {
if(!nextExecutionShouldBeNextDay && currentDateTime.toLocalTime().compareTo(executionTime) <= 0) {
return executionTime.atDate(currentDateTime.toLocalDate()).atZone(zoneId);
ZonedDateTime executionDateTime = executionTime.atDate(currentDateTime.toLocalDate()).atZone(zoneId);
if(!nextExecutionShouldBeNextDay && !executionDateTime.isBefore(currentDateTime)) {
return executionDateTime;
}
return executionTime.atDate(currentDateTime.toLocalDate()).plusDays(1).atZone(zoneId);
}
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/com/coreoz/wisp/schedule/FixedHourScheduleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

import org.junit.Test;

import com.coreoz.wisp.Job;
import com.coreoz.wisp.JobStatus;
import com.coreoz.wisp.Scheduler;
import com.coreoz.wisp.SchedulerConfig;

public class FixedHourScheduleTest {

@Test
Expand Down Expand Up @@ -130,4 +135,40 @@ public void should_not_return_current_time_if_last_execution_equals_current_time
).isEqualTo(24 * 60 * 60 * 1000);
}

@Test
public void should_skip_past_execution_during_repeated_hour() {
ZoneId zone = ZoneId.of("Europe/Paris");
long currentTime = ZonedDateTime.parse("2016-10-30T02:15+01:00[Europe/Paris]").toInstant().toEpochMilli();
long nextDay = ZonedDateTime.parse("2016-10-31T02:30+01:00[Europe/Paris]").toInstant().toEpochMilli();

assertThat(new FixedHourSchedule("02:30", zone).nextExecutionInMillis(currentTime, 0, null))
.isEqualTo(nextDay);
}

@Test
public void should_wait_for_adjusted_execution_during_spring_gap() {
ZoneId zone = ZoneId.of("Europe/Paris");
long currentTime = ZonedDateTime.parse("2016-03-27T03:15+02:00[Europe/Paris]").toInstant().toEpochMilli();
long executionTime = ZonedDateTime.parse("2016-03-27T03:30+02:00[Europe/Paris]").toInstant().toEpochMilli();
long nextDay = ZonedDateTime.parse("2016-03-28T02:30+02:00[Europe/Paris]").toInstant().toEpochMilli();
FixedHourSchedule schedule = new FixedHourSchedule("02:30", zone);

assertThat(schedule.nextExecutionInMillis(currentTime, 0, null)).isEqualTo(executionTime);
assertThat(schedule.nextExecutionInMillis(executionTime, 0, null)).isEqualTo(executionTime);
assertThat(schedule.nextExecutionInMillis(executionTime, 1, executionTime)).isEqualTo(nextDay);
assertThat(schedule.nextExecutionInMillis(executionTime + 1, 1, executionTime)).isEqualTo(nextDay);
}

@Test
public void should_keep_daily_job_scheduled_during_repeated_hour() {
long currentTime = ZonedDateTime.parse("2016-10-30T02:15+01:00[Europe/Paris]").toInstant().toEpochMilli();
long nextDay = ZonedDateTime.parse("2016-10-31T02:30+01:00[Europe/Paris]").toInstant().toEpochMilli();
try (Scheduler scheduler = new Scheduler(SchedulerConfig.builder().timeProvider(() -> currentTime).build())) {
Job job = scheduler.schedule("daily", () -> {}, new FixedHourSchedule("02:30", ZoneId.of("Europe/Paris")));

assertThat(job.status()).isEqualTo(JobStatus.SCHEDULED);
assertThat(job.nextExecutionTimeInMillis()).isEqualTo(nextDay);
}
}

}