Skip to content

Commit

Permalink
Fix race condition in test
Browse files Browse the repository at this point in the history
If the exector is really quick, it can run the test runnable before the array list is assigned to the `times` field.
  • Loading branch information
dmlloyd committed Oct 3, 2024
1 parent c2e1c42 commit ba0e4e1
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,22 @@ public void testFixedDelayExecution() throws Exception {
@Test
public void testThatFixedDelayTerminatesTask() {
EnhancedQueueExecutor eqe = new EnhancedQueueExecutor.Builder().build();
final ArrayList<LocalDateTime> times = new ArrayList<>();
var r = new Runnable() {
final ScheduledFuture<?> future = eqe.scheduleWithFixedDelay(this, 0, 100, TimeUnit.MILLISECONDS);
final ArrayList<LocalDateTime> times = new ArrayList<>();
ScheduledFuture<?> future;

public void run() {
times.add(LocalDateTime.now());
if (times.size() >= 5) {
future.cancel(false);
}
}

public void schedule() {
future = eqe.scheduleWithFixedDelay(this, 0, 100, TimeUnit.MILLISECONDS);
}
};
r.schedule();
assertThrows(CancellationException.class, () -> r.future.get(5, TimeUnit.SECONDS));
}

Expand Down

0 comments on commit ba0e4e1

Please sign in to comment.