From ba0e4e1cc538ef6abf716cf56828dd4c27bf0846 Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Thu, 3 Oct 2024 11:13:42 -0500 Subject: [PATCH] Fix race condition in test If the exector is really quick, it can run the test runnable before the array list is assigned to the `times` field. --- .../threads/ScheduledEnhancedQueueExecutorTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/jboss/threads/ScheduledEnhancedQueueExecutorTest.java b/src/test/java/org/jboss/threads/ScheduledEnhancedQueueExecutorTest.java index fc6749e..0f6ce0e 100644 --- a/src/test/java/org/jboss/threads/ScheduledEnhancedQueueExecutorTest.java +++ b/src/test/java/org/jboss/threads/ScheduledEnhancedQueueExecutorTest.java @@ -146,16 +146,22 @@ public void testFixedDelayExecution() throws Exception { @Test public void testThatFixedDelayTerminatesTask() { EnhancedQueueExecutor eqe = new EnhancedQueueExecutor.Builder().build(); + final ArrayList times = new ArrayList<>(); var r = new Runnable() { - final ScheduledFuture future = eqe.scheduleWithFixedDelay(this, 0, 100, TimeUnit.MILLISECONDS); - final ArrayList 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)); }