From 55eccde031d4a2d2b3c9ade033ca001918b37e64 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 20 Jan 2021 14:00:15 +0000 Subject: [PATCH] Add some comments. --- .../bramble/system/TestTaskScheduler.java | 12 ++++++++++++ .../bramble/system/TimeTravelModule.java | 2 ++ 2 files changed, 14 insertions(+) diff --git a/bramble-core/src/test/java/org/briarproject/bramble/system/TestTaskScheduler.java b/bramble-core/src/test/java/org/briarproject/bramble/system/TestTaskScheduler.java index 928c2b3a9..55d753e22 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/system/TestTaskScheduler.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/system/TestTaskScheduler.java @@ -15,6 +15,10 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MINUTES; 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 class TestTaskScheduler implements TaskScheduler { @@ -60,12 +64,16 @@ class TestTaskScheduler implements TaskScheduler { return schedule(wrapped, executor, delay, unit, cancelled); } + /** + * Runs any scheduled tasks that are due. + */ void runTasks() throws InterruptedException { long now = clock.currentTimeMillis(); while (true) { Task t = queue.peek(); if (t == null || t.dueMillis > now) return; t = queue.poll(); + // Submit the task to its executor and wait for it to finish if (!t.run().await(1, MINUTES)) fail(); } } @@ -92,6 +100,10 @@ class TestTaskScheduler implements TaskScheduler { 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() { if (cancelled.get()) return new CountDownLatch(0); CountDownLatch latch = new CountDownLatch(1); diff --git a/bramble-core/src/test/java/org/briarproject/bramble/system/TimeTravelModule.java b/bramble-core/src/test/java/org/briarproject/bramble/system/TimeTravelModule.java index bbdae5133..d00d81300 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/system/TimeTravelModule.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/system/TimeTravelModule.java @@ -41,6 +41,7 @@ public class TimeTravelModule { scheduledExecutorService = new ScheduledThreadPoolExecutor(1, policy); if (travel) { + // Use a SettableClock and TestTaskScheduler to allow time travel AtomicLong time = new AtomicLong(System.currentTimeMillis()); clock = new SettableClock(time); TestTaskScheduler testTaskScheduler = new TestTaskScheduler(clock); @@ -61,6 +62,7 @@ public class TimeTravelModule { } }; } else { + // Use the default clock and task scheduler clock = new SystemClock(); taskScheduler = new TaskSchedulerImpl(scheduledExecutorService); timeTravel = new TimeTravel() {