mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-02-04 08:03:53 +01:00
Fix unstable CronSchedulerImplTest (#2354)
This test would often fail when the system is under load. Fixes #1242 Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
parent
e124ad4fec
commit
0e9cecc29b
@ -14,6 +14,8 @@ package org.openhab.core.internal.scheduler;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.Semaphore;
|
import java.util.concurrent.Semaphore;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@ -36,24 +38,26 @@ public class CronSchedulerImplTest {
|
|||||||
private final CronSchedulerImpl cronScheduler = new CronSchedulerImpl(new SchedulerImpl());
|
private final CronSchedulerImpl cronScheduler = new CronSchedulerImpl(new SchedulerImpl());
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Timeout(value = 1, unit = TimeUnit.SECONDS)
|
@Timeout(value = 5, unit = TimeUnit.SECONDS)
|
||||||
public void testCronReboot() throws Exception {
|
public void testCronReboot() throws InterruptedException {
|
||||||
long now = System.currentTimeMillis();
|
Instant start = Instant.now();
|
||||||
Semaphore s = new Semaphore(0);
|
Semaphore s = new Semaphore(0);
|
||||||
ScheduledCompletableFuture<Void> future = cronScheduler.schedule(() -> {
|
ScheduledCompletableFuture<Void> future = cronScheduler.schedule(() -> {
|
||||||
}, "@reboot");
|
}, "@reboot");
|
||||||
future.getPromise().thenAccept(x -> s.release());
|
future.getPromise().thenAccept(x -> s.release());
|
||||||
s.acquire(1);
|
s.acquire(1);
|
||||||
|
|
||||||
long diff = System.currentTimeMillis() - now;
|
Duration duration = Duration.between(start, Instant.now());
|
||||||
assertTrue(diff < 200, "Time difference should be less 200 but was: " + diff);
|
Duration maxDuration = Duration.ofSeconds(2);
|
||||||
|
assertTrue(duration.compareTo(maxDuration) < 0,
|
||||||
|
"Reboot call should occur within " + maxDuration + " but was called after: " + duration);
|
||||||
assertTrue(future.isDone(), "Scheduler should be done once reboot call done.");
|
assertTrue(future.isDone(), "Scheduler should be done once reboot call done.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Timeout(value = 6, unit = TimeUnit.SECONDS)
|
@Timeout(value = 15, unit = TimeUnit.SECONDS)
|
||||||
public void testCronScheduling() throws Exception {
|
public void testCronScheduling() throws InterruptedException {
|
||||||
long now = System.currentTimeMillis();
|
Instant start = Instant.now();
|
||||||
AtomicReference<Object> ref = new AtomicReference<>();
|
AtomicReference<Object> ref = new AtomicReference<>();
|
||||||
|
|
||||||
Semaphore s = new Semaphore(0);
|
Semaphore s = new Semaphore(0);
|
||||||
@ -64,11 +68,22 @@ public class CronSchedulerImplTest {
|
|||||||
+ "\n" //
|
+ "\n" //
|
||||||
+ " foo = bar \n" //
|
+ " foo = bar \n" //
|
||||||
+ "# bla bla foo=foo\n" //
|
+ "# bla bla foo=foo\n" //
|
||||||
+ "0/2 * * * * *");
|
+ "*/5 * * * * *");
|
||||||
s.acquire(2);
|
s.acquire(2);
|
||||||
|
|
||||||
long diff = (System.currentTimeMillis() - now + 50) / 1000;
|
Duration duration = Duration.between(start, Instant.now());
|
||||||
assertTrue(diff >= 3 && diff <= 4, "Difference calculation should be between 3 and 4 but was: " + diff);
|
|
||||||
|
// The call should occur every 5 seconds.
|
||||||
|
// So the fastest execution of 2 calls would be immediately and after 5 seconds.
|
||||||
|
Duration minDuration = Duration.ofSeconds(5);
|
||||||
|
|
||||||
|
// The slowest execution of 2 calls would be after 2*5 seconds.
|
||||||
|
// When the load is high, it can be a bit slower, so we account for this by adding another 2 seconds.
|
||||||
|
Duration maxDuration = Duration.ofSeconds(12);
|
||||||
|
|
||||||
|
assertTrue(minDuration.compareTo(duration) < 0 && duration.compareTo(maxDuration) < 0,
|
||||||
|
"The two calls should be executed between " + minDuration + " and " + maxDuration
|
||||||
|
+ " but the total duration was: " + duration);
|
||||||
assertEquals("bar", ref.get(), "Environment variable 'foo' should be correctly set");
|
assertEquals("bar", ref.get(), "Environment variable 'foo' should be correctly set");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user