[timer] fix to allow timer reschedule after termination (#2153)

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
pull/2161/head
jimtng 2021-01-27 02:21:41 +10:00 committed by GitHub
parent 6de92ec6ef
commit b55933d769
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View File

@ -72,6 +72,21 @@ public class TimerImplTest {
assertThat(subject.hasTerminated(), is(true));
}
@Test
public void testTimerHasTerminatedAndReschedule() throws InterruptedException {
Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 1));
assertThat(subject.isActive(), is(false));
assertThat(subject.hasTerminated(), is(true));
subject.reschedule(ZonedDateTime.now().plusSeconds(DEFAULT_TIMEOUT_SECONDS));
assertThat(subject.isActive(), is(true));
assertThat(subject.hasTerminated(), is(false));
Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 1));
assertThat(subject.isActive(), is(false));
assertThat(subject.hasTerminated(), is(true));
}
@Test
public void testTimerIsRunning() throws InterruptedException {
assertThat(subject.isRunning(), is(false));

View File

@ -54,14 +54,10 @@ public class TimerImpl implements Timer {
@Override
public boolean reschedule(ZonedDateTime newTime) {
if (future.cancel(false)) {
cancelled = false;
future = scheduler.schedule(runnable, newTime.toInstant());
return true;
} else {
logger.warn("Rescheduling failed as execution has already started!");
return false;
}
future.cancel(false);
cancelled = false;
future = scheduler.schedule(runnable, newTime.toInstant());
return true;
}
@Override