Skip to content

Commit

Permalink
Fix first schedule execution for scheduling a done job
Browse files Browse the repository at this point in the history
  • Loading branch information
amanteaux committed Apr 2, 2019
1 parent 2bf3a46 commit b6d874c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/coreoz/wisp/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,16 @@ public Job schedule(String nullableName, Runnable runnable, Schedule when) {

String name = nullableName == null ? runnable.toString() : nullableName;

Job job = prepareJob(name, runnable, when);
long currentTimeInMillis = timeProvider.currentTime();
if(when.nextExecutionInMillis(currentTimeInMillis, 0, null) < currentTimeInMillis) {
if(when.nextExecutionInMillis(
currentTimeInMillis,
job.executionsCount(),
job.lastExecutionEndedTimeInMillis()
) < currentTimeInMillis) {
logger.warn("The job '{}' is scheduled at a paste date: it will never be executed", name);
}

Job job = prepareJob(name, runnable, when);
logger.info("Scheduling job '{}' to run {}", job.name(), job.schedule());
scheduleNextExecution(job);

Expand Down
38 changes: 37 additions & 1 deletion src/test/java/com/coreoz/wisp/SchedulerCancelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import static org.junit.Assert.fail;

import java.time.Duration;
import java.util.Queue;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand All @@ -15,6 +17,8 @@
import com.coreoz.wisp.Utils.SingleJob;
import com.coreoz.wisp.schedule.Schedules;

import lombok.Value;

/**
* Tests about {@link Scheduler#cancel(String)} only
*/
Expand Down Expand Up @@ -162,7 +166,7 @@ public void cancelling_a_job_should_wait_until_it_is_terminated_and_other_jobs_s


@Test
public void scheduling_a_cancelled_job_should_keep_its_previous_stats() throws InterruptedException, ExecutionException, TimeoutException {
public void scheduling_a_done_job_should_keep_its_previous_stats() throws InterruptedException, ExecutionException, TimeoutException {
Scheduler scheduler = new Scheduler();

Job job = scheduler.schedule("job", Utils.doNothing(), Schedules.fixedDelaySchedule(Duration.ofMillis(1)));
Expand All @@ -176,4 +180,36 @@ public void scheduling_a_cancelled_job_should_keep_its_previous_stats() throws I
assertThat(newJob.lastExecutionEndedTimeInMillis()).isNotNull();
}

@Test
public void check_that_a_done_job_scheduled_again_keeps_its_scheduler_stats() throws InterruptedException, ExecutionException, TimeoutException {
Scheduler scheduler = new Scheduler();

Job job = scheduler.schedule("job", Utils.doNothing(), Schedules.fixedDelaySchedule(Duration.ofMillis(1)));
Thread.sleep(25L);

scheduler.cancel("job").toCompletableFuture().get(1, TimeUnit.SECONDS);
int beforeScheduledAgainCount = job.executionsCount();

Queue<ScheduledExecution> scheduledExecutions = new ConcurrentLinkedQueue<>();
scheduler.schedule("job", Utils.doNothing(), (long currentTimeInMillis, int executionsCount, Long lastExecutionEndedTimeInMillis) -> {
scheduledExecutions.add(ScheduledExecution.of(currentTimeInMillis, executionsCount, lastExecutionEndedTimeInMillis));
return currentTimeInMillis;
});
Thread.sleep(25L);
scheduler.gracefullyShutdown();

for(ScheduledExecution scheduledExecution : scheduledExecutions) {
assertThat(scheduledExecution.executionsCount).isGreaterThanOrEqualTo(beforeScheduledAgainCount);
assertThat(scheduledExecution.lastExecutionEndedTimeInMillis).isNotNull();
assertThat(scheduledExecution.lastExecutionEndedTimeInMillis).isLessThanOrEqualTo(scheduledExecution.currentTimeInMillis);
}
}

@Value(staticConstructor = "of")
private static final class ScheduledExecution {
private long currentTimeInMillis;
private int executionsCount;
private Long lastExecutionEndedTimeInMillis;
}

}

0 comments on commit b6d874c

Please sign in to comment.