~yerinalexey/pcrond

pcrond/test_scheduler.py -rwxr-xr-x 9.7 KiB
58f77bb3 — Alexey Yerin fix: crontabfile -> crontab 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python
"""Unit tests for pcrond.py"""
import unittest
import sys
from datetime import datetime as d, timedelta
from pcrond import Scheduler, Job, Parser

scheduler = Scheduler()

def do_nothing():
    pass


def modify_obj(obj):
    def f():
        obj['modified'] = True
    return f


class SchedulerTests(unittest.TestCase):
    def setUp(self):
        scheduler.clear()
        self.parser = Parser()

    def test_split_tokens(self):
        with self.assertRaises(ValueError):
            self.parser.split_tokens("1/2/3")
        with self.assertRaises(ValueError):
            self.parser.split_tokens("1-2-3")
        with self.assertRaises(ValueError):
            self.parser.split_tokens("1/3")
        with self.assertRaises(ValueError):
            self.parser.split_tokens("1-2/goofy")
        (s, r) = self.parser.split_tokens("1,2-5/3,jul,10-goofy/6")
        assert s == ['1', 'jul']
        assert r == [['2', '5', 3], ['10', 'goofy', 6]]

    def test_decode_token(self):
        assert self.parser.decode_token("1234", {}) == "1234"
        assert self.parser.decode_token("goofy", {'goofy': "1234"}) == "1234"
        assert self.parser.decode_token("goofy", {'goofy': 1234}) == 1234

    def test_get_num_wom(self):
        job = Job()
        assert job.get_num_wom(d(2019, 3, 7)) == 1      # first thursday of month
        assert job.get_num_wom(d(2019, 3, 8)) == 2      # second friday of month
        assert job.get_num_wom(d(2019, 3, 31)) == 5     # fifth sunday of month

    def test_is_last_wom(self):
        job = Job()
        assert not job.is_last_wom(d(2019, 3, 1))
        assert not job.is_last_wom(d(2019, 3, 24))
        assert job.is_last_wom(d(2019, 3, 25))
        assert job.is_last_wom(d(2019, 3, 31))

    def test_job_constructor_basic(self):
        job = Job("* 4 * * *")
        ###
        assert job.allowed_every_min
        ###
        assert not job.allowed_every_hour
        assert job.allowed_hours == set([4])
        ###
        assert job.allowed_every_dom
        assert not job.allowed_last_dom
        ###
        assert job.allowed_every_month
        ###
        assert job.allowed_every_dow
        ###
        assert job.allowed_every_year

    def test_job_constructor_more_complicated(self):
        job = Job("30 */3 * mar-jun,dec MON")
        ###
        assert not job.allowed_every_min
        assert job.allowed_min == set([30])
        ###
        assert not job.allowed_every_hour
        assert job.allowed_hours == set([0, 3, 6, 9, 12, 15, 18, 21])
        ###
        assert job.allowed_every_dom
        assert not job.allowed_last_dom
        ###
        assert not job.allowed_every_month
        assert job.allowed_months == set([3, 4, 5, 6, 12])
        ###
        assert not job.allowed_every_dow
        assert job.allowed_dow == set([0])             # 1 in cron, 0 in python
        ###
        assert job.allowed_every_year

    def test_job_constructor_L_dom(self):
        job = Job("* * L * *")
        assert not job.allowed_every_dom
        assert job.allowed_last_dom
        assert job.allowed_dom == set([-1])
        assert job._should_run_at(d(2019, 3, 31))
        assert not job._should_run_at(d(2019, 3, 28))
        assert job._should_run_at(d(2019, 2, 28))

    def test_job_constructor_W_dom(self):
        job = Job("* * 15w * *")
        assert not job.allowed_every_dom
        assert not job.allowed_last_dom
        assert job.allowed_dom == set()
        assert job.allowed_wdom == set([15])
        assert job._should_run_at(d(2019, 3, 15))       # was fri
        assert not job._should_run_at(d(2018, 12, 15))  # was sat
        assert job._should_run_at(d(2018, 12, 14))      # was fri   *
        assert not job._should_run_at(d(2018, 7, 15))   # was sun
        assert job._should_run_at(d(2018, 7, 16))       # was mon
        ###
        job = Job("* * 1w * *")
        assert not job.allowed_every_dom
        assert not job.allowed_last_dom
        assert job.allowed_dom == set()
        assert job.allowed_wdom == set([1])
        assert job._should_run_at(d(2019, 3, 1))        # was fri
        assert not job._should_run_at(d(2019, 6, 1))    # was sat
        assert job._should_run_at(d(2019, 6, 3))        # was mon

    def test_job_constructor_L_dow(self):
        job = Job("* * * * 5l")      # 5=friday, l=only the last one of the month
        assert job.allowed_every_dom
        assert not job.allowed_last_dom
        assert not job.allowed_every_dow
        assert not job.allowed_dow
        assert job.allowed_dowl == set([4])             # 5 in cron, 4 in python
        assert job._should_run_at(d(2019, 3, 29))       # was fri
        assert not job._should_run_at(d(2019, 3, 28))   # was thu
        assert not job._should_run_at(d(2019, 3, 8))    # was fri

    def test_job_constructor_dow_sharp(self):
        job = Job("* * * * 5#2")      # second friday in the month
        assert job.allowed_every_dom
        assert not job.allowed_last_dom
        assert not job.allowed_every_dow
        assert not job.allowed_dow
        assert not job.allowed_dowl
        assert 2 in job.allowed_dow_sharp.keys()
        assert job.allowed_dow_sharp[2] == set([4])      # 5 in cron, 4 in python
        assert not job._should_run_at(d(2019, 3, 1))     # was fri
        assert job._should_run_at(d(2019, 3, 8))         # was fri
        assert not job._should_run_at(d(2019, 3, 15))    # was fri
        assert not job._should_run_at(d(2019, 3, 22))    # was fri
        assert not job._should_run_at(d(2019, 3, 29))    # was fri
        assert not job._should_run_at(d(2019, 3, 28))    # was thu

    def test_job_constructor_alias(self):
        job = Job("@hourly")
        assert not job.allowed_every_min
        assert job.allowed_every_hour
        assert job.allowed_every_month
        assert job.allowed_every_year

    def test_job_constructor_reverse_order(self):
        job = Job("* 23-4 * * *")
        assert job.allowed_hours == set([23, 0, 1, 2, 3, 4])

    def test_job_constructor_wrong(self):
        with self.assertRaises(ValueError):
            Job("some silly text")
        with self.assertRaises(ValueError):
            Job("* * goofy * *")
        with self.assertRaises(ValueError):
            Job("* * * * * L")
        with self.assertRaises(ValueError):
            Job("* 1-2-3 * *")
        with self.assertRaises(ValueError):
            Job("* 1;2;3 * *")
        with self.assertRaises(ValueError):
            Job("* @hourly")
        with self.assertRaises(ValueError):
            Job("* L * * *")
        with self.assertRaises(ValueError):
            Job("* * 1w2 * *")
        # currently, hour=25 does not raise errors.

    def test_misconfigured_job_wont_break_scheduler(self):
        """
        Ensure an interrupted job definition chain won't break
        the scheduler instance permanently.
        """
        scheduler.cron("* * * * *", do_nothing)
        with self.assertRaises(ValueError):
            scheduler.cron("some very bad string pattern", do_nothing)
        scheduler.run_pending()

    def test_add_job_run_all(self):
        """ schedule a task, then invoke run_all()"""
        test_obj = {'modified': False}
        scheduler.cron("* * * * *", modify_obj(test_obj))
        assert len(scheduler.jobs) == 1
        scheduler.run_all()
        assert test_obj['modified']

    def test_add_job_run_pending(self):
        """ schedule a task for this exact minute, then invoke run_pending()"""
        test_obj = {'modified': False}
        now = d.now()
        scheduler.cron("%d %d * * *" % (now.minute, now.hour), modify_obj(test_obj))
        assert len(scheduler.jobs) == 1
        scheduler.run_pending()
        assert test_obj['modified']

    def test_add_job_run_pending_not(self):
        """ schedule a task for this exact minute+5, then invoke run_pending()"""
        test_obj = {'modified': False}
        now = d.now()
        scheduler.cron("%d %d * * *" % (now.minute+5, now.hour), modify_obj(test_obj))
        assert len(scheduler.jobs) == 1
        scheduler.run_pending()
        assert test_obj['modified'] is False

    def test_split_input_line(self):
        assert scheduler._split_input_line('aaaa%%bbbbbb%cccc%dd%%ee') == ['aaaa%bbbbbb', 'cccc\ndd%ee']
        assert scheduler._split_input_line('aaaa%%bbbbbb') == ['aaaa%bbbbbb']

    def test_load_crontab(self):
        """ load test crontab file """
        import os
        scheduler.load_crontab_file(os.path.join("tests", "crontab.txt"))
        assert len(scheduler.jobs) == 4

    @unittest.skipIf(sys.platform.startswith("win"), "requires *NIX")
    def test_load_crontab_and_main_loop(self):
        # FIXME this is a slow test, currently 1 minute
        import os
        import time
        from threading import Thread

        def run_in_another_thread():
            scheduler.load_crontab_file(os.path.join("tests", "crontab2.txt"))
            scheduler.main_loop()

        start_time = d.now()
        thread = Thread(target=run_in_another_thread)
        thread.start()
        print("Waiting for 3 seconds...")
        time.sleep(3)
        scheduler.ask_for_stop = True
        print("Waiting other thread to stop...")
        thread.join()
        print("Other thread stopped.")
        assert os.path.isfile(os.path.join("tests", "somefile"))
        # FIXME system may be utc or not...
        # assert d.utcfromtimestamp(os.path.getmtime(os.path.join("tests", "somefile"))) >= start_time
        assert d.fromtimestamp(os.path.getmtime(os.path.join("tests", "somefile"))) >= start_time - timedelta(seconds=1)
        # FIXME on Travis CI, I had to add 1 second delay and I don't understand why

    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_load_crontab_and_main_loop_win(self):
        # TODO
        pass


if __name__ == '__main__':
    unittest.main()