diff --git a/pom.xml b/pom.xml index 728ab41..c14213d 100644 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ - 2.479 + 2.504 ${jenkins.baseline}.3 2545.va_5c4d760c7ef false @@ -40,7 +40,7 @@ io.jenkins.tools.bom bom-${jenkins.baseline}.x - 5054.v620b_5d2b_d5e6 + 5888.vd99c2b_38128d pom import diff --git a/src/test/java/hudson/plugins/disk_usage/DiskUsageCalculationTest.java b/src/test/java/hudson/plugins/disk_usage/DiskUsageCalculationTest.java index 44f22d7..abea8f0 100644 --- a/src/test/java/hudson/plugins/disk_usage/DiskUsageCalculationTest.java +++ b/src/test/java/hudson/plugins/disk_usage/DiskUsageCalculationTest.java @@ -4,6 +4,7 @@ */ package hudson.plugins.disk_usage; +import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure; import static org.junit.jupiter.api.Assertions.assertEquals; import java.time.Duration; @@ -14,6 +15,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; import jenkins.util.Timer; +import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.Test; /** @@ -28,7 +30,7 @@ public class DiskUsageCalculationTest { * see @testReschedule() */ @Test - void testScheduledExecutionTime() throws Exception { + void testScheduledExecutionTime() { GregorianCalendar calendar = new GregorianCalendar(); calendar.add(Calendar.MINUTE, 10); int minute = calendar.get(Calendar.MINUTE); @@ -39,9 +41,9 @@ void testScheduledExecutionTime() throws Exception { calculation.getLastTask().cancel(); } long expectedNextExecution = calendar.getTimeInMillis(); - assertEquals(0, calculation.scheduledLastInstanceExecutionTime(), 60000, "Scheduled time of disk usage calculation should 0, because calculation is not scheduled"); + assertCloseEnough(0, calculation.scheduledLastInstanceExecutionTime(), 60000, "Scheduled time of disk usage calculation should 0, because calculation is not scheduled"); Timer.get().schedule(calculation.getNewInstance(), calculation.getRecurrencePeriod(), TimeUnit.MILLISECONDS); - assertEquals(expectedNextExecution, calculation.scheduledLastInstanceExecutionTime(), 60000, "Scheduled time of disk usage calculation should be in 10 minutes"); + assertCloseEnough(expectedNextExecution, calculation.scheduledLastInstanceExecutionTime(), 60000, "Scheduled time of disk usage calculation should be in 10 minutes"); // scheduled time should be changed if configuration of cron is changed calendar.add(Calendar.MINUTE, 10); @@ -49,7 +51,7 @@ void testScheduledExecutionTime() throws Exception { calculation.setCron(minute + " * * * *"); calculation.reschedule(); expectedNextExecution = calendar.getTimeInMillis(); - assertEquals(expectedNextExecution, calculation.scheduledLastInstanceExecutionTime(), 60000, "Scheduled time of disk usage calculation should be changed"); + assertCloseEnough(expectedNextExecution, calculation.scheduledLastInstanceExecutionTime(), 120_000, "Scheduled time of disk usage calculation should be changed"); } @@ -65,7 +67,7 @@ void testGetRecurrencePeriod() { TestDiskUsageCalculation calculation = new TestDiskUsageCalculation(minute + " * * * *"); long period = calculation.getRecurrencePeriod(); long expectedPeriod = calendar.getTimeInMillis() - System.currentTimeMillis(); - assertEquals(expectedPeriod, period, 60000, "Disk usage calculation should be executed accurately in 2 minutes"); + assertCloseEnough(expectedPeriod, period, 60000, "Disk usage calculation should be executed accurately in 2 minutes"); // for hours calendar = new GregorianCalendar(); @@ -76,7 +78,7 @@ void testGetRecurrencePeriod() { calculation = new TestDiskUsageCalculation("0 " + hour + " * * *"); period = calculation.getRecurrencePeriod(); expectedPeriod = calendar.getTimeInMillis() - System.currentTimeMillis(); - assertEquals(expectedPeriod, period, 60000, "Disk usage calculation should be executed accurately in 2 hours."); + assertCloseEnough(expectedPeriod, period, 60000, "Disk usage calculation should be executed accurately in 2 hours."); // for days calendar = new GregorianCalendar(); @@ -85,7 +87,7 @@ void testGetRecurrencePeriod() { calculation = new TestDiskUsageCalculation(calendar.get(Calendar.MINUTE) + " " + calendar.get(Calendar.HOUR_OF_DAY) + " " + day + " * *"); period = calculation.getRecurrencePeriod(); expectedPeriod = calendar.getTimeInMillis() - System.currentTimeMillis(); - assertEquals(expectedPeriod, period, 60000, "Disk usage calculation should be executed accurately in 2 days."); + assertCloseEnough(expectedPeriod, period, 60000, "Disk usage calculation should be executed accurately in 2 days."); // for months calendar = new GregorianCalendar(); @@ -95,7 +97,7 @@ void testGetRecurrencePeriod() { period = calculation.getRecurrencePeriod(); expectedPeriod = calendar.getTimeInMillis() - System.currentTimeMillis(); calendar.setTimeInMillis(System.currentTimeMillis() + period); - assertEquals(expectedPeriod, period, 60000, "Disk usage calculation should be executed accurately in 2 months."); + assertCloseEnough(expectedPeriod, period, 60000, "Disk usage calculation should be executed accurately in 2 months."); // day of week calendar = new GregorianCalendar(); @@ -105,7 +107,7 @@ void testGetRecurrencePeriod() { period = calculation.getRecurrencePeriod(); expectedPeriod = calendar.getTimeInMillis() - System.currentTimeMillis(); calendar.setTimeInMillis(System.currentTimeMillis() + period); - assertEquals(expectedPeriod, period, 60000, "Disk usage calculation should be executed accurately in 2 months."); + assertCloseEnough(expectedPeriod, period, 60000, "Disk usage calculation should be executed accurately in 2 months."); } @@ -116,7 +118,7 @@ void testGetRecurrencePeriod() { * see @testScheduledExecutionTime() */ @Test - void testReschedule() throws Exception { + void testReschedule() { GregorianCalendar calendar = new GregorianCalendar(); calendar.add(Calendar.MINUTE, 10); int minute = calendar.get(Calendar.MINUTE); @@ -128,7 +130,7 @@ void testReschedule() throws Exception { calculation.setCron(minute + " * * * *"); calculation.reschedule(); // should cancel this calculation and schedule new instance - assertEquals(calendar.getTimeInMillis(), calculation.scheduledLastInstanceExecutionTime(), 60000, "A new calculation should be scheduled with a new scheduled time."); + assertCloseEnough(calendar.getTimeInMillis(), calculation.scheduledLastInstanceExecutionTime(), 60000, "A new calculation should be scheduled with a new scheduled time."); } @@ -153,4 +155,13 @@ void testTaskIsScheduledOnlyOneTimesPerMinute() throws Exception { TestDiskUsageCalculation.stopLoadInstancesHistory(); } + static void assertCloseEnough(long expected, long actual, int delta, @Nullable String message) { + if (!((expected == actual) || Math.abs(expected - actual) <= delta)) { + assertionFailure() // + .message(message) // + .expected(expected) // + .actual(actual) // + .buildAndThrow(); + } + } }