mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-10 21:31:53 +01:00
Add Timer.isCancelled() method (#2570)
Signed-off-by: Jimmy Tanagra <jimmy@tanagra.id.au>
This commit is contained in:
parent
3e8b664085
commit
a4b737c401
@ -52,24 +52,29 @@ public class TimerImplTest {
|
||||
public void testTimerIsActiveAndCancel() {
|
||||
assertThat(subject.isActive(), is(true));
|
||||
assertThat(subject.hasTerminated(), is(false));
|
||||
assertThat(subject.isCancelled(), is(false));
|
||||
|
||||
subject.cancel();
|
||||
assertThat(subject.isActive(), is(false));
|
||||
assertThat(subject.hasTerminated(), is(true));
|
||||
assertThat(subject.isCancelled(), is(true));
|
||||
|
||||
subject.reschedule(ZonedDateTime.now().plusSeconds(DEFAULT_TIMEOUT_SECONDS));
|
||||
assertThat(subject.isActive(), is(true));
|
||||
assertThat(subject.hasTerminated(), is(false));
|
||||
assertThat(subject.isCancelled(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTimerIsActiveAndTerminate() throws InterruptedException {
|
||||
assertThat(subject.isActive(), is(true));
|
||||
assertThat(subject.hasTerminated(), is(false));
|
||||
assertThat(subject.isCancelled(), is(false));
|
||||
|
||||
Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 1));
|
||||
assertThat(subject.isActive(), is(false));
|
||||
assertThat(subject.hasTerminated(), is(true));
|
||||
assertThat(subject.isCancelled(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -77,28 +82,34 @@ public class TimerImplTest {
|
||||
Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 1));
|
||||
assertThat(subject.isActive(), is(false));
|
||||
assertThat(subject.hasTerminated(), is(true));
|
||||
assertThat(subject.isCancelled(), is(false));
|
||||
|
||||
subject.reschedule(ZonedDateTime.now().plusSeconds(DEFAULT_TIMEOUT_SECONDS));
|
||||
assertThat(subject.isActive(), is(true));
|
||||
assertThat(subject.hasTerminated(), is(false));
|
||||
assertThat(subject.isCancelled(), is(false));
|
||||
|
||||
Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 1));
|
||||
assertThat(subject.isActive(), is(false));
|
||||
assertThat(subject.hasTerminated(), is(true));
|
||||
assertThat(subject.isCancelled(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTimerIsRunning() throws InterruptedException {
|
||||
assertThat(subject.isRunning(), is(false));
|
||||
assertThat(subject.hasTerminated(), is(false));
|
||||
assertThat(subject.isCancelled(), is(false));
|
||||
|
||||
Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS) + 500);
|
||||
assertThat(subject.isRunning(), is(true));
|
||||
assertThat(subject.hasTerminated(), is(false));
|
||||
assertThat(subject.isCancelled(), is(false));
|
||||
|
||||
Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_RUNTIME_SECONDS + 1));
|
||||
assertThat(subject.isRunning(), is(false));
|
||||
assertThat(subject.hasTerminated(), is(true));
|
||||
assertThat(subject.isCancelled(), is(false));
|
||||
}
|
||||
|
||||
private Timer createTimer(ZonedDateTime instant, SchedulerRunnable runnable) {
|
||||
|
@ -44,6 +44,13 @@ public interface Timer {
|
||||
*/
|
||||
public boolean isActive();
|
||||
|
||||
/**
|
||||
* Determines whether the timer has been cancelled
|
||||
*
|
||||
* @return true, if the timer has been cancelled, false otherwise
|
||||
*/
|
||||
public boolean isCancelled();
|
||||
|
||||
/**
|
||||
* Determines whether the scheduled code is currently executed.
|
||||
*
|
||||
|
@ -36,8 +36,6 @@ public class TimerImpl implements Timer {
|
||||
private final SchedulerRunnable runnable;
|
||||
private ScheduledCompletableFuture<Object> future;
|
||||
|
||||
private boolean cancelled;
|
||||
|
||||
public TimerImpl(Scheduler scheduler, ZonedDateTime startTime, SchedulerRunnable runnable) {
|
||||
this.scheduler = scheduler;
|
||||
this.startTime = startTime;
|
||||
@ -48,26 +46,29 @@ public class TimerImpl implements Timer {
|
||||
|
||||
@Override
|
||||
public boolean cancel() {
|
||||
cancelled = true;
|
||||
return future.cancel(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean reschedule(ZonedDateTime newTime) {
|
||||
future.cancel(false);
|
||||
cancelled = false;
|
||||
future = scheduler.schedule(runnable, newTime.toInstant());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZonedDateTime getExecutionTime() {
|
||||
return cancelled ? null : ZonedDateTime.now().plusNanos(future.getDelay(TimeUnit.NANOSECONDS));
|
||||
return future.isCancelled() ? null : ZonedDateTime.now().plusNanos(future.getDelay(TimeUnit.NANOSECONDS));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return !future.isDone() && !cancelled;
|
||||
return !future.isDone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return future.isCancelled();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user