Add some comments.

This commit is contained in:
akwizgran
2021-01-20 14:00:15 +00:00
committed by Torsten Grote
parent 5716820439
commit 55eccde031
2 changed files with 14 additions and 0 deletions

View File

@@ -15,6 +15,10 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.MINUTES;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
/**
* A {@link TaskScheduler} for use in tests. The scheduler keeps all scheduled
* tasks in a queue until {@link #runTasks()} is called.
*/
@NotNullByDefault @NotNullByDefault
class TestTaskScheduler implements TaskScheduler { class TestTaskScheduler implements TaskScheduler {
@@ -60,12 +64,16 @@ class TestTaskScheduler implements TaskScheduler {
return schedule(wrapped, executor, delay, unit, cancelled); return schedule(wrapped, executor, delay, unit, cancelled);
} }
/**
* Runs any scheduled tasks that are due.
*/
void runTasks() throws InterruptedException { void runTasks() throws InterruptedException {
long now = clock.currentTimeMillis(); long now = clock.currentTimeMillis();
while (true) { while (true) {
Task t = queue.peek(); Task t = queue.peek();
if (t == null || t.dueMillis > now) return; if (t == null || t.dueMillis > now) return;
t = queue.poll(); t = queue.poll();
// Submit the task to its executor and wait for it to finish
if (!t.run().await(1, MINUTES)) fail(); if (!t.run().await(1, MINUTES)) fail();
} }
} }
@@ -92,6 +100,10 @@ class TestTaskScheduler implements TaskScheduler {
return Long.valueOf(dueMillis).compareTo(task.dueMillis); return Long.valueOf(dueMillis).compareTo(task.dueMillis);
} }
/**
* Submits the task to its executor and returns a latch that will be
* released when the task finishes.
*/
public CountDownLatch run() { public CountDownLatch run() {
if (cancelled.get()) return new CountDownLatch(0); if (cancelled.get()) return new CountDownLatch(0);
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);

View File

@@ -41,6 +41,7 @@ public class TimeTravelModule {
scheduledExecutorService = scheduledExecutorService =
new ScheduledThreadPoolExecutor(1, policy); new ScheduledThreadPoolExecutor(1, policy);
if (travel) { if (travel) {
// Use a SettableClock and TestTaskScheduler to allow time travel
AtomicLong time = new AtomicLong(System.currentTimeMillis()); AtomicLong time = new AtomicLong(System.currentTimeMillis());
clock = new SettableClock(time); clock = new SettableClock(time);
TestTaskScheduler testTaskScheduler = new TestTaskScheduler(clock); TestTaskScheduler testTaskScheduler = new TestTaskScheduler(clock);
@@ -61,6 +62,7 @@ public class TimeTravelModule {
} }
}; };
} else { } else {
// Use the default clock and task scheduler
clock = new SystemClock(); clock = new SystemClock();
taskScheduler = new TaskSchedulerImpl(scheduledExecutorService); taskScheduler = new TaskSchedulerImpl(scheduledExecutorService);
timeTravel = new TimeTravel() { timeTravel = new TimeTravel() {