Skip to content

Commit

Permalink
Defensive access to volatile ScheduledFuture field
Browse files Browse the repository at this point in the history
Includes defensive test arrangement for isInThePast() with at least 1 ms having passed.

See gh-24560
  • Loading branch information
jhoeller committed Jun 12, 2024
1 parent 099d016 commit 7a7f34f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ public void cancel(boolean mayInterruptIfRunning) {
*/
@Nullable
public Instant nextExecution() {
if (this.future != null && !this.future.isCancelled()) {
long delay = this.future.getDelay(TimeUnit.MILLISECONDS);
ScheduledFuture<?> future = this.future;
if (future != null && !future.isCancelled()) {
long delay = future.getDelay(TimeUnit.MILLISECONDS);
if (delay > 0) {
return Instant.now().plusMillis(delay);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

/**
* Tests for {@link Task}.
*
* @author Brian Clozel
* @since 6.2
*/
class TaskTests {

Expand Down Expand Up @@ -77,20 +79,32 @@ static class TestRunnable implements Runnable {

@Override
public void run() {
try {
Thread.sleep(1);
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
this.hasRun = true;
}
}


static class FailingTestRunnable implements Runnable {

boolean hasRun;

@Override
public void run() {
try {
Thread.sleep(1);
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
this.hasRun = true;
throw new IllegalStateException("test exception");
}
}


}

0 comments on commit 7a7f34f

Please sign in to comment.