Fix get min interval error

When we use get_min_interval method, the exit condition is
the execution time is bigger than the rate time. But default
minutes rate is 2, this may leads errors when the interval
is bigger than 2 minutes, it always return None. So we can
not use min interval to judge if the interval is smaller than
that we configured in the configuration file. This should be
fixed.
Story: 2006848
Task: 37436

Change-Id: I268051f05f9b89e1b6013cfc8cecf90e9dc78581
This commit is contained in:
jiaopengju 2019-11-14 09:34:06 +08:00 committed by Jiao Pengju
parent 6093b5b11e
commit 66d007e501
2 changed files with 16 additions and 20 deletions

View File

@ -11,7 +11,7 @@
# under the License.
import os
from datetime import timedelta
from datetime import datetime
from dateutil import rrule
from icalendar import Calendar
from oslo_serialization import jsonutils
@ -121,23 +121,9 @@ class ICal(timeformats.TimeFormat):
:return: int(seconds) or None
"""
gen = self.rrule_obj
kwargs = FREQ_TO_KWARGS[self.min_freq]
endtime = self.dtstart + timedelta(**kwargs)
deltas = []
t0 = None
for dt in gen:
if dt > endtime:
break
t1 = t0
t0 = dt
if t1 is None or t0 is None or dt <= self.dtstart:
continue
delta = timeutils.delta_seconds(t1, t0)
if delta:
deltas.append(delta)
if len(deltas):
return min(deltas)
else:
try:
t1 = self.compute_next_time(datetime.now())
t2 = self.compute_next_time(t1)
return timeutils.delta_seconds(t1, t2)
except Exception:
return None

View File

@ -173,3 +173,13 @@ class CalendarTimeTestCase(base.TestCase):
dtstart = datetime(2016, 2, 20, 17, 0, 0)
time_obj = calendar_time.ICal(dtstart, pattern)
self.assertIsNone(time_obj.get_min_interval())
def test_get_min_interval_when_interval_is_bigger_than_default_rate(self):
pattern = (
"BEGIN:VEVENT\n"
"RRULE:FREQ=MINUTELY;INTERVAL=5;\n"
"END:VEVENT"
)
dtstart = datetime(2016, 2, 20, 17, 0, 0)
time_obj = calendar_time.ICal(dtstart, pattern)
self.assertEqual(300, time_obj.get_min_interval())