Changed to nl.andrewl domain and added DailyScheduleTest

This commit is contained in:
Andrew Lalis 2021-08-06 11:38:31 +02:00
parent 27dce051f7
commit 7438c3aa5b
14 changed files with 205 additions and 34 deletions

71
pom.xml
View File

@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nl.andrewlalis</groupId>
<groupId>nl.andrewl</groupId>
<artifactId>simply-scheduled</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
@ -42,15 +42,16 @@
<java.version>11</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<dependencyManagement>
<dependencies>
@ -73,4 +74,54 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,6 +1,10 @@
/**
* The SimplyScheduled module.
*/
module simply_scheduled {
exports nl.andrewlalis.simply_scheduled;
exports nl.andrewlalis.simply_scheduled.schedule;
exports nl.andrewl.simply_scheduled;
exports nl.andrewl.simply_scheduled.schedule;
opens nl.andrewlalis.simply_scheduled;
// Needed for JUnit testing.
opens nl.andrewl.simply_scheduled;
}

View File

@ -1,6 +1,6 @@
package nl.andrewlalis.simply_scheduled;
package nl.andrewl.simply_scheduled;
import nl.andrewlalis.simply_scheduled.schedule.Task;
import nl.andrewl.simply_scheduled.schedule.Task;
import java.time.Clock;
import java.time.Instant;
@ -20,12 +20,22 @@ public class BasicScheduler extends Thread implements Scheduler {
private final ExecutorService executorService;
private boolean running = false;
/**
* Constructs the scheduler using the given clock and executor service. This
* constructor is most useful for test cases where a custom clock is used.
* @param clock The clock to use.
* @param executorService The executor service to use.
*/
public BasicScheduler(Clock clock, ExecutorService executorService) {
this.clock = clock;
this.tasks = new PriorityBlockingQueue<>();
this.executorService = executorService;
}
/**
* Constructs the scheduler using the system's default clock, and a new
* work-stealing thread pool.
*/
public BasicScheduler() {
this(Clock.systemDefaultZone(), Executors.newWorkStealingPool());
}

View File

@ -1,11 +1,14 @@
package nl.andrewlalis.simply_scheduled;
package nl.andrewl.simply_scheduled;
import nl.andrewlalis.simply_scheduled.schedule.RepeatingSchedule;
import nl.andrewlalis.simply_scheduled.schedule.Schedule;
import nl.andrewl.simply_scheduled.schedule.RepeatingSchedule;
import nl.andrewl.simply_scheduled.schedule.Schedule;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
/**
* Simple demonstration of scheduling functionality.
*/
public class Demo {
public static void main(String[] args) {
Scheduler scheduler = new BasicScheduler();

View File

@ -1,7 +1,7 @@
package nl.andrewlalis.simply_scheduled;
package nl.andrewl.simply_scheduled;
import nl.andrewlalis.simply_scheduled.schedule.Schedule;
import nl.andrewlalis.simply_scheduled.schedule.Task;
import nl.andrewl.simply_scheduled.schedule.Task;
import nl.andrewl.simply_scheduled.schedule.Schedule;
/**
* A scheduler is responsible for storing and executing tasks as defined by each

View File

@ -1,4 +1,4 @@
package nl.andrewlalis.simply_scheduled.schedule;
package nl.andrewl.simply_scheduled.schedule;
import java.time.*;
import java.util.Optional;
@ -11,10 +11,21 @@ public class DailySchedule implements Schedule {
private final ZoneId zoneId;
private final LocalTime time;
/**
* Constructs a new schedule that will execute at the given time, using the
* system's default time zone.
* @param time The time at which to execute any tasks using this schedule.
*/
public DailySchedule(LocalTime time) {
this(time, ZoneId.systemDefault());
}
/**
* Constructs a new schedule that will execute at the given time, using the
* given zone id for time zone information.
* @param time The time at which to execute any tasks using this schedule.
* @param zoneId The time zone id.
*/
public DailySchedule(LocalTime time, ZoneId zoneId) {
this.time = time;
this.zoneId = zoneId;
@ -23,9 +34,9 @@ public class DailySchedule implements Schedule {
@Override
public Optional<Instant> getNextExecutionTime(Instant referenceInstant) {
ZonedDateTime currentTime = referenceInstant.atZone(this.zoneId);
LocalDate currentDay = LocalDate.from(referenceInstant);
LocalDate currentDay = LocalDate.ofInstant(referenceInstant, this.zoneId);
ZonedDateTime sameDayExecution = currentDay.atTime(this.time).atZone(this.zoneId);
if (sameDayExecution.isBefore(currentTime)) {
if (sameDayExecution.isAfter(currentTime)) {
return Optional.of(sameDayExecution.toInstant());
}
return Optional.of(sameDayExecution.plusDays(1).toInstant());

View File

@ -1,4 +1,4 @@
package nl.andrewlalis.simply_scheduled.schedule;
package nl.andrewl.simply_scheduled.schedule;
import java.time.Instant;
import java.time.ZoneId;

View File

@ -1,4 +1,4 @@
package nl.andrewlalis.simply_scheduled.schedule;
package nl.andrewl.simply_scheduled.schedule;
import java.time.Instant;
import java.time.temporal.ChronoUnit;

View File

@ -1,6 +1,6 @@
package nl.andrewlalis.simply_scheduled.schedule;
package nl.andrewl.simply_scheduled.schedule;
import nl.andrewlalis.simply_scheduled.Scheduler;
import nl.andrewl.simply_scheduled.Scheduler;
import java.time.Instant;
import java.util.Optional;

View File

@ -1,4 +1,4 @@
package nl.andrewlalis.simply_scheduled.schedule;
package nl.andrewl.simply_scheduled.schedule;
import java.time.Instant;
import java.time.ZonedDateTime;

View File

@ -1,4 +1,4 @@
package nl.andrewlalis.simply_scheduled.schedule;
package nl.andrewl.simply_scheduled.schedule;
import java.time.Clock;
import java.time.Instant;
@ -52,6 +52,15 @@ public class Task implements Comparable<Task>{
return schedule;
}
/**
* Compares this task to another. This imposes a natural ordering of tasks
* according to their schedule's next planned execution time, such that
* tasks are ordered starting with those with the nearest execution time, to
* those whose execution time is further in the future.
* @param o The task to compare to.
* @return -1 if this task's next execution time is before the other task's,
* 1 if this task's next execution time is after the other, and 0 otherwise.
*/
@Override
public int compareTo(Task o) {
Instant now = clock.instant();

View File

@ -1,8 +1,8 @@
package nl.andrewlalis.simply_scheduled;
package nl.andrewl.simply_scheduled;
import nl.andrewlalis.simply_scheduled.schedule.RepeatingSchedule;
import nl.andrewlalis.simply_scheduled.schedule.Schedule;
import nl.andrewlalis.simply_scheduled.schedule.Task;
import nl.andrewl.simply_scheduled.schedule.Task;
import nl.andrewl.simply_scheduled.schedule.RepeatingSchedule;
import nl.andrewl.simply_scheduled.schedule.Schedule;
import org.junit.jupiter.api.Test;
import java.time.Clock;

View File

@ -0,0 +1,40 @@
package nl.andrewl.simply_scheduled.schedule;
import java.time.*;
import java.util.stream.Stream;
/**
* Tests the ability of the {@link DailySchedule} to reliably give the correct
* next execution time.
*/
public class DailyScheduleTest extends ScheduleTest {
@Override
protected Stream<TestCase> getTestCases() {
var utc = ZoneOffset.UTC;
// For this test, we use a fixed clock at 12:30:45 on August 6, 2021, UTC.
Clock clock = Clock.fixed(ZonedDateTime.of(2021, 8, 6, 12, 30, 45, 0, utc).toInstant(), utc);
ZonedDateTime time = ZonedDateTime.ofInstant(clock.instant(), utc);
return Stream.of(
new TestCase( // A daily schedule whose time has already passed will be scheduled for tomorrow.
new DailySchedule(LocalTime.of(12, 0), utc),
clock.instant(),
time.plusDays(1).toLocalDate().atTime(12, 0).toInstant(utc)
),
new TestCase( // A daily schedule whose time has not yet passed will be scheduled for today.
new DailySchedule(LocalTime.of(18, 44, 3), utc),
clock.instant(),
time.toLocalDate().atTime(18, 44, 3).toInstant(utc)
),
new TestCase( // Account for a time zone which introduces some offset.
new DailySchedule(LocalTime.of(10, 30), ZoneOffset.ofHours(-5)),
clock.instant(),
time.toLocalDate().atTime(15, 30).toInstant(utc)
),
new TestCase( // Account for a time zone whose offset makes it such that the next event is scheduled for tomorrow, in UTC.
new DailySchedule(LocalTime.of(10, 30), ZoneOffset.ofHours(-1)),
clock.instant(),
time.plusDays(1).toLocalDate().atTime(11, 30).toInstant(utc)
)
);
}
}

View File

@ -0,0 +1,43 @@
package nl.andrewl.simply_scheduled.schedule;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
/**
* Abstract test class which can be extended to test the functionality of a
* particular schedule's {@link Schedule#getNextExecutionTime(Instant)} method.
*/
public abstract class ScheduleTest {
protected static class TestCase {
public final Schedule schedule;
public final Instant referenceInstant;
public final Instant expectedNextExecution;
public TestCase(Schedule schedule, Instant referenceInstant, Instant expectedNextExecution) {
this.schedule = schedule;
this.referenceInstant = referenceInstant;
this.expectedNextExecution = expectedNextExecution;
}
}
@Test
public void testGetNextExecutionTime() {
var cases = getTestCases().collect(Collectors.toList());
for (int i = 0; i < cases.size(); i++) {
var testCase = cases.get(i);
var r = testCase.schedule.getNextExecutionTime(testCase.referenceInstant);
if (testCase.expectedNextExecution == null) {
assertTrue(r.isEmpty(), "Case " + i + ": next execution time is not empty when it should be.");
} else {
assertTrue(r.isPresent(), "Case " + i + ": next execution time is not present when it should be.");
assertEquals(testCase.expectedNextExecution, r.get(), "Case " + i + ": expected next execution time does not match expected.");
}
}
}
protected abstract Stream<TestCase> getTestCases();
}