|
18 | 18 | */ |
19 | 19 | package org.elasticsearch.common.util.concurrent; |
20 | 20 |
|
| 21 | +import org.elasticsearch.common.Randomness; |
21 | 22 | import org.elasticsearch.common.unit.TimeValue; |
| 23 | +import org.elasticsearch.core.internal.io.IOUtils; |
22 | 24 | import org.elasticsearch.test.ESTestCase; |
23 | 25 | import org.elasticsearch.threadpool.TestThreadPool; |
24 | 26 | import org.elasticsearch.threadpool.ThreadPool; |
25 | 27 | import org.junit.AfterClass; |
26 | 28 | import org.junit.BeforeClass; |
27 | 29 |
|
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
28 | 32 | import java.util.concurrent.CountDownLatch; |
29 | 33 | import java.util.concurrent.CyclicBarrier; |
30 | 34 | import java.util.concurrent.TimeUnit; |
31 | 35 | import java.util.concurrent.TimeoutException; |
32 | 36 | import java.util.concurrent.atomic.AtomicInteger; |
| 37 | +import java.util.concurrent.atomic.AtomicLong; |
33 | 38 |
|
34 | 39 | public class AbstractAsyncTaskTests extends ESTestCase { |
35 | 40 |
|
@@ -203,4 +208,31 @@ protected void runInternal() { |
203 | 208 | assertFalse(task.isScheduled()); |
204 | 209 | assertTrue(task.isClosed()); |
205 | 210 | } |
| 211 | + |
| 212 | + public void testIsScheduledRemainFalseAfterClose() throws Exception { |
| 213 | + int numTasks = between(10, 50); |
| 214 | + List<AbstractAsyncTask> tasks = new ArrayList<>(numTasks); |
| 215 | + AtomicLong counter = new AtomicLong(); |
| 216 | + for (int i = 0; i < numTasks; i++) { |
| 217 | + AbstractAsyncTask task = new AbstractAsyncTask(logger, threadPool, TimeValue.timeValueMillis(randomIntBetween(1, 2)), true) { |
| 218 | + @Override |
| 219 | + protected boolean mustReschedule() { |
| 220 | + return counter.get() <= 1000; |
| 221 | + } |
| 222 | + @Override |
| 223 | + protected void runInternal() { |
| 224 | + counter.incrementAndGet(); |
| 225 | + } |
| 226 | + }; |
| 227 | + task.rescheduleIfNecessary(); |
| 228 | + tasks.add(task); |
| 229 | + } |
| 230 | + Randomness.shuffle(tasks); |
| 231 | + IOUtils.close(tasks); |
| 232 | + Randomness.shuffle(tasks); |
| 233 | + for (AbstractAsyncTask task : tasks) { |
| 234 | + assertTrue(task.isClosed()); |
| 235 | + assertFalse(task.isScheduled()); |
| 236 | + } |
| 237 | + } |
206 | 238 | } |
0 commit comments