|
23 | 23 | import org.junit.Test; |
24 | 24 |
|
25 | 25 | import java.sql.Timestamp; |
| 26 | +import java.util.Arrays; |
26 | 27 | import java.util.Calendar; |
| 28 | +import java.util.Collections; |
27 | 29 |
|
28 | 30 | import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | + |
29 | 33 | /** |
30 | 34 | * Test scheduled actions |
31 | 35 | */ |
@@ -130,6 +134,48 @@ public void testComputingTimeOfLastSchedule(){ |
130 | 134 |
|
131 | 135 | } |
132 | 136 |
|
| 137 | + /** |
| 138 | + * Weekly actions scheduled to run on multiple weekdays should be due |
| 139 | + * in each of them in the same week. |
| 140 | + * |
| 141 | + * For an action scheduled on Mondays and Thursdays, we test that, if |
| 142 | + * the last run was on Monday, the next should be due on the Thursday |
| 143 | + * of the same week instead of the following week. |
| 144 | + */ |
| 145 | + @Test |
| 146 | + public void multiWeekdayWeeklyActions_shouldBeDueOnEachWeekdaySet() { |
| 147 | + ScheduledAction scheduledAction = new ScheduledAction(ScheduledAction.ActionType.BACKUP); |
| 148 | + Recurrence recurrence = new Recurrence(PeriodType.WEEK); |
| 149 | + recurrence.setByDays(Arrays.asList(Calendar.MONDAY, Calendar.THURSDAY)); |
| 150 | + scheduledAction.setRecurrence(recurrence); |
| 151 | + scheduledAction.setStartTime(new DateTime(2016, 6, 6, 9, 0).getMillis()); |
| 152 | + scheduledAction.setLastRun(new DateTime(2017, 4, 17, 9, 0).getMillis()); // Monday |
| 153 | + |
| 154 | + long expectedNextDueDate = new DateTime(2017, 4, 20, 9, 0).getMillis(); // Thursday |
| 155 | + assertThat(scheduledAction.computeNextTimeBasedScheduledExecutionTime()) |
| 156 | + .isEqualTo(expectedNextDueDate); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Weekly actions scheduled with multiplier should skip intermediate |
| 161 | + * weeks and be due in the specified weekday. |
| 162 | + */ |
| 163 | + @Test |
| 164 | + public void weeklyActionsWithMultiplier_shouldBeDueOnTheWeekdaySet() { |
| 165 | + ScheduledAction scheduledAction = new ScheduledAction(ScheduledAction.ActionType.BACKUP); |
| 166 | + Recurrence recurrence = new Recurrence(PeriodType.WEEK); |
| 167 | + recurrence.setMultiplier(2); |
| 168 | + recurrence.setByDays(Collections.singletonList(Calendar.WEDNESDAY)); |
| 169 | + scheduledAction.setRecurrence(recurrence); |
| 170 | + scheduledAction.setStartTime(new DateTime(2016, 6, 6, 9, 0).getMillis()); |
| 171 | + scheduledAction.setLastRun(new DateTime(2017, 4, 12, 9, 0).getMillis()); // Wednesday |
| 172 | + |
| 173 | + // Wednesday, 2 weeks after the last run |
| 174 | + long expectedNextDueDate = new DateTime(2017, 4, 26, 9, 0).getMillis(); |
| 175 | + assertThat(scheduledAction.computeNextTimeBasedScheduledExecutionTime()) |
| 176 | + .isEqualTo(expectedNextDueDate); |
| 177 | + } |
| 178 | + |
133 | 179 | private long getTimeInMillis(int year, int month, int day) { |
134 | 180 | Calendar calendar = Calendar.getInstance(); |
135 | 181 | calendar.set(year, month, day); |
|
0 commit comments