Skip to content

Commit

Permalink
Fixed CronTrigger with jitter producing fire times beyond end_date
Browse files Browse the repository at this point in the history
Fixes #269.
  • Loading branch information
agronholm committed Jan 3, 2018
1 parent c385f94 commit 01dcea4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
5 changes: 2 additions & 3 deletions apscheduler/triggers/cron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ def get_next_fire_time(self, previous_fire_time, now):
return None

if fieldnum >= 0:
if self.jitter is not None:
next_date = self._apply_jitter(next_date, self.jitter, now)
return next_date
next_date = self._apply_jitter(next_date, self.jitter, now)
return min(next_date, self.end_date) if self.end_date else next_date

def __getstate__(self):
return {
Expand Down
3 changes: 3 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ APScheduler, see the :doc:`migration section <migration>`.

* Fixed ``OverflowError`` on Windows when the wait time is too long

* Fixed ``CronTrigger`` sometimes producing fire times beyond ``end_date`` when jitter is enabled
(thanks to gilbsgilbs for the tests)


3.5.0
-----
Expand Down
18 changes: 18 additions & 0 deletions tests/test_triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,15 @@ def test_jitter_dst_change(self, trigger_args, start_date, start_date_dst, corre
next_fire_time = trigger.get_next_fire_time(None, start_date)
assert abs(next_fire_time - correct_next_date) <= timedelta(seconds=5)

def test_jitter_with_end_date(self, timezone):
now = timezone.localize(datetime(2017, 11, 12, 6, 55, 30))
end_date = timezone.localize(datetime(2017, 11, 12, 6, 56, 0))
trigger = CronTrigger(minute='*', jitter=5, end_date=end_date)

for _ in range(0, 100):
next_fire_time = trigger.get_next_fire_time(None, now)
assert next_fire_time is None or next_fire_time <= end_date

@pytest.mark.parametrize('values, expected', [
(dict(day='*/31'), "Error validating expression '\*/31': the step value \(31\) is higher "
"than the total range of the expression \(30\)"),
Expand Down Expand Up @@ -575,6 +584,15 @@ def test_jitter_dst_change(self, trigger_args, start_date, start_date_dst, corre
next_fire_time = trigger.get_next_fire_time(None, start_date + epsilon)
assert abs(next_fire_time - correct_next_date) <= timedelta(seconds=5)

def test_jitter_with_end_date(self, timezone):
now = timezone.localize(datetime(2017, 11, 12, 6, 55, 58))
end_date = timezone.localize(datetime(2017, 11, 12, 6, 56, 0))
trigger = IntervalTrigger(seconds=5, jitter=5, end_date=end_date)

for _ in range(0, 100):
next_fire_time = trigger.get_next_fire_time(None, now)
assert next_fire_time is None or next_fire_time <= end_date


class TestAndTrigger(object):
@pytest.fixture
Expand Down

0 comments on commit 01dcea4

Please sign in to comment.