Fix Timer.isRunning() returning true immediately after rescheduling (#4313)

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
This commit is contained in:
jimtng 2024-07-13 23:10:31 +10:00 committed by GitHub
parent 918b4faa3b
commit 8cc267afd8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 3 deletions

View File

@ -30,7 +30,6 @@ import org.openhab.core.scheduler.SchedulerRunnable;
public class TimerImpl implements Timer {
private final Scheduler scheduler;
private final ZonedDateTime startTime;
private final SchedulerRunnable runnable;
private final @Nullable String identifier;
private ScheduledCompletableFuture<?> future;
@ -42,7 +41,6 @@ public class TimerImpl implements Timer {
public TimerImpl(Scheduler scheduler, ZonedDateTime startTime, SchedulerRunnable runnable,
@Nullable String identifier) {
this.scheduler = scheduler;
this.startTime = startTime;
this.runnable = runnable;
this.identifier = identifier;
@ -78,7 +76,7 @@ public class TimerImpl implements Timer {
@Override
public boolean isRunning() {
return isActive() && ZonedDateTime.now().isAfter(startTime);
return isActive() && ZonedDateTime.now().isAfter(future.getScheduledTime());
}
@Override

View File

@ -111,6 +111,12 @@ public class TimerImplTest {
assertThat(subject.isRunning(), is(false));
assertThat(subject.hasTerminated(), is(true));
assertThat(subject.isCancelled(), is(false));
subject.reschedule(ZonedDateTime.now().plusSeconds(DEFAULT_TIMEOUT_SECONDS));
assertThat(subject.isRunning(), is(false));
Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS) + 500);
assertThat(subject.isRunning(), is(true));
}
private Timer createTimer(ZonedDateTime instant, SchedulerRunnable runnable) {