2016-01-04 01:56:22 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2018-01-04 16:16:06 +01:00
|
|
|
# Copyright (C) 2015-2018
|
2016-01-05 14:12:03 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2016-01-04 01:56:22 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2017-08-11 23:58:41 +02:00
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
2016-01-04 01:56:22 +01:00
|
|
|
# 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
|
2017-08-11 23:58:41 +02:00
|
|
|
# GNU Lesser Public License for more details.
|
2016-01-04 01:56:22 +01:00
|
|
|
#
|
2017-08-11 23:58:41 +02:00
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
2016-01-04 01:56:22 +01:00
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2016-11-08 23:39:25 +01:00
|
|
|
import datetime
|
2017-09-14 17:54:00 +02:00
|
|
|
import os
|
2018-09-21 08:57:01 +02:00
|
|
|
import sys
|
2016-11-08 23:39:25 +01:00
|
|
|
import time
|
2018-09-21 08:57:01 +02:00
|
|
|
from queue import Queue
|
2016-01-04 01:56:22 +01:00
|
|
|
from time import sleep
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
import pytest
|
|
|
|
from flaky import flaky
|
2016-09-06 15:38:07 +02:00
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
from telegram.ext import JobQueue, Updater, Job, CallbackContext
|
|
|
|
from telegram.utils.deprecate import TelegramDeprecationWarning
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
@pytest.fixture(scope='function')
|
2018-09-21 08:57:01 +02:00
|
|
|
def job_queue(bot, _dp):
|
|
|
|
jq = JobQueue()
|
|
|
|
jq.set_dispatcher(_dp)
|
2017-08-11 23:58:41 +02:00
|
|
|
jq.start()
|
|
|
|
yield jq
|
|
|
|
jq.stop()
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
|
2017-09-14 17:54:00 +02:00
|
|
|
@pytest.mark.skipif(os.getenv('APPVEYOR'), reason="On Appveyor precise timings are not accurate.")
|
2017-08-11 23:58:41 +02:00
|
|
|
@flaky(10, 1) # Timings aren't quite perfect
|
|
|
|
class TestJobQueue(object):
|
|
|
|
result = 0
|
|
|
|
job_time = 0
|
2016-01-04 01:56:22 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def reset(self):
|
2016-01-04 01:56:22 +01:00
|
|
|
self.result = 0
|
2016-11-08 23:39:25 +01:00
|
|
|
self.job_time = 0
|
2016-01-04 01:56:22 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def job_run_once(self, bot, job):
|
2016-01-04 01:56:22 +01:00
|
|
|
self.result += 1
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def job_with_exception(self, bot, job):
|
|
|
|
raise Exception('Test Error')
|
2016-01-04 02:36:15 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def job_remove_self(self, bot, job):
|
2016-05-25 22:51:13 +02:00
|
|
|
self.result += 1
|
|
|
|
job.schedule_removal()
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def job_run_once_with_context(self, bot, job):
|
2016-05-26 14:07:44 +02:00
|
|
|
self.result += job.context
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def job_datetime_tests(self, bot, job):
|
2016-12-13 23:38:13 +01:00
|
|
|
self.job_time = time.time()
|
2016-11-08 23:39:25 +01:00
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
def job_context_based_callback(self, context):
|
2019-02-08 20:55:40 +01:00
|
|
|
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 context.job_queue is context.job.job_queue):
|
2018-09-21 08:57:01 +02:00
|
|
|
self.result += 1
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
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_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_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)
|
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
j1.enabled = False
|
2017-08-11 23:58:41 +02:00
|
|
|
j2.enabled = False
|
|
|
|
|
|
|
|
sleep(0.06)
|
|
|
|
|
|
|
|
assert self.result == 0
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
j1.enabled = True
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
sleep(0.2)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
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)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
|
|
|
j1.schedule_removal()
|
2017-08-11 23:58:41 +02:00
|
|
|
j2.schedule_removal()
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
sleep(0.04)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 1
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def test_schedule_removal_from_within(self, job_queue):
|
|
|
|
job_queue.run_repeating(self.job_remove_self, 0.01)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
sleep(0.05)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 1
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
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)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
sleep(0.015)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 1
|
2016-01-05 13:32:19 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
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)
|
2016-09-27 09:21:35 +02:00
|
|
|
u.job_queue.start()
|
2016-09-06 15:38:07 +02:00
|
|
|
try:
|
2017-08-11 23:58:41 +02:00
|
|
|
u.job_queue.run_repeating(self.job_run_once, 0.02)
|
|
|
|
sleep(0.03)
|
|
|
|
assert self.result == 1
|
2016-09-06 15:38:07 +02:00
|
|
|
u.stop()
|
2017-08-11 23:58:41 +02:00
|
|
|
sleep(1)
|
|
|
|
assert self.result == 1
|
2016-09-06 15:38:07 +02:00
|
|
|
finally:
|
|
|
|
u.stop()
|
2016-01-04 11:08:43 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def test_time_unit_int(self, job_queue):
|
2016-11-08 23:39:25 +01:00
|
|
|
# Testing seconds in int
|
2017-08-11 23:58:41 +02:00
|
|
|
delta = 0.05
|
2016-12-14 16:27:45 +01:00
|
|
|
expected_time = time.time() + delta
|
2016-11-08 23:39:25 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
job_queue.run_once(self.job_datetime_tests, delta)
|
|
|
|
sleep(0.06)
|
|
|
|
assert pytest.approx(self.job_time) == expected_time
|
2016-11-08 23:39:25 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def test_time_unit_dt_timedelta(self, job_queue):
|
2016-11-08 23:39:25 +01:00
|
|
|
# Testing seconds, minutes and hours as datetime.timedelta object
|
|
|
|
# This is sufficient to test that it actually works.
|
2017-08-11 23:58:41 +02:00
|
|
|
interval = datetime.timedelta(seconds=0.05)
|
2016-12-13 23:38:13 +01:00
|
|
|
expected_time = time.time() + interval.total_seconds()
|
2016-11-08 23:39:25 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
job_queue.run_once(self.job_datetime_tests, interval)
|
|
|
|
sleep(0.06)
|
|
|
|
assert pytest.approx(self.job_time) == expected_time
|
2016-12-13 23:38:13 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def test_time_unit_dt_datetime(self, job_queue):
|
2016-12-13 23:38:13 +01:00
|
|
|
# Testing running at a specific datetime
|
2017-08-11 23:58:41 +02:00
|
|
|
delta = datetime.timedelta(seconds=0.05)
|
|
|
|
when = datetime.datetime.now() + delta
|
2016-12-13 23:38:13 +01:00
|
|
|
expected_time = time.time() + delta.total_seconds()
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
job_queue.run_once(self.job_datetime_tests, when)
|
|
|
|
sleep(0.06)
|
|
|
|
assert pytest.approx(self.job_time) == expected_time
|
2016-12-13 23:38:13 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def test_time_unit_dt_time_today(self, job_queue):
|
2016-12-13 23:38:13 +01:00
|
|
|
# Testing running at a specific time today
|
2017-08-11 23:58:41 +02:00
|
|
|
delta = 0.05
|
|
|
|
when = (datetime.datetime.now() + datetime.timedelta(seconds=delta)).time()
|
2016-12-13 23:38:13 +01:00
|
|
|
expected_time = time.time() + delta
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
job_queue.run_once(self.job_datetime_tests, when)
|
|
|
|
sleep(0.06)
|
|
|
|
assert pytest.approx(self.job_time) == expected_time
|
2016-12-13 23:38:13 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def test_time_unit_dt_time_tomorrow(self, job_queue):
|
2016-12-13 23:38:13 +01:00
|
|
|
# Testing running at a specific time that has passed today. Since we can't wait a day, we
|
|
|
|
# test if the jobs next_t has been calculated correctly
|
|
|
|
delta = -2
|
2017-08-11 23:58:41 +02:00
|
|
|
when = (datetime.datetime.now() + datetime.timedelta(seconds=delta)).time()
|
2016-12-13 23:38:13 +01:00
|
|
|
expected_time = time.time() + delta + 60 * 60 * 24
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
job_queue.run_once(self.job_datetime_tests, when)
|
2018-01-20 14:27:01 +01:00
|
|
|
assert pytest.approx(job_queue._queue.get(False)[0]) == expected_time
|
2016-12-14 16:27:45 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def test_run_daily(self, job_queue):
|
|
|
|
delta = 0.5
|
2017-01-07 14:04:58 +01:00
|
|
|
time_of_day = (datetime.datetime.now() + datetime.timedelta(seconds=delta)).time()
|
2016-12-14 16:27:45 +01:00
|
|
|
expected_time = time.time() + 60 * 60 * 24 + delta
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
job_queue.run_daily(self.job_run_once, time_of_day)
|
|
|
|
sleep(0.6)
|
|
|
|
assert self.result == 1
|
2018-01-20 14:27:01 +01:00
|
|
|
assert pytest.approx(job_queue._queue.get(False)[0]) == expected_time
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
def test_warnings(self, job_queue):
|
|
|
|
j = Job(self.job_run_once, repeat=False)
|
|
|
|
with pytest.raises(ValueError, match='can not be set to'):
|
|
|
|
j.repeat = True
|
|
|
|
j.interval = 15
|
|
|
|
assert j.interval_seconds == 15
|
|
|
|
j.repeat = True
|
|
|
|
with pytest.raises(ValueError, match='can not be'):
|
|
|
|
j.interval = None
|
|
|
|
j.repeat = False
|
|
|
|
with pytest.raises(ValueError, match='must be of type'):
|
|
|
|
j.interval = 'every 3 minutes'
|
|
|
|
j.interval = 15
|
|
|
|
assert j.interval_seconds == 15
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match='argument should be of type'):
|
|
|
|
j.days = 'every day'
|
|
|
|
with pytest.raises(ValueError, match='The elements of the'):
|
|
|
|
j.days = ('mon', 'wed')
|
|
|
|
with pytest.raises(ValueError, match='from 0 up to and'):
|
|
|
|
j.days = (0, 6, 12, 14)
|
2018-02-19 09:36:40 +01:00
|
|
|
|
|
|
|
def test_get_jobs(self, job_queue):
|
|
|
|
job1 = job_queue.run_once(self.job_run_once, 10, name='name1')
|
|
|
|
job2 = job_queue.run_once(self.job_run_once, 10, name='name1')
|
|
|
|
job3 = job_queue.run_once(self.job_run_once, 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,)
|
2018-09-21 08:57:01 +02:00
|
|
|
|
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 0), reason='pytest fails this for no reason')
|
|
|
|
def test_bot_in_init_deprecation(self, bot):
|
|
|
|
with pytest.warns(TelegramDeprecationWarning):
|
|
|
|
JobQueue(bot)
|
|
|
|
|
|
|
|
def test_context_based_callback(self, job_queue):
|
|
|
|
job_queue.run_once(self.job_context_based_callback, 0.01, context=2)
|
|
|
|
|
|
|
|
sleep(0.03)
|
|
|
|
|
|
|
|
assert self.result == 0
|