Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<properties>
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
<jenkins.baseline>2.479</jenkins.baseline>
<jenkins.baseline>2.504</jenkins.baseline>
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
<jenkins-test-harness.version>2545.va_5c4d760c7ef</jenkins-test-harness.version>
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
Expand All @@ -40,7 +40,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>5054.v620b_5d2b_d5e6</version>
<version>5888.vd99c2b_38128d</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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);
Expand All @@ -39,17 +41,17 @@ 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);
minute = calendar.get(Calendar.MINUTE);
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");

}

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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.");

}

Expand All @@ -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);
Expand All @@ -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.");

}

Expand All @@ -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();
}
}
}
Loading