[timer] fix to allow timer reschedule after termination (#2153)
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>pull/2161/head
parent
6de92ec6ef
commit
b55933d769
|
@ -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));
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue