|
11 | 11 | import org.elasticsearch.common.collect.Tuple;
|
12 | 12 | import org.elasticsearch.common.settings.Settings;
|
13 | 13 | import org.elasticsearch.test.ESTestCase;
|
| 14 | +import org.elasticsearch.xpack.core.scheduler.SchedulerEngine.Job; |
14 | 15 | import org.mockito.ArgumentCaptor;
|
15 | 16 |
|
16 | 17 | import java.time.Clock;
|
17 | 18 | import java.util.ArrayList;
|
18 | 19 | import java.util.Collections;
|
19 | 20 | import java.util.List;
|
20 | 21 | import java.util.concurrent.CountDownLatch;
|
| 22 | +import java.util.concurrent.TimeUnit; |
21 | 23 | import java.util.concurrent.atomic.AtomicBoolean;
|
22 | 24 | import java.util.concurrent.atomic.AtomicInteger;
|
23 | 25 |
|
24 | 26 | import static org.hamcrest.Matchers.any;
|
25 | 27 | import static org.hamcrest.Matchers.arrayWithSize;
|
26 | 28 | import static org.hamcrest.Matchers.equalTo;
|
27 | 29 | import static org.hamcrest.Matchers.instanceOf;
|
| 30 | +import static org.hamcrest.Matchers.is; |
28 | 31 | import static org.mockito.Matchers.argThat;
|
29 | 32 | import static org.mockito.Mockito.doAnswer;
|
30 | 33 | import static org.mockito.Mockito.mock;
|
@@ -150,6 +153,37 @@ public void testListenersThrowingExceptionsDoNotCauseNextScheduledTaskToBeSkippe
|
150 | 153 | }
|
151 | 154 | }
|
152 | 155 |
|
| 156 | + public void testCancellingDuringRunPreventsRescheduling() throws Exception { |
| 157 | + final CountDownLatch jobRunningLatch = new CountDownLatch(1); |
| 158 | + final CountDownLatch listenerLatch = new CountDownLatch(1); |
| 159 | + final AtomicInteger calledCount = new AtomicInteger(0); |
| 160 | + final SchedulerEngine engine = new SchedulerEngine(Settings.EMPTY, Clock.systemUTC()); |
| 161 | + final String jobId = randomAlphaOfLength(4); |
| 162 | + try { |
| 163 | + engine.register(event -> { |
| 164 | + assertThat(event.getJobName(), is(jobId)); |
| 165 | + calledCount.incrementAndGet(); |
| 166 | + jobRunningLatch.countDown(); |
| 167 | + try { |
| 168 | + listenerLatch.await(); |
| 169 | + } catch (InterruptedException e) { |
| 170 | + Thread.currentThread().interrupt(); |
| 171 | + } |
| 172 | + }); |
| 173 | + engine.add(new Job(jobId, ((startTime, now) -> 0))); |
| 174 | + |
| 175 | + jobRunningLatch.await(); |
| 176 | + final int called = calledCount.get(); |
| 177 | + assertEquals(1, called); |
| 178 | + engine.remove(jobId); |
| 179 | + listenerLatch.countDown(); |
| 180 | + |
| 181 | + assertBusy(() -> assertEquals(called, calledCount.get()), 5, TimeUnit.MILLISECONDS); |
| 182 | + } finally { |
| 183 | + engine.stop(); |
| 184 | + } |
| 185 | + } |
| 186 | + |
153 | 187 | private void assertFailedListenerLogMessage(Logger mockLogger, int times) {
|
154 | 188 | final ArgumentCaptor<ParameterizedMessage> messageCaptor = ArgumentCaptor.forClass(ParameterizedMessage.class);
|
155 | 189 | final ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
|
|
0 commit comments