test_schedules.py 28 KB

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