forked from python-telegram-bot/python-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_jobqueue.py
More file actions
502 lines (410 loc) · 17.5 KB
/
test_jobqueue.py
File metadata and controls
502 lines (410 loc) · 17.5 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2021
# Leandro Toledo de Souza <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
import calendar
import datetime as dtm
import logging
import os
import platform
import time
from queue import Queue
from time import sleep
import pytest
import pytz
from apscheduler.schedulers import SchedulerNotRunningError
from flaky import flaky
from telegram.ext import JobQueue, Updater, Job, CallbackContext, Dispatcher, ContextTypes
class CustomContext(CallbackContext):
pass
@pytest.fixture(scope="function")
def job_queue(bot, _dp):
jq = JobQueue()
jq.set_dispatcher(_dp)
jq.start()
yield jq
jq.stop()
@pytest.mark.skipif(
os.getenv("GITHUB_ACTIONS", False) and platform.system() in ["Windows", "Darwin"],
reason="On Windows & MacOS precise timings are not accurate.",
)
@flaky(10, 1) # Timings aren't quite perfect
class TestJobQueue:
result = 0
job_time = 0
received_error = None
def test_slot_behaviour(self, job_queue, mro_slots, _dp):
for attr in job_queue.__slots__:
assert getattr(job_queue, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(job_queue)) == len(set(mro_slots(job_queue))), "duplicate slot"
@pytest.fixture(autouse=True)
def reset(self):
self.result = 0
self.job_time = 0
self.received_error = None
def job_run_once(self, context):
self.result += 1
def job_with_exception(self, context):
raise Exception("Test Error")
def job_remove_self(self, context):
self.result += 1
context.job.schedule_removal()
def job_run_once_with_context(self, context):
self.result += context.job.context
def job_datetime_tests(self, context):
self.job_time = time.time()
def job_context_based_callback(self, context):
if (
isinstance(context, CallbackContext)
and isinstance(context.job, Job)
and isinstance(context.update_queue, Queue)
and context.job.context == 2
and context.chat_data is None
and context.user_data is None
and isinstance(context.bot_data, dict)
and context.job_queue is not context.job.job_queue
):
self.result += 1
def error_handler_context(self, update, context):
self.received_error = str(context.error)
def error_handler_raise_error(self, *args):
raise Exception("Failing bigly")
def test_run_once(self, job_queue):
job_queue.run_once(self.job_run_once, 0.01)
sleep(0.02)
assert self.result == 1
def test_run_once_timezone(self, job_queue, timezone):
"""Test the correct handling of aware datetimes"""
# we're parametrizing this with two different UTC offsets to exclude the possibility
# of an xpass when the test is run in a timezone with the same UTC offset
when = dtm.datetime.now(timezone)
job_queue.run_once(self.job_run_once, when)
sleep(0.001)
assert self.result == 1
def test_job_with_context(self, job_queue):
job_queue.run_once(self.job_run_once_with_context, 0.01, context=5)
sleep(0.02)
assert self.result == 5
def test_run_repeating(self, job_queue):
job_queue.run_repeating(self.job_run_once, 0.02)
sleep(0.05)
assert self.result == 2
def test_run_repeating_first(self, job_queue):
job_queue.run_repeating(self.job_run_once, 0.05, first=0.2)
sleep(0.15)
assert self.result == 0
sleep(0.07)
assert self.result == 1
def test_run_repeating_first_timezone(self, job_queue, timezone):
"""Test correct scheduling of job when passing a timezone-aware datetime as ``first``"""
job_queue.run_repeating(
self.job_run_once, 0.1, first=dtm.datetime.now(timezone) + dtm.timedelta(seconds=0.05)
)
sleep(0.1)
assert self.result == 1
def test_run_repeating_last(self, job_queue):
job_queue.run_repeating(self.job_run_once, 0.05, last=0.06)
sleep(0.1)
assert self.result == 1
sleep(0.1)
assert self.result == 1
def test_run_repeating_last_timezone(self, job_queue, timezone):
"""Test correct scheduling of job when passing a timezone-aware datetime as ``first``"""
job_queue.run_repeating(
self.job_run_once, 0.05, last=dtm.datetime.now(timezone) + dtm.timedelta(seconds=0.06)
)
sleep(0.1)
assert self.result == 1
sleep(0.1)
assert self.result == 1
def test_run_repeating_last_before_first(self, job_queue):
with pytest.raises(ValueError, match="'last' must not be before 'first'!"):
job_queue.run_repeating(self.job_run_once, 0.05, first=1, last=0.5)
def test_run_repeating_timedelta(self, job_queue):
job_queue.run_repeating(self.job_run_once, dtm.timedelta(minutes=3.3333e-4))
sleep(0.05)
assert self.result == 2
def test_run_custom(self, job_queue):
job_queue.run_custom(self.job_run_once, {"trigger": "interval", "seconds": 0.02})
sleep(0.05)
assert self.result == 2
def test_multiple(self, job_queue):
job_queue.run_once(self.job_run_once, 0.01)
job_queue.run_once(self.job_run_once, 0.02)
job_queue.run_repeating(self.job_run_once, 0.02)
sleep(0.055)
assert self.result == 4
def test_disabled(self, job_queue):
j1 = job_queue.run_once(self.job_run_once, 0.1)
j2 = job_queue.run_repeating(self.job_run_once, 0.05)
j1.enabled = False
j2.enabled = False
sleep(0.06)
assert self.result == 0
j1.enabled = True
sleep(0.2)
assert self.result == 1
def test_schedule_removal(self, job_queue):
j1 = job_queue.run_once(self.job_run_once, 0.03)
j2 = job_queue.run_repeating(self.job_run_once, 0.02)
sleep(0.025)
j1.schedule_removal()
j2.schedule_removal()
sleep(0.04)
assert self.result == 1
def test_schedule_removal_from_within(self, job_queue):
job_queue.run_repeating(self.job_remove_self, 0.01)
sleep(0.05)
assert self.result == 1
def test_longer_first(self, job_queue):
job_queue.run_once(self.job_run_once, 0.02)
job_queue.run_once(self.job_run_once, 0.01)
sleep(0.015)
assert self.result == 1
def test_error(self, job_queue):
job_queue.run_repeating(self.job_with_exception, 0.01)
job_queue.run_repeating(self.job_run_once, 0.02)
sleep(0.03)
assert self.result == 1
def test_in_updater(self, bot):
u = Updater(bot=bot)
u.job_queue.start()
try:
u.job_queue.run_repeating(self.job_run_once, 0.02)
sleep(0.03)
assert self.result == 1
u.stop()
sleep(1)
assert self.result == 1
finally:
try:
u.stop()
except SchedulerNotRunningError:
pass
def test_time_unit_int(self, job_queue):
# Testing seconds in int
delta = 0.05
expected_time = time.time() + delta
job_queue.run_once(self.job_datetime_tests, delta)
sleep(0.06)
assert pytest.approx(self.job_time) == expected_time
def test_time_unit_dt_timedelta(self, job_queue):
# Testing seconds, minutes and hours as datetime.timedelta object
# This is sufficient to test that it actually works.
interval = dtm.timedelta(seconds=0.05)
expected_time = time.time() + interval.total_seconds()
job_queue.run_once(self.job_datetime_tests, interval)
sleep(0.06)
assert pytest.approx(self.job_time) == expected_time
def test_time_unit_dt_datetime(self, job_queue):
# Testing running at a specific datetime
delta, now = dtm.timedelta(seconds=0.05), dtm.datetime.now(pytz.utc)
when = now + delta
expected_time = (now + delta).timestamp()
job_queue.run_once(self.job_datetime_tests, when)
sleep(0.06)
assert self.job_time == pytest.approx(expected_time)
def test_time_unit_dt_time_today(self, job_queue):
# Testing running at a specific time today
delta, now = 0.05, dtm.datetime.now(pytz.utc)
expected_time = now + dtm.timedelta(seconds=delta)
when = expected_time.time()
expected_time = expected_time.timestamp()
job_queue.run_once(self.job_datetime_tests, when)
sleep(0.06)
assert self.job_time == pytest.approx(expected_time)
def test_time_unit_dt_time_tomorrow(self, job_queue):
# Testing running at a specific time that has passed today. Since we can't wait a day, we
# test if the job's next scheduled execution time has been calculated correctly
delta, now = -2, dtm.datetime.now(pytz.utc)
when = (now + dtm.timedelta(seconds=delta)).time()
expected_time = (now + dtm.timedelta(seconds=delta, days=1)).timestamp()
job_queue.run_once(self.job_datetime_tests, when)
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
assert scheduled_time == pytest.approx(expected_time)
def test_run_daily(self, job_queue):
delta, now = 1, dtm.datetime.now(pytz.utc)
time_of_day = (now + dtm.timedelta(seconds=delta)).time()
expected_reschedule_time = (now + dtm.timedelta(seconds=delta, days=1)).timestamp()
job_queue.run_daily(self.job_run_once, time_of_day)
sleep(delta + 0.1)
assert self.result == 1
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
assert scheduled_time == pytest.approx(expected_reschedule_time)
def test_run_monthly(self, job_queue, timezone):
delta, now = 1, dtm.datetime.now(timezone)
expected_reschedule_time = now + dtm.timedelta(seconds=delta)
time_of_day = expected_reschedule_time.time().replace(tzinfo=timezone)
day = now.day
this_months_days = calendar.monthrange(now.year, now.month)[1]
if now.month == 12:
next_months_days = calendar.monthrange(now.year + 1, 1)[1]
else:
next_months_days = calendar.monthrange(now.year, now.month + 1)[1]
expected_reschedule_time += dtm.timedelta(this_months_days)
if day > next_months_days:
expected_reschedule_time += dtm.timedelta(next_months_days)
expected_reschedule_time = timezone.normalize(expected_reschedule_time)
# Adjust the hour for the special case that between now and next month a DST switch happens
expected_reschedule_time += dtm.timedelta(
hours=time_of_day.hour - expected_reschedule_time.hour
)
expected_reschedule_time = expected_reschedule_time.timestamp()
job_queue.run_monthly(self.job_run_once, time_of_day, day)
sleep(delta + 0.1)
assert self.result == 1
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
assert scheduled_time == pytest.approx(expected_reschedule_time)
def test_run_monthly_non_strict_day(self, job_queue, timezone):
delta, now = 1, dtm.datetime.now(timezone)
expected_reschedule_time = now + dtm.timedelta(seconds=delta)
time_of_day = expected_reschedule_time.time().replace(tzinfo=timezone)
expected_reschedule_time += dtm.timedelta(
calendar.monthrange(now.year, now.month)[1]
) - dtm.timedelta(days=now.day)
# Adjust the hour for the special case that between now & end of month a DST switch happens
expected_reschedule_time = timezone.normalize(expected_reschedule_time)
expected_reschedule_time += dtm.timedelta(
hours=time_of_day.hour - expected_reschedule_time.hour
)
expected_reschedule_time = expected_reschedule_time.timestamp()
job_queue.run_monthly(self.job_run_once, time_of_day, -1)
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
assert scheduled_time == pytest.approx(expected_reschedule_time)
def test_default_tzinfo(self, _dp, tz_bot):
# we're parametrizing this with two different UTC offsets to exclude the possibility
# of an xpass when the test is run in a timezone with the same UTC offset
jq = JobQueue()
original_bot = _dp.bot
_dp.bot = tz_bot
jq.set_dispatcher(_dp)
try:
jq.start()
when = dtm.datetime.now(tz_bot.defaults.tzinfo) + dtm.timedelta(seconds=0.0005)
jq.run_once(self.job_run_once, when.time())
sleep(0.001)
assert self.result == 1
jq.stop()
finally:
_dp.bot = original_bot
def test_get_jobs(self, job_queue):
callback = self.job_context_based_callback
job1 = job_queue.run_once(callback, 10, name="name1")
job2 = job_queue.run_once(callback, 10, name="name1")
job3 = job_queue.run_once(callback, 10, name="name2")
assert job_queue.jobs() == (job1, job2, job3)
assert job_queue.get_jobs_by_name("name1") == (job1, job2)
assert job_queue.get_jobs_by_name("name2") == (job3,)
def test_job_run(self, _dp):
job_queue = JobQueue()
job_queue.set_dispatcher(_dp)
job = job_queue.run_repeating(self.job_context_based_callback, 0.02, context=2)
assert self.result == 0
job.run(_dp)
assert self.result == 1
def test_enable_disable_job(self, job_queue):
job = job_queue.run_repeating(self.job_run_once, 0.02)
sleep(0.05)
assert self.result == 2
job.enabled = False
assert not job.enabled
sleep(0.05)
assert self.result == 2
job.enabled = True
assert job.enabled
sleep(0.05)
assert self.result == 4
def test_remove_job(self, job_queue):
job = job_queue.run_repeating(self.job_run_once, 0.02)
sleep(0.05)
assert self.result == 2
assert not job.removed
job.schedule_removal()
assert job.removed
sleep(0.05)
assert self.result == 2
def test_job_lt_eq(self, job_queue):
job = job_queue.run_repeating(self.job_run_once, 0.02)
assert not job == job_queue
assert not job < job
def test_dispatch_error_context(self, job_queue, dp):
dp.add_error_handler(self.error_handler_context)
job = job_queue.run_once(self.job_with_exception, 0.05)
sleep(0.1)
assert self.received_error == "Test Error"
self.received_error = None
job.run(dp)
assert self.received_error == "Test Error"
# Remove handler
dp.remove_error_handler(self.error_handler_context)
self.received_error = None
job = job_queue.run_once(self.job_with_exception, 0.05)
sleep(0.1)
assert self.received_error is None
job.run(dp)
assert self.received_error is None
def test_dispatch_error_that_raises_errors(self, job_queue, dp, caplog):
dp.add_error_handler(self.error_handler_raise_error)
with caplog.at_level(logging.ERROR):
job = job_queue.run_once(self.job_with_exception, 0.05)
sleep(0.1)
assert len(caplog.records) == 1
rec = caplog.records[-1]
assert "processing the job" in rec.getMessage()
assert "uncaught error was raised while handling" in rec.getMessage()
caplog.clear()
with caplog.at_level(logging.ERROR):
job.run(dp)
assert len(caplog.records) == 1
rec = caplog.records[-1]
assert "processing the job" in rec.getMessage()
assert "uncaught error was raised while handling" in rec.getMessage()
caplog.clear()
# Remove handler
dp.remove_error_handler(self.error_handler_raise_error)
self.received_error = None
with caplog.at_level(logging.ERROR):
job = job_queue.run_once(self.job_with_exception, 0.05)
sleep(0.1)
assert len(caplog.records) == 1
rec = caplog.records[-1]
assert "No error handlers are registered" in rec.getMessage()
caplog.clear()
with caplog.at_level(logging.ERROR):
job.run(dp)
assert len(caplog.records) == 1
rec = caplog.records[-1]
assert "No error handlers are registered" in rec.getMessage()
def test_custom_context(self, bot, job_queue):
dispatcher = Dispatcher(
bot,
Queue(),
context_types=ContextTypes(
context=CustomContext, bot_data=int, user_data=float, chat_data=complex
),
)
job_queue.set_dispatcher(dispatcher)
def callback(context):
self.result = (
type(context),
context.user_data,
context.chat_data,
type(context.bot_data),
)
job_queue.run_once(callback, 0.1)
sleep(0.15)
assert self.result == (CustomContext, None, None, int)