test_schedules.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. import pytest
  2. import time
  3. from contextlib import contextmanager
  4. from datetime import datetime, timedelta
  5. from pickle import dumps, loads
  6. from case import Case, Mock, skip
  7. from celery.schedules import (
  8. ParseException, crontab, crontab_parser, schedule, solar,
  9. )
  10. assertions = Case('__init__')
  11. @contextmanager
  12. def patch_crontab_nowfun(cls, retval):
  13. prev_nowfun = cls.nowfun
  14. cls.nowfun = lambda: retval
  15. try:
  16. yield
  17. finally:
  18. cls.nowfun = prev_nowfun
  19. @skip.unless_module('ephem')
  20. class test_solar:
  21. def setup(self):
  22. self.s = solar('sunrise', 60, 30, app=self.app)
  23. def test_reduce(self):
  24. fun, args = self.s.__reduce__()
  25. assert fun(*args) == self.s
  26. def test_eq(self):
  27. assert self.s == solar('sunrise', 60, 30, app=self.app)
  28. assert self.s != solar('sunset', 60, 30, app=self.app)
  29. assert self.s != schedule(10)
  30. def test_repr(self):
  31. assert repr(self.s)
  32. def test_is_due(self):
  33. self.s.remaining_estimate = Mock(name='rem')
  34. self.s.remaining_estimate.return_value = timedelta(seconds=0)
  35. assert self.s.is_due(datetime.utcnow()).is_due
  36. def test_is_due__not_due(self):
  37. self.s.remaining_estimate = Mock(name='rem')
  38. self.s.remaining_estimate.return_value = timedelta(hours=10)
  39. assert not self.s.is_due(datetime.utcnow()).is_due
  40. def test_remaining_estimate(self):
  41. self.s.cal = Mock(name='cal')
  42. self.s.cal.next_rising().datetime.return_value = datetime.utcnow()
  43. self.s.remaining_estimate(datetime.utcnow())
  44. def test_coordinates(self):
  45. with pytest.raises(ValueError):
  46. solar('sunrise', -120, 60, app=self.app)
  47. with pytest.raises(ValueError):
  48. solar('sunrise', 120, 60, app=self.app)
  49. with pytest.raises(ValueError):
  50. solar('sunrise', 60, -200, app=self.app)
  51. with pytest.raises(ValueError):
  52. solar('sunrise', 60, 200, app=self.app)
  53. def test_invalid_event(self):
  54. with pytest.raises(ValueError):
  55. solar('asdqwewqew', 60, 60, app=self.app)
  56. class test_schedule:
  57. def test_ne(self):
  58. s1 = schedule(10, app=self.app)
  59. s2 = schedule(12, app=self.app)
  60. s3 = schedule(10, app=self.app)
  61. assert s1 == s3
  62. assert s1 != s2
  63. def test_pickle(self):
  64. s1 = schedule(10, app=self.app)
  65. fun, args = s1.__reduce__()
  66. s2 = fun(*args)
  67. assert s1 == s2
  68. class test_crontab_parser:
  69. def crontab(self, *args, **kwargs):
  70. return crontab(*args, **dict(kwargs, app=self.app))
  71. def test_crontab_reduce(self):
  72. assert loads(dumps(self.crontab('*')))
  73. def test_range_steps_not_enough(self):
  74. with pytest.raises(crontab_parser.ParseException):
  75. crontab_parser(24)._range_steps([1])
  76. def test_parse_star(self):
  77. assert crontab_parser(24).parse('*') == set(range(24))
  78. assert crontab_parser(60).parse('*') == set(range(60))
  79. assert crontab_parser(7).parse('*') == set(range(7))
  80. assert crontab_parser(31, 1).parse('*') == set(range(1, 31 + 1))
  81. assert crontab_parser(12, 1).parse('*') == set(range(1, 12 + 1))
  82. def test_parse_range(self):
  83. assert crontab_parser(60).parse('1-10') == set(range(1, 10 + 1))
  84. assert crontab_parser(24).parse('0-20') == set(range(0, 20 + 1))
  85. assert crontab_parser().parse('2-10') == set(range(2, 10 + 1))
  86. assert crontab_parser(60, 1).parse('1-10') == set(range(1, 10 + 1))
  87. def test_parse_range_wraps(self):
  88. assert crontab_parser(12).parse('11-1') == {11, 0, 1}
  89. assert crontab_parser(60, 1).parse('2-1') == set(range(1, 60 + 1))
  90. def test_parse_groups(self):
  91. assert crontab_parser().parse('1,2,3,4') == {1, 2, 3, 4}
  92. assert crontab_parser().parse('0,15,30,45') == {0, 15, 30, 45}
  93. assert crontab_parser(min_=1).parse('1,2,3,4') == {1, 2, 3, 4}
  94. def test_parse_steps(self):
  95. assert crontab_parser(8).parse('*/2') == {0, 2, 4, 6}
  96. assert crontab_parser().parse('*/2') == {i * 2 for i in range(30)}
  97. assert crontab_parser().parse('*/3') == {i * 3 for i in range(20)}
  98. assert crontab_parser(8, 1).parse('*/2') == {1, 3, 5, 7}
  99. assert crontab_parser(min_=1).parse('*/2') == {
  100. i * 2 + 1 for i in range(30)
  101. }
  102. assert crontab_parser(min_=1).parse('*/3') == {
  103. i * 3 + 1 for i in range(20)
  104. }
  105. def test_parse_composite(self):
  106. assert crontab_parser(8).parse('*/2') == {0, 2, 4, 6}
  107. assert crontab_parser().parse('2-9/5') == {2, 7}
  108. assert crontab_parser().parse('2-10/5') == {2, 7}
  109. assert crontab_parser(min_=1).parse('55-5/3') == {55, 58, 1, 4}
  110. assert crontab_parser().parse('2-11/5,3') == {2, 3, 7}
  111. assert crontab_parser().parse('2-4/3,*/5,0-21/4') == {
  112. 0, 2, 4, 5, 8, 10, 12, 15, 16, 20, 25, 30, 35, 40, 45, 50, 55,
  113. }
  114. assert crontab_parser().parse('1-9/2') == {1, 3, 5, 7, 9}
  115. assert crontab_parser(8, 1).parse('*/2') == {1, 3, 5, 7}
  116. assert crontab_parser(min_=1).parse('2-9/5') == {2, 7}
  117. assert crontab_parser(min_=1).parse('2-10/5') == {2, 7}
  118. assert crontab_parser(min_=1).parse('2-11/5,3') == {2, 3, 7}
  119. assert crontab_parser(min_=1).parse('2-4/3,*/5,1-21/4') == {
  120. 1, 2, 5, 6, 9, 11, 13, 16, 17, 21, 26, 31, 36, 41, 46, 51, 56,
  121. }
  122. assert crontab_parser(min_=1).parse('1-9/2') == {1, 3, 5, 7, 9}
  123. def test_parse_errors_on_empty_string(self):
  124. with pytest.raises(ParseException):
  125. crontab_parser(60).parse('')
  126. def test_parse_errors_on_empty_group(self):
  127. with pytest.raises(ParseException):
  128. crontab_parser(60).parse('1,,2')
  129. def test_parse_errors_on_empty_steps(self):
  130. with pytest.raises(ParseException):
  131. crontab_parser(60).parse('*/')
  132. def test_parse_errors_on_negative_number(self):
  133. with pytest.raises(ParseException):
  134. crontab_parser(60).parse('-20')
  135. def test_parse_errors_on_lt_min(self):
  136. crontab_parser(min_=1).parse('1')
  137. with pytest.raises(ValueError):
  138. crontab_parser(12, 1).parse('0')
  139. with pytest.raises(ValueError):
  140. crontab_parser(24, 1).parse('12-0')
  141. def test_parse_errors_on_gt_max(self):
  142. crontab_parser(1).parse('0')
  143. with pytest.raises(ValueError):
  144. crontab_parser(1).parse('1')
  145. with pytest.raises(ValueError):
  146. crontab_parser(60).parse('61-0')
  147. def test_expand_cronspec_eats_iterables(self):
  148. assert crontab._expand_cronspec(iter([1, 2, 3]), 100) == {1, 2, 3}
  149. assert crontab._expand_cronspec(iter([1, 2, 3]), 100, 1) == {1, 2, 3}
  150. def test_expand_cronspec_invalid_type(self):
  151. with pytest.raises(TypeError):
  152. crontab._expand_cronspec(object(), 100)
  153. def test_repr(self):
  154. assert '*' in repr(self.crontab('*'))
  155. def test_eq(self):
  156. assert (self.crontab(day_of_week='1, 2') ==
  157. self.crontab(day_of_week='1-2'))
  158. assert (self.crontab(day_of_month='1, 16, 31') ==
  159. self.crontab(day_of_month='*/15'))
  160. assert (
  161. self.crontab(
  162. minute='1', hour='2', day_of_week='5',
  163. day_of_month='10', month_of_year='5') ==
  164. self.crontab(
  165. minute='1', hour='2', day_of_week='5',
  166. day_of_month='10', month_of_year='5'))
  167. assert crontab(minute='1') != crontab(minute='2')
  168. assert (self.crontab(month_of_year='1') !=
  169. self.crontab(month_of_year='2'))
  170. assert object() != self.crontab(minute='1')
  171. assert self.crontab(minute='1') != object()
  172. assert crontab(month_of_year='1') != schedule(10)
  173. class test_crontab_remaining_estimate:
  174. def crontab(self, *args, **kwargs):
  175. return crontab(*args, **dict(kwargs, app=self.app))
  176. def next_ocurrance(self, crontab, now):
  177. crontab.nowfun = lambda: now
  178. return now + crontab.remaining_estimate(now)
  179. def test_next_minute(self):
  180. next = self.next_ocurrance(
  181. self.crontab(), datetime(2010, 9, 11, 14, 30, 15),
  182. )
  183. assert next == datetime(2010, 9, 11, 14, 31)
  184. def test_not_next_minute(self):
  185. next = self.next_ocurrance(
  186. self.crontab(), datetime(2010, 9, 11, 14, 59, 15),
  187. )
  188. assert next == datetime(2010, 9, 11, 15, 0)
  189. def test_this_hour(self):
  190. next = self.next_ocurrance(
  191. self.crontab(minute=[5, 42]), datetime(2010, 9, 11, 14, 30, 15),
  192. )
  193. assert next == datetime(2010, 9, 11, 14, 42)
  194. def test_not_this_hour(self):
  195. next = self.next_ocurrance(
  196. self.crontab(minute=[5, 10, 15]),
  197. datetime(2010, 9, 11, 14, 30, 15),
  198. )
  199. assert next == datetime(2010, 9, 11, 15, 5)
  200. def test_today(self):
  201. next = self.next_ocurrance(
  202. self.crontab(minute=[5, 42], hour=[12, 17]),
  203. datetime(2010, 9, 11, 14, 30, 15),
  204. )
  205. assert next == datetime(2010, 9, 11, 17, 5)
  206. def test_not_today(self):
  207. next = self.next_ocurrance(
  208. self.crontab(minute=[5, 42], hour=[12]),
  209. datetime(2010, 9, 11, 14, 30, 15),
  210. )
  211. assert next == datetime(2010, 9, 12, 12, 5)
  212. def test_weekday(self):
  213. next = self.next_ocurrance(
  214. self.crontab(minute=30, hour=14, day_of_week='sat'),
  215. datetime(2010, 9, 11, 14, 30, 15),
  216. )
  217. assert next == datetime(2010, 9, 18, 14, 30)
  218. def test_not_weekday(self):
  219. next = self.next_ocurrance(
  220. self.crontab(minute=[5, 42], day_of_week='mon-fri'),
  221. datetime(2010, 9, 11, 14, 30, 15),
  222. )
  223. assert next == datetime(2010, 9, 13, 0, 5)
  224. def test_monthday(self):
  225. next = self.next_ocurrance(
  226. self.crontab(minute=30, hour=14, day_of_month=18),
  227. datetime(2010, 9, 11, 14, 30, 15),
  228. )
  229. assert next == datetime(2010, 9, 18, 14, 30)
  230. def test_not_monthday(self):
  231. next = self.next_ocurrance(
  232. self.crontab(minute=[5, 42], day_of_month=29),
  233. datetime(2010, 1, 22, 14, 30, 15),
  234. )
  235. assert next == datetime(2010, 1, 29, 0, 5)
  236. def test_weekday_monthday(self):
  237. next = self.next_ocurrance(
  238. self.crontab(minute=30, hour=14,
  239. day_of_week='mon', day_of_month=18),
  240. datetime(2010, 1, 18, 14, 30, 15),
  241. )
  242. assert next == datetime(2010, 10, 18, 14, 30)
  243. def test_monthday_not_weekday(self):
  244. next = self.next_ocurrance(
  245. self.crontab(minute=[5, 42], day_of_week='sat', day_of_month=29),
  246. datetime(2010, 1, 29, 0, 5, 15),
  247. )
  248. assert next == datetime(2010, 5, 29, 0, 5)
  249. def test_weekday_not_monthday(self):
  250. next = self.next_ocurrance(
  251. self.crontab(minute=[5, 42], day_of_week='mon', day_of_month=18),
  252. datetime(2010, 1, 11, 0, 5, 15),
  253. )
  254. assert next == datetime(2010, 1, 18, 0, 5)
  255. def test_not_weekday_not_monthday(self):
  256. next = self.next_ocurrance(
  257. self.crontab(minute=[5, 42], day_of_week='mon', day_of_month=18),
  258. datetime(2010, 1, 10, 0, 5, 15),
  259. )
  260. assert next == datetime(2010, 1, 18, 0, 5)
  261. def test_leapday(self):
  262. next = self.next_ocurrance(
  263. self.crontab(minute=30, hour=14, day_of_month=29),
  264. datetime(2012, 1, 29, 14, 30, 15),
  265. )
  266. assert next == datetime(2012, 2, 29, 14, 30)
  267. def test_not_leapday(self):
  268. next = self.next_ocurrance(
  269. self.crontab(minute=30, hour=14, day_of_month=29),
  270. datetime(2010, 1, 29, 14, 30, 15),
  271. )
  272. assert next == datetime(2010, 3, 29, 14, 30)
  273. def test_weekmonthdayyear(self):
  274. next = self.next_ocurrance(
  275. self.crontab(minute=30, hour=14, day_of_week='fri',
  276. day_of_month=29, month_of_year=1),
  277. datetime(2010, 1, 22, 14, 30, 15),
  278. )
  279. assert next == datetime(2010, 1, 29, 14, 30)
  280. def test_monthdayyear_not_week(self):
  281. next = self.next_ocurrance(
  282. self.crontab(minute=[5, 42], day_of_week='wed,thu',
  283. day_of_month=29, month_of_year='1,4,7'),
  284. datetime(2010, 1, 29, 14, 30, 15),
  285. )
  286. assert next == datetime(2010, 4, 29, 0, 5)
  287. def test_weekdaymonthyear_not_monthday(self):
  288. next = self.next_ocurrance(
  289. self.crontab(minute=30, hour=14, day_of_week='fri',
  290. day_of_month=29, month_of_year='1-10'),
  291. datetime(2010, 1, 29, 14, 30, 15),
  292. )
  293. assert next == datetime(2010, 10, 29, 14, 30)
  294. def test_weekmonthday_not_monthyear(self):
  295. next = self.next_ocurrance(
  296. self.crontab(minute=[5, 42], day_of_week='fri',
  297. day_of_month=29, month_of_year='2-10'),
  298. datetime(2010, 1, 29, 14, 30, 15),
  299. )
  300. assert next == datetime(2010, 10, 29, 0, 5)
  301. def test_weekday_not_monthdayyear(self):
  302. next = self.next_ocurrance(
  303. self.crontab(minute=[5, 42], day_of_week='mon',
  304. day_of_month=18, month_of_year='2-10'),
  305. datetime(2010, 1, 11, 0, 5, 15),
  306. )
  307. assert next == datetime(2010, 10, 18, 0, 5)
  308. def test_monthday_not_weekdaymonthyear(self):
  309. next = self.next_ocurrance(
  310. self.crontab(minute=[5, 42], day_of_week='mon',
  311. day_of_month=29, month_of_year='2-4'),
  312. datetime(2010, 1, 29, 0, 5, 15),
  313. )
  314. assert next == datetime(2010, 3, 29, 0, 5)
  315. def test_monthyear_not_weekmonthday(self):
  316. next = self.next_ocurrance(
  317. self.crontab(minute=[5, 42], day_of_week='mon',
  318. day_of_month=29, month_of_year='2-4'),
  319. datetime(2010, 2, 28, 0, 5, 15),
  320. )
  321. assert next == datetime(2010, 3, 29, 0, 5)
  322. def test_not_weekmonthdayyear(self):
  323. next = self.next_ocurrance(
  324. self.crontab(minute=[5, 42], day_of_week='fri,sat',
  325. day_of_month=29, month_of_year='2-10'),
  326. datetime(2010, 1, 28, 14, 30, 15),
  327. )
  328. assert next == datetime(2010, 5, 29, 0, 5)
  329. def test_invalid_specification(self):
  330. # *** WARNING ***
  331. # This test triggers an infinite loop in case of a regression
  332. with pytest.raises(RuntimeError):
  333. self.next_ocurrance(
  334. self.crontab(day_of_month=31, month_of_year=4),
  335. datetime(2010, 1, 28, 14, 30, 15),
  336. )
  337. def test_leapyear(self):
  338. next = self.next_ocurrance(
  339. self.crontab(minute=30, hour=14, day_of_month=29, month_of_year=2),
  340. datetime(2012, 2, 29, 14, 30),
  341. )
  342. assert next == datetime(2016, 2, 29, 14, 30)
  343. class test_crontab_is_due:
  344. def setup(self):
  345. self.now = self.app.now()
  346. self.next_minute = 60 - self.now.second - 1e-6 * self.now.microsecond
  347. self.every_minute = self.crontab()
  348. self.quarterly = self.crontab(minute='*/15')
  349. self.hourly = self.crontab(minute=30)
  350. self.daily = self.crontab(hour=7, minute=30)
  351. self.weekly = self.crontab(hour=7, minute=30, day_of_week='thursday')
  352. self.monthly = self.crontab(
  353. hour=7, minute=30, day_of_week='thursday', day_of_month='8-14',
  354. )
  355. self.monthly_moy = self.crontab(
  356. hour=22, day_of_week='*', month_of_year='2',
  357. day_of_month='26,27,28',
  358. )
  359. self.yearly = self.crontab(
  360. hour=7, minute=30, day_of_week='thursday',
  361. day_of_month='8-14', month_of_year=3,
  362. )
  363. def crontab(self, *args, **kwargs):
  364. return crontab(*args, app=self.app, **kwargs)
  365. def test_default_crontab_spec(self):
  366. c = self.crontab()
  367. assert c.minute == set(range(60))
  368. assert c.hour == set(range(24))
  369. assert c.day_of_week == set(range(7))
  370. assert c.day_of_month == set(range(1, 32))
  371. assert c.month_of_year == set(range(1, 13))
  372. def test_simple_crontab_spec(self):
  373. c = self.crontab(minute=30)
  374. assert c.minute == {30}
  375. assert c.hour == set(range(24))
  376. assert c.day_of_week == set(range(7))
  377. assert c.day_of_month == set(range(1, 32))
  378. assert c.month_of_year == set(range(1, 13))
  379. @pytest.mark.parametrize('minute,expected', [
  380. (30, {30}),
  381. ('30', {30}),
  382. ((30, 40, 50), {30, 40, 50}),
  383. ((30, 40, 50, 51), {30, 40, 50, 51})
  384. ])
  385. def test_crontab_spec_minute_formats(self, minute, expected):
  386. c = self.crontab(minute=minute)
  387. assert c.minute == expected
  388. @pytest.mark.parametrize('minute', [60, '0-100'])
  389. def test_crontab_spec_invalid_minute(self, minute):
  390. with pytest.raises(ValueError):
  391. self.crontab(minute=minute)
  392. @pytest.mark.parametrize('hour,expected', [
  393. (6, {6}),
  394. ('5', {5}),
  395. ((4, 8, 12), {4, 8, 12}),
  396. ])
  397. def test_crontab_spec_hour_formats(self, hour, expected):
  398. c = self.crontab(hour=hour)
  399. assert c.hour == expected
  400. @pytest.mark.parametrize('hour', [24, '0-30'])
  401. def test_crontab_spec_invalid_hour(self, hour):
  402. with pytest.raises(ValueError):
  403. self.crontab(hour=hour)
  404. @pytest.mark.parametrize('day_of_week,expected', [
  405. (5, {5}),
  406. ('5', {5}),
  407. ('fri', {5}),
  408. ('tuesday,sunday,fri', {0, 2, 5}),
  409. ('mon-fri', {1, 2, 3, 4, 5}),
  410. ('*/2', {0, 2, 4, 6}),
  411. ])
  412. def test_crontab_spec_dow_formats(self, day_of_week, expected):
  413. c = self.crontab(day_of_week=day_of_week)
  414. assert c.day_of_week == expected
  415. @pytest.mark.parametrize('day_of_week', [
  416. 'fooday-barday', '1,4,foo', '7', '12',
  417. ])
  418. def test_crontab_spec_invalid_dow(self, day_of_week):
  419. with pytest.raises(ValueError):
  420. self.crontab(day_of_week=day_of_week)
  421. @pytest.mark.parametrize('day_of_month,expected', [
  422. (5, {5}),
  423. ('5', {5}),
  424. ('2,4,6', {2, 4, 6}),
  425. ('*/5', {1, 6, 11, 16, 21, 26, 31}),
  426. ])
  427. def test_crontab_spec_dom_formats(self, day_of_month, expected):
  428. c = self.crontab(day_of_month=day_of_month)
  429. assert c.day_of_month == expected
  430. @pytest.mark.parametrize('day_of_month', [0, '0-10', 32, '31,32'])
  431. def test_crontab_spec_invalid_dom(self, day_of_month):
  432. with pytest.raises(ValueError):
  433. self.crontab(day_of_month=day_of_month)
  434. @pytest.mark.parametrize('month_of_year,expected', [
  435. (1, {1}),
  436. ('1', {1}),
  437. ('2,4,6', {2, 4, 6}),
  438. ('*/2', {1, 3, 5, 7, 9, 11}),
  439. ('2-12/2', {2, 4, 6, 8, 10, 12}),
  440. ])
  441. def test_crontab_spec_moy_formats(self, month_of_year, expected):
  442. c = self.crontab(month_of_year=month_of_year)
  443. assert c.month_of_year == expected
  444. @pytest.mark.parametrize('month_of_year', [0, '0-5', 13, '12,13'])
  445. def test_crontab_spec_invalid_moy(self, month_of_year):
  446. with pytest.raises(ValueError):
  447. self.crontab(month_of_year=month_of_year)
  448. def seconds_almost_equal(self, a, b, precision):
  449. for index, skew in enumerate((+1, -1, 0)):
  450. try:
  451. assertions.assertAlmostEqual(a, b + skew, precision)
  452. except Exception as exc:
  453. # AssertionError != builtins.AssertionError in py.test
  454. if 'AssertionError' in str(exc):
  455. if index + 1 >= 3:
  456. raise
  457. else:
  458. break
  459. def test_every_minute_execution_is_due(self):
  460. last_ran = self.now - timedelta(seconds=61)
  461. due, remaining = self.every_minute.is_due(last_ran)
  462. self.assert_relativedelta(self.every_minute, last_ran)
  463. assert due
  464. self.seconds_almost_equal(remaining, self.next_minute, 1)
  465. def assert_relativedelta(self, due, last_ran):
  466. try:
  467. from dateutil.relativedelta import relativedelta
  468. except ImportError:
  469. return
  470. l1, d1, n1 = due.remaining_delta(last_ran)
  471. l2, d2, n2 = due.remaining_delta(last_ran, ffwd=relativedelta)
  472. if not isinstance(d1, relativedelta):
  473. assert l1 == l2
  474. for field, value in d1._fields().items():
  475. assert getattr(d1, field) == value
  476. assert not d2.years
  477. assert not d2.months
  478. assert not d2.days
  479. assert not d2.leapdays
  480. assert not d2.hours
  481. assert not d2.minutes
  482. assert not d2.seconds
  483. assert not d2.microseconds
  484. def test_every_minute_execution_is_not_due(self):
  485. last_ran = self.now - timedelta(seconds=self.now.second)
  486. due, remaining = self.every_minute.is_due(last_ran)
  487. assert not due
  488. self.seconds_almost_equal(remaining, self.next_minute, 1)
  489. def test_execution_is_due_on_saturday(self):
  490. # 29th of May 2010 is a saturday
  491. with patch_crontab_nowfun(self.hourly, datetime(2010, 5, 29, 10, 30)):
  492. last_ran = self.now - timedelta(seconds=61)
  493. due, remaining = self.every_minute.is_due(last_ran)
  494. assert due
  495. self.seconds_almost_equal(remaining, self.next_minute, 1)
  496. def test_execution_is_due_on_sunday(self):
  497. # 30th of May 2010 is a sunday
  498. with patch_crontab_nowfun(self.hourly, datetime(2010, 5, 30, 10, 30)):
  499. last_ran = self.now - timedelta(seconds=61)
  500. due, remaining = self.every_minute.is_due(last_ran)
  501. assert due
  502. self.seconds_almost_equal(remaining, self.next_minute, 1)
  503. def test_execution_is_due_on_monday(self):
  504. # 31st of May 2010 is a monday
  505. with patch_crontab_nowfun(self.hourly, datetime(2010, 5, 31, 10, 30)):
  506. last_ran = self.now - timedelta(seconds=61)
  507. due, remaining = self.every_minute.is_due(last_ran)
  508. assert due
  509. self.seconds_almost_equal(remaining, self.next_minute, 1)
  510. def test_every_hour_execution_is_due(self):
  511. with patch_crontab_nowfun(self.hourly, datetime(2010, 5, 10, 10, 30)):
  512. due, remaining = self.hourly.is_due(datetime(2010, 5, 10, 6, 30))
  513. assert due
  514. assert remaining == 60 * 60
  515. def test_every_hour_execution_is_not_due(self):
  516. with patch_crontab_nowfun(self.hourly, datetime(2010, 5, 10, 10, 29)):
  517. due, remaining = self.hourly.is_due(datetime(2010, 5, 10, 9, 30))
  518. assert not due
  519. assert remaining == 60
  520. def test_first_quarter_execution_is_due(self):
  521. with patch_crontab_nowfun(
  522. self.quarterly, datetime(2010, 5, 10, 10, 15)):
  523. due, remaining = self.quarterly.is_due(
  524. datetime(2010, 5, 10, 6, 30),
  525. )
  526. assert due
  527. assert remaining == 15 * 60
  528. def test_second_quarter_execution_is_due(self):
  529. with patch_crontab_nowfun(
  530. self.quarterly, datetime(2010, 5, 10, 10, 30)):
  531. due, remaining = self.quarterly.is_due(
  532. datetime(2010, 5, 10, 6, 30),
  533. )
  534. assert due
  535. assert remaining == 15 * 60
  536. def test_first_quarter_execution_is_not_due(self):
  537. with patch_crontab_nowfun(
  538. self.quarterly, datetime(2010, 5, 10, 10, 14)):
  539. due, remaining = self.quarterly.is_due(
  540. datetime(2010, 5, 10, 10, 0),
  541. )
  542. assert not due
  543. assert remaining == 60
  544. def test_second_quarter_execution_is_not_due(self):
  545. with patch_crontab_nowfun(
  546. self.quarterly, datetime(2010, 5, 10, 10, 29)):
  547. due, remaining = self.quarterly.is_due(
  548. datetime(2010, 5, 10, 10, 15),
  549. )
  550. assert not due
  551. assert remaining == 60
  552. def test_daily_execution_is_due(self):
  553. with patch_crontab_nowfun(self.daily, datetime(2010, 5, 10, 7, 30)):
  554. due, remaining = self.daily.is_due(datetime(2010, 5, 9, 7, 30))
  555. assert due
  556. assert remaining == 24 * 60 * 60
  557. def test_daily_execution_is_not_due(self):
  558. with patch_crontab_nowfun(self.daily, datetime(2010, 5, 10, 10, 30)):
  559. due, remaining = self.daily.is_due(datetime(2010, 5, 10, 7, 30))
  560. assert not due
  561. assert remaining == 21 * 60 * 60
  562. def test_weekly_execution_is_due(self):
  563. with patch_crontab_nowfun(self.weekly, datetime(2010, 5, 6, 7, 30)):
  564. due, remaining = self.weekly.is_due(datetime(2010, 4, 30, 7, 30))
  565. assert due
  566. assert remaining == 7 * 24 * 60 * 60
  567. def test_weekly_execution_is_not_due(self):
  568. with patch_crontab_nowfun(self.weekly, datetime(2010, 5, 7, 10, 30)):
  569. due, remaining = self.weekly.is_due(datetime(2010, 5, 6, 7, 30))
  570. assert not due
  571. assert remaining == 6 * 24 * 60 * 60 - 3 * 60 * 60
  572. def test_monthly_execution_is_due(self):
  573. with patch_crontab_nowfun(self.monthly, datetime(2010, 5, 13, 7, 30)):
  574. due, remaining = self.monthly.is_due(datetime(2010, 4, 8, 7, 30))
  575. assert due
  576. assert remaining == 28 * 24 * 60 * 60
  577. def test_monthly_execution_is_not_due(self):
  578. with patch_crontab_nowfun(self.monthly, datetime(2010, 5, 9, 10, 30)):
  579. due, remaining = self.monthly.is_due(datetime(2010, 4, 8, 7, 30))
  580. assert not due
  581. assert remaining == 4 * 24 * 60 * 60 - 3 * 60 * 60
  582. def test_monthly_moy_execution_is_due(self):
  583. with patch_crontab_nowfun(
  584. self.monthly_moy, datetime(2014, 2, 26, 22, 0)):
  585. due, remaining = self.monthly_moy.is_due(
  586. datetime(2013, 7, 4, 10, 0),
  587. )
  588. assert due
  589. assert remaining == 60.0
  590. @skip.todo('unstable test')
  591. def test_monthly_moy_execution_is_not_due(self):
  592. with patch_crontab_nowfun(
  593. self.monthly_moy, datetime(2013, 6, 28, 14, 30)):
  594. due, remaining = self.monthly_moy.is_due(
  595. datetime(2013, 6, 28, 22, 14),
  596. )
  597. assert not due
  598. attempt = (
  599. time.mktime(datetime(2014, 2, 26, 22, 0).timetuple()) -
  600. time.mktime(datetime(2013, 6, 28, 14, 30).timetuple()) -
  601. 60 * 60
  602. )
  603. assert remaining == attempt
  604. def test_monthly_moy_execution_is_due2(self):
  605. with patch_crontab_nowfun(
  606. self.monthly_moy, datetime(2014, 2, 26, 22, 0)):
  607. due, remaining = self.monthly_moy.is_due(
  608. datetime(2013, 2, 28, 10, 0),
  609. )
  610. assert due
  611. assert remaining == 60.0
  612. def test_monthly_moy_execution_is_not_due2(self):
  613. with patch_crontab_nowfun(
  614. self.monthly_moy, datetime(2014, 2, 26, 21, 0)):
  615. due, remaining = self.monthly_moy.is_due(
  616. datetime(2013, 6, 28, 22, 14),
  617. )
  618. assert not due
  619. attempt = 60 * 60
  620. assert remaining == attempt
  621. def test_yearly_execution_is_due(self):
  622. with patch_crontab_nowfun(self.yearly, datetime(2010, 3, 11, 7, 30)):
  623. due, remaining = self.yearly.is_due(datetime(2009, 3, 12, 7, 30))
  624. assert due
  625. assert remaining == 364 * 24 * 60 * 60
  626. def test_yearly_execution_is_not_due(self):
  627. with patch_crontab_nowfun(self.yearly, datetime(2010, 3, 7, 10, 30)):
  628. due, remaining = self.yearly.is_due(datetime(2009, 3, 12, 7, 30))
  629. assert not due
  630. assert remaining == 4 * 24 * 60 * 60 - 3 * 60 * 60