Skip to content

Commit 6914aff

Browse files
Dave Syersbrannen
authored andcommitted
Add additional test for daylight savings glitch
The problem was that clocks go forward *at* 2am, so 2am doesn't exist once a year. Users might be surprised that their cron trigger doesn't go off one night, but that is arguably correct (and what happens now). The test can be modified if we decide to change the trigger behaviour.
1 parent 4171646 commit 6914aff

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

spring-context/src/test/java/org/springframework/scheduling/support/CronTriggerTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public CronTriggerTests(Date date, TimeZone timeZone) {
5656
@Parameters
5757
public static List<Object[]> getParameters() {
5858
List<Object[]> list = new ArrayList<Object[]>();
59-
list.add(new Object[] { new Date(), TimeZone.getDefault() });
59+
list.add(new Object[] { new Date(), TimeZone.getTimeZone("PST") });
6060
list.add(new Object[] { new Date(), TimeZone.getTimeZone("CET") });
6161
return list;
6262
}
@@ -694,6 +694,26 @@ public void testMonthSequence() throws Exception {
694694
assertEquals(calendar.getTime(), date = trigger.nextExecutionTime(context3));
695695
}
696696

697+
@Test
698+
public void testDaylightSavingMissingHour() throws Exception {
699+
// This trigger has to be somewhere in between 2am and 3am
700+
CronTrigger trigger = new CronTrigger("0 10 2 * * *", timeZone);
701+
calendar.set(Calendar.DAY_OF_MONTH, 31);
702+
calendar.set(Calendar.MONTH, Calendar.MARCH);
703+
calendar.set(Calendar.YEAR, 2013);
704+
calendar.set(Calendar.HOUR_OF_DAY, 1);
705+
calendar.set(Calendar.SECOND, 54);
706+
Date date = calendar.getTime();
707+
TriggerContext context1 = getTriggerContext(date);
708+
if (timeZone.equals(TimeZone.getTimeZone("CET"))) {
709+
// Clocks go forward an hour so 2am doesn't exist in CET for this date
710+
calendar.add(Calendar.DAY_OF_MONTH, 1);
711+
}
712+
calendar.add(Calendar.HOUR_OF_DAY, 1);
713+
calendar.set(Calendar.MINUTE, 10);
714+
calendar.set(Calendar.SECOND, 0);
715+
assertEquals(calendar.getTime(), date = trigger.nextExecutionTime(context1));
716+
}
697717

698718
private void assertMatchesNextSecond(CronTrigger trigger, Calendar calendar) {
699719
Date date = calendar.getTime();

0 commit comments

Comments
 (0)