This is an automated email from the ASF dual-hosted git repository.
weizhou pushed a commit to branch 4.20
in repository https://gitbox.apache.org/repos/asf/cloudstack.git
The following commit(s) were added to refs/heads/4.20 by this push:
new 030ed55f36c server/test: ResourceCleanupService test fix for daylight
saving time (#10749)
030ed55f36c is described below
commit 030ed55f36cfdefd81042290c4c9bf0fded44992
Author: Suresh Kumar Anaparti <[email protected]>
AuthorDate: Wed Apr 30 14:53:12 2025 +0530
server/test: ResourceCleanupService test fix for daylight saving time
(#10749)
---
.../resource/ResourceCleanupServiceImplTest.java | 25 +++++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)
diff --git
a/server/src/test/java/org/apache/cloudstack/resource/ResourceCleanupServiceImplTest.java
b/server/src/test/java/org/apache/cloudstack/resource/ResourceCleanupServiceImplTest.java
index 7446e290488..2ca9c0ef11b 100644
---
a/server/src/test/java/org/apache/cloudstack/resource/ResourceCleanupServiceImplTest.java
+++
b/server/src/test/java/org/apache/cloudstack/resource/ResourceCleanupServiceImplTest.java
@@ -17,6 +17,11 @@
package org.apache.cloudstack.resource;
import java.lang.reflect.Field;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.zone.ZoneRules;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@@ -629,9 +634,23 @@ public class ResourceCleanupServiceImplTest {
Date result = resourceCleanupService.calculatePastDateFromConfig(
ResourceCleanupService.ExpungedResourcesPurgeKeepPastDays.key(),
days);
- Date today = new Date();
- long diff = today.getTime() - result.getTime();
- Assert.assertEquals(days, TimeUnit.DAYS.convert(diff,
TimeUnit.MILLISECONDS));
+ Instant resultInstant = result.toInstant();
+ ZoneId systemZone = ZoneId.systemDefault();
+ ZoneRules rules = systemZone.getRules();
+ ZonedDateTime resultDateTime = resultInstant.atZone(systemZone);
+ boolean isDSTInResultDateTime =
rules.isDaylightSavings(resultDateTime.toInstant());
+
+ ZonedDateTime todayDateTime = ZonedDateTime.now(systemZone);
+ boolean isDSTInTodayDateTime =
rules.isDaylightSavings(todayDateTime.toInstant());
+
+ Duration duration = Duration.between(resultDateTime, todayDateTime);
+ long actualDays = TimeUnit.DAYS.convert(duration.toMillis(),
TimeUnit.MILLISECONDS);
+
+ if (!isDSTInResultDateTime && isDSTInTodayDateTime) {
+ Assert.assertEquals(days - 1, actualDays);
+ } else {
+ Assert.assertEquals(days, actualDays);
+ }
}
@Test