2016-01-04 01:56:22 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2024-02-19 20:06:25 +01:00
|
|
|
# Copyright (C) 2015-2024
|
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/].
|
2022-04-24 12:38:09 +02:00
|
|
|
import asyncio
|
2020-05-02 08:59:50 +02:00
|
|
|
import calendar
|
2019-11-15 21:51:22 +01:00
|
|
|
import datetime as dtm
|
2020-07-10 13:11:28 +02:00
|
|
|
import logging
|
2020-12-30 15:39:38 +01:00
|
|
|
import platform
|
2016-11-08 23:39:25 +01:00
|
|
|
import time
|
2016-01-04 01:56:22 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
import pytest
|
2022-05-05 09:27:54 +02:00
|
|
|
|
2023-10-16 20:25:25 +02:00
|
|
|
from telegram.ext import ApplicationBuilder, CallbackContext, ContextTypes, Defaults, Job, JobQueue
|
2023-03-25 19:18:04 +01:00
|
|
|
from tests.auxil.envvars import GITHUB_ACTION, TEST_WITH_OPT_DEPS
|
2023-02-22 20:19:46 +01:00
|
|
|
from tests.auxil.pytest_classes import make_bot
|
|
|
|
from tests.auxil.slots import mro_slots
|
2022-10-31 10:12:18 +01:00
|
|
|
|
|
|
|
if TEST_WITH_OPT_DEPS:
|
|
|
|
import pytz
|
|
|
|
|
|
|
|
UTC = pytz.utc
|
|
|
|
else:
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
UTC = datetime.timezone.utc
|
2021-06-06 10:37:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CustomContext(CallbackContext):
|
|
|
|
pass
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
|
2024-08-07 21:56:46 +02:00
|
|
|
@pytest.fixture
|
2023-02-11 10:45:17 +01:00
|
|
|
async def job_queue(app):
|
2018-09-21 08:57:01 +02:00
|
|
|
jq = JobQueue()
|
2022-04-24 12:38:09 +02:00
|
|
|
jq.set_application(app)
|
|
|
|
await jq.start()
|
2017-08-11 23:58:41 +02:00
|
|
|
yield jq
|
2022-04-24 12:38:09 +02:00
|
|
|
await jq.stop()
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
|
2022-10-31 10:12:18 +01:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
TEST_WITH_OPT_DEPS, reason="Only relevant if the optional dependency is not installed"
|
|
|
|
)
|
|
|
|
class TestNoJobQueue:
|
|
|
|
def test_init_job_queue(self):
|
|
|
|
with pytest.raises(RuntimeError, match=r"python-telegram-bot\[job-queue\]"):
|
|
|
|
JobQueue()
|
|
|
|
|
|
|
|
def test_init_job(self):
|
|
|
|
with pytest.raises(RuntimeError, match=r"python-telegram-bot\[job-queue\]"):
|
|
|
|
Job(None)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
not TEST_WITH_OPT_DEPS, reason="Only relevant if the optional dependency is installed"
|
|
|
|
)
|
2020-10-09 17:22:07 +02:00
|
|
|
@pytest.mark.skipif(
|
2023-03-25 19:18:04 +01:00
|
|
|
bool(GITHUB_ACTION and platform.system() in ["Windows", "Darwin"]),
|
2020-12-30 15:39:38 +01:00
|
|
|
reason="On Windows & MacOS precise timings are not accurate.",
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2022-11-09 20:43:27 +01:00
|
|
|
@pytest.mark.flaky(10, 1) # Timings aren't quite perfect
|
2020-06-15 18:20:51 +02:00
|
|
|
class TestJobQueue:
|
2017-08-11 23:58:41 +02:00
|
|
|
result = 0
|
|
|
|
job_time = 0
|
2020-07-10 13:11:28 +02:00
|
|
|
received_error = None
|
2016-01-04 01:56:22 +01:00
|
|
|
|
2023-09-15 21:35:45 +02:00
|
|
|
async def test_repr(self, app):
|
|
|
|
jq = JobQueue()
|
|
|
|
jq.set_application(app)
|
|
|
|
assert repr(jq) == f"JobQueue[application={app!r}]"
|
|
|
|
|
|
|
|
when = dtm.datetime.utcnow() + dtm.timedelta(days=1)
|
|
|
|
callback = self.job_run_once
|
|
|
|
job = jq.run_once(callback, when, name="name2")
|
|
|
|
assert repr(job) == (
|
|
|
|
f"Job[id={job.job.id}, name={job.name}, callback=job_run_once, "
|
|
|
|
f"trigger=date["
|
|
|
|
f"{when.strftime('%Y-%m-%d %H:%M:%S UTC')}"
|
|
|
|
f"]]"
|
|
|
|
)
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
2023-03-25 19:18:04 +01:00
|
|
|
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
|
2020-07-10 13:11:28 +02:00
|
|
|
self.received_error = None
|
2016-01-04 01:56:22 +01:00
|
|
|
|
2023-10-16 20:25:25 +02:00
|
|
|
def test_scheduler_configuration(self, job_queue, timezone, bot):
|
|
|
|
# Unfortunately, we can't really test the executor setting explicitly without relying
|
|
|
|
# on protected attributes. However, this should be tested enough implicitly via all the
|
|
|
|
# other tests in here
|
|
|
|
assert job_queue.scheduler_configuration["timezone"] is UTC
|
|
|
|
|
|
|
|
tz_app = ApplicationBuilder().defaults(Defaults(tzinfo=timezone)).token(bot.token).build()
|
|
|
|
assert tz_app.job_queue.scheduler_configuration["timezone"] is timezone
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def job_run_once(self, context):
|
|
|
|
if (
|
|
|
|
isinstance(context, CallbackContext)
|
|
|
|
and isinstance(context.job, Job)
|
|
|
|
and isinstance(context.update_queue, asyncio.Queue)
|
2022-05-12 19:26:03 +02:00
|
|
|
and context.job.data is None
|
2022-04-24 12:38:09 +02:00
|
|
|
and context.chat_data is None
|
|
|
|
and context.user_data is None
|
|
|
|
and isinstance(context.bot_data, dict)
|
|
|
|
):
|
|
|
|
self.result += 1
|
2016-01-04 01:56:22 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def job_with_exception(self, context):
|
2022-05-05 17:40:22 +02:00
|
|
|
raise Exception("Test Error")
|
2016-01-04 02:36:15 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def job_remove_self(self, context):
|
2016-05-25 22:51:13 +02:00
|
|
|
self.result += 1
|
2021-08-29 18:15:04 +02:00
|
|
|
context.job.schedule_removal()
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2022-05-12 19:26:03 +02:00
|
|
|
async def job_run_once_with_data(self, context):
|
|
|
|
self.result += context.job.data
|
2016-05-26 14:07:44 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def job_datetime_tests(self, context):
|
2016-12-13 23:38:13 +01:00
|
|
|
self.job_time = time.time()
|
2016-11-08 23:39:25 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def error_handler_context(self, update, context):
|
2022-07-11 07:54:03 +02:00
|
|
|
self.received_error = (
|
|
|
|
str(context.error),
|
|
|
|
context.job,
|
|
|
|
context.user_data,
|
|
|
|
context.chat_data,
|
|
|
|
)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def error_handler_raise_error(self, *args):
|
2022-05-05 17:40:22 +02:00
|
|
|
raise Exception("Failing bigly")
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2023-02-22 20:19:46 +01:00
|
|
|
def test_slot_behaviour(self, job_queue):
|
2021-10-09 13:56:50 +02:00
|
|
|
for attr in job_queue.__slots__:
|
2022-05-05 17:40:22 +02:00
|
|
|
assert getattr(job_queue, attr, "err") != "err", f"got extra slot '{attr}'"
|
2021-10-09 13:56:50 +02:00
|
|
|
assert len(mro_slots(job_queue)) == len(set(mro_slots(job_queue))), "duplicate slot"
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
def test_application_weakref(self, bot):
|
2021-10-09 13:56:50 +02:00
|
|
|
jq = JobQueue()
|
2022-04-24 12:38:09 +02:00
|
|
|
application = ApplicationBuilder().token(bot.token).job_queue(None).build()
|
2022-05-05 17:40:22 +02:00
|
|
|
with pytest.raises(RuntimeError, match="No application was set"):
|
2022-04-24 12:38:09 +02:00
|
|
|
jq.application
|
|
|
|
jq.set_application(application)
|
|
|
|
assert jq.application is application
|
|
|
|
del application
|
2022-05-05 17:40:22 +02:00
|
|
|
with pytest.raises(RuntimeError, match="no longer alive"):
|
2022-04-24 12:38:09 +02:00
|
|
|
jq.application
|
2021-10-09 13:56:50 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_run_once(self, job_queue):
|
|
|
|
job_queue.run_once(self.job_run_once, 0.1)
|
|
|
|
await asyncio.sleep(0.2)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 1
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_run_once_timezone(self, job_queue, timezone):
|
2020-07-10 13:11:28 +02:00
|
|
|
"""Test the correct handling of aware datetimes"""
|
2019-11-15 21:51:22 +01:00
|
|
|
# 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
|
2020-07-10 13:11:28 +02:00
|
|
|
when = dtm.datetime.now(timezone)
|
2019-11-15 21:51:22 +01:00
|
|
|
job_queue.run_once(self.job_run_once, when)
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.1)
|
2019-11-15 21:51:22 +01:00
|
|
|
assert self.result == 1
|
|
|
|
|
2022-05-12 19:26:03 +02:00
|
|
|
async def test_job_with_data(self, job_queue):
|
|
|
|
job_queue.run_once(self.job_run_once_with_data, 0.1, data=5)
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.2)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 5
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_run_repeating(self, job_queue):
|
|
|
|
job_queue.run_repeating(self.job_run_once, 0.1)
|
|
|
|
await asyncio.sleep(0.25)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 2
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_run_repeating_first(self, job_queue):
|
|
|
|
job_queue.run_repeating(self.job_run_once, 0.5, first=0.2)
|
|
|
|
await asyncio.sleep(0.15)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 0
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.1)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 1
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_run_repeating_first_timezone(self, job_queue, timezone):
|
2020-07-10 13:11:28 +02:00
|
|
|
"""Test correct scheduling of job when passing a timezone-aware datetime as ``first``"""
|
2020-10-09 17:22:07 +02:00
|
|
|
job_queue.run_repeating(
|
2022-04-24 12:38:09 +02:00
|
|
|
self.job_run_once, 0.5, first=dtm.datetime.now(timezone) + dtm.timedelta(seconds=0.2)
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2023-02-11 10:45:17 +01:00
|
|
|
await asyncio.sleep(0.05)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert self.result == 0
|
2023-02-11 10:45:17 +01:00
|
|
|
await asyncio.sleep(0.25)
|
2020-01-26 22:07:17 +01:00
|
|
|
assert self.result == 1
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_run_repeating_last(self, job_queue):
|
|
|
|
job_queue.run_repeating(self.job_run_once, 0.25, last=0.4)
|
|
|
|
await asyncio.sleep(0.3)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert self.result == 1
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.4)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert self.result == 1
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_run_repeating_last_timezone(self, job_queue, timezone):
|
2023-02-11 10:45:17 +01:00
|
|
|
"""Test correct scheduling of job when passing a timezone-aware datetime as ``last``"""
|
2020-10-09 17:22:07 +02:00
|
|
|
job_queue.run_repeating(
|
2022-04-24 12:38:09 +02:00
|
|
|
self.job_run_once, 0.25, last=dtm.datetime.now(timezone) + dtm.timedelta(seconds=0.4)
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.3)
|
2020-10-06 19:28:40 +02:00
|
|
|
assert self.result == 1
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.4)
|
2020-10-06 19:28:40 +02:00
|
|
|
assert self.result == 1
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_run_repeating_last_before_first(self, job_queue):
|
2020-07-10 13:11:28 +02:00
|
|
|
with pytest.raises(ValueError, match="'last' must not be before 'first'!"):
|
2022-04-24 12:38:09 +02:00
|
|
|
job_queue.run_repeating(self.job_run_once, 0.5, first=1, last=0.5)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_run_repeating_timedelta(self, job_queue):
|
|
|
|
job_queue.run_repeating(self.job_run_once, dtm.timedelta(seconds=0.1))
|
|
|
|
await asyncio.sleep(0.25)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert self.result == 2
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_run_custom(self, job_queue):
|
2022-05-05 17:40:22 +02:00
|
|
|
job_queue.run_custom(self.job_run_once, {"trigger": "interval", "seconds": 0.2})
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.5)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert self.result == 2
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_multiple(self, job_queue):
|
|
|
|
job_queue.run_once(self.job_run_once, 0.1)
|
|
|
|
job_queue.run_once(self.job_run_once, 0.2)
|
|
|
|
job_queue.run_repeating(self.job_run_once, 0.2)
|
|
|
|
await asyncio.sleep(0.55)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 4
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_disabled(self, job_queue):
|
2017-08-11 23:58:41 +02:00
|
|
|
j1 = job_queue.run_once(self.job_run_once, 0.1)
|
2022-04-24 12:38:09 +02:00
|
|
|
j2 = job_queue.run_repeating(self.job_run_once, 0.5)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
j1.enabled = False
|
2017-08-11 23:58:41 +02:00
|
|
|
j2.enabled = False
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.6)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.6)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 1
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_schedule_removal(self, job_queue):
|
|
|
|
j1 = job_queue.run_once(self.job_run_once, 0.3)
|
|
|
|
j2 = job_queue.run_repeating(self.job_run_once, 0.2)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.25)
|
2023-02-11 10:45:17 +01:00
|
|
|
assert self.result == 1
|
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
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.4)
|
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
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_schedule_removal_from_within(self, job_queue):
|
|
|
|
job_queue.run_repeating(self.job_remove_self, 0.1)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.5)
|
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
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_longer_first(self, job_queue):
|
|
|
|
job_queue.run_once(self.job_run_once, 0.2)
|
|
|
|
job_queue.run_once(self.job_run_once, 0.1)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.15)
|
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
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_error(self, job_queue):
|
|
|
|
job_queue.run_repeating(self.job_with_exception, 0.1)
|
|
|
|
job_queue.run_repeating(self.job_run_once, 0.2)
|
|
|
|
await asyncio.sleep(0.3)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 1
|
|
|
|
|
2023-02-11 10:45:17 +01:00
|
|
|
async def test_in_application(self, bot_info):
|
|
|
|
app = ApplicationBuilder().bot(make_bot(bot_info)).build()
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
assert not app.job_queue.scheduler.running
|
|
|
|
await app.start()
|
|
|
|
assert app.job_queue.scheduler.running
|
|
|
|
|
|
|
|
app.job_queue.run_repeating(self.job_run_once, 0.2)
|
|
|
|
await asyncio.sleep(0.3)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 1
|
2022-04-24 12:38:09 +02:00
|
|
|
await app.stop()
|
|
|
|
assert not app.job_queue.scheduler.running
|
|
|
|
await asyncio.sleep(1)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 1
|
2016-01-04 11:08:43 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_time_unit_int(self, job_queue):
|
2016-11-08 23:39:25 +01:00
|
|
|
# Testing seconds in int
|
2022-04-24 12:38:09 +02:00
|
|
|
delta = 0.5
|
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)
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.6)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert pytest.approx(self.job_time) == expected_time
|
2016-11-08 23:39:25 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async 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.
|
2022-04-24 12:38:09 +02:00
|
|
|
interval = dtm.timedelta(seconds=0.5)
|
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)
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.6)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert pytest.approx(self.job_time) == expected_time
|
2016-12-13 23:38:13 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_time_unit_dt_datetime(self, job_queue):
|
2016-12-13 23:38:13 +01:00
|
|
|
# Testing running at a specific datetime
|
2022-10-31 10:12:18 +01:00
|
|
|
delta, now = dtm.timedelta(seconds=0.5), dtm.datetime.now(UTC)
|
2020-07-10 13:11:28 +02:00
|
|
|
when = now + delta
|
2023-02-11 10:45:17 +01:00
|
|
|
expected_time = when.timestamp()
|
2016-12-13 23:38:13 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
job_queue.run_once(self.job_datetime_tests, when)
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.6)
|
2019-11-15 21:51:22 +01:00
|
|
|
assert self.job_time == pytest.approx(expected_time)
|
2016-12-13 23:38:13 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async 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
|
2022-10-31 10:12:18 +01:00
|
|
|
delta, now = 0.5, dtm.datetime.now(UTC)
|
2020-07-10 13:11:28 +02:00
|
|
|
expected_time = now + dtm.timedelta(seconds=delta)
|
|
|
|
when = expected_time.time()
|
|
|
|
expected_time = expected_time.timestamp()
|
2016-12-13 23:38:13 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
job_queue.run_once(self.job_datetime_tests, when)
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.6)
|
2019-11-15 21:51:22 +01:00
|
|
|
assert self.job_time == pytest.approx(expected_time)
|
2016-12-13 23:38:13 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async 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
|
2019-11-15 21:51:22 +01:00
|
|
|
# test if the job's next scheduled execution time has been calculated correctly
|
2022-10-31 10:12:18 +01:00
|
|
|
delta, now = -2, dtm.datetime.now(UTC)
|
2020-07-10 13:11:28 +02:00
|
|
|
when = (now + dtm.timedelta(seconds=delta)).time()
|
|
|
|
expected_time = (now + dtm.timedelta(seconds=delta, days=1)).timestamp()
|
2016-12-13 23:38:13 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
job_queue.run_once(self.job_datetime_tests, when)
|
2020-07-10 13:11:28 +02:00
|
|
|
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
|
|
|
|
assert scheduled_time == pytest.approx(expected_time)
|
2016-12-14 16:27:45 +01:00
|
|
|
|
2023-01-14 18:57:08 +01:00
|
|
|
async def test_run_daily(self, job_queue):
|
2022-10-31 10:12:18 +01:00
|
|
|
delta, now = 1, dtm.datetime.now(UTC)
|
2020-07-10 13:11:28 +02:00
|
|
|
time_of_day = (now + dtm.timedelta(seconds=delta)).time()
|
|
|
|
expected_reschedule_time = (now + dtm.timedelta(seconds=delta, days=1)).timestamp()
|
2016-12-14 16:27:45 +01:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
job_queue.run_daily(self.job_run_once, time_of_day)
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(delta + 0.1)
|
2017-08-11 23:58:41 +02:00
|
|
|
assert self.result == 1
|
2020-07-10 13:11:28 +02:00
|
|
|
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
|
|
|
|
assert scheduled_time == pytest.approx(expected_reschedule_time)
|
2023-01-14 18:57:08 +01:00
|
|
|
|
2023-03-25 19:18:04 +01:00
|
|
|
@pytest.mark.parametrize("weekday", [0, 1, 2, 3, 4, 5, 6])
|
2024-04-14 14:14:45 +02:00
|
|
|
async def test_run_daily_days_of_week(self, job_queue, weekday):
|
2022-10-31 10:12:18 +01:00
|
|
|
delta, now = 1, dtm.datetime.now(UTC)
|
2022-05-25 10:02:00 +02:00
|
|
|
time_of_day = (now + dtm.timedelta(seconds=delta)).time()
|
|
|
|
# offset in days until next weekday
|
|
|
|
offset = (weekday + 6 - now.weekday()) % 7
|
|
|
|
offset = offset if offset > 0 else 7
|
|
|
|
expected_reschedule_time = (now + dtm.timedelta(seconds=delta, days=offset)).timestamp()
|
|
|
|
|
|
|
|
job_queue.run_daily(self.job_run_once, time_of_day, days=[weekday])
|
|
|
|
await asyncio.sleep(delta + 0.1)
|
|
|
|
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
|
|
|
|
assert scheduled_time == pytest.approx(expected_reschedule_time)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_run_monthly(self, job_queue, timezone):
|
2020-07-10 13:11:28 +02:00
|
|
|
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)
|
2020-05-02 08:59:50 +02:00
|
|
|
|
2020-07-10 13:11:28 +02:00
|
|
|
day = now.day
|
2020-10-31 16:33:34 +01:00
|
|
|
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)
|
2021-01-07 21:31:00 +01:00
|
|
|
if day > next_months_days:
|
2020-10-31 16:33:34 +01:00
|
|
|
expected_reschedule_time += dtm.timedelta(next_months_days)
|
|
|
|
|
|
|
|
expected_reschedule_time = timezone.normalize(expected_reschedule_time)
|
2020-09-27 12:59:48 +02:00
|
|
|
# Adjust the hour for the special case that between now and next month a DST switch happens
|
|
|
|
expected_reschedule_time += dtm.timedelta(
|
2020-10-09 17:22:07 +02:00
|
|
|
hours=time_of_day.hour - expected_reschedule_time.hour
|
|
|
|
)
|
2020-07-10 13:11:28 +02:00
|
|
|
expected_reschedule_time = expected_reschedule_time.timestamp()
|
2020-05-02 08:59:50 +02:00
|
|
|
|
|
|
|
job_queue.run_monthly(self.job_run_once, time_of_day, day)
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(delta + 0.1)
|
2020-05-02 08:59:50 +02:00
|
|
|
assert self.result == 1
|
2020-07-10 13:11:28 +02:00
|
|
|
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
|
2021-11-20 11:36:18 +01:00
|
|
|
assert scheduled_time == pytest.approx(expected_reschedule_time, rel=1e-3)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_run_monthly_non_strict_day(self, job_queue, timezone):
|
2020-07-10 13:11:28 +02:00
|
|
|
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)
|
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
expected_reschedule_time += dtm.timedelta(
|
|
|
|
calendar.monthrange(now.year, now.month)[1]
|
|
|
|
) - dtm.timedelta(days=now.day)
|
2020-07-10 13:11:28 +02:00
|
|
|
# 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(
|
2020-10-09 17:22:07 +02:00
|
|
|
hours=time_of_day.hour - expected_reschedule_time.hour
|
|
|
|
)
|
2020-07-10 13:11:28 +02:00
|
|
|
expected_reschedule_time = expected_reschedule_time.timestamp()
|
2020-05-02 08:59:50 +02:00
|
|
|
|
2021-08-26 20:59:23 +02:00
|
|
|
job_queue.run_monthly(self.job_run_once, time_of_day, -1)
|
2020-07-10 13:11:28 +02:00
|
|
|
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
|
|
|
|
assert scheduled_time == pytest.approx(expected_reschedule_time)
|
2020-05-02 08:59:50 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_default_tzinfo(self, tz_bot):
|
2020-09-27 12:59:48 +02:00
|
|
|
# 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
|
2022-04-24 12:38:09 +02:00
|
|
|
app = ApplicationBuilder().bot(tz_bot).build()
|
|
|
|
jq = app.job_queue
|
|
|
|
await jq.start()
|
2020-09-27 12:59:48 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
when = dtm.datetime.now(tz_bot.defaults.tzinfo) + dtm.timedelta(seconds=0.1)
|
|
|
|
jq.run_once(self.job_run_once, when.time())
|
|
|
|
await asyncio.sleep(0.15)
|
|
|
|
assert self.result == 1
|
|
|
|
|
|
|
|
await jq.stop()
|
2020-09-27 12:59:48 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_get_jobs(self, job_queue):
|
|
|
|
callback = self.job_run_once
|
2020-05-02 08:59:50 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
job1 = job_queue.run_once(callback, 10, name="name1")
|
2023-02-11 10:45:17 +01:00
|
|
|
await asyncio.sleep(0.03) # To stablize tests on windows
|
2022-05-05 17:40:22 +02:00
|
|
|
job2 = job_queue.run_once(callback, 10, name="name1")
|
2023-02-11 10:45:17 +01:00
|
|
|
await asyncio.sleep(0.03)
|
2022-05-05 17:40:22 +02:00
|
|
|
job3 = job_queue.run_once(callback, 10, name="name2")
|
2023-02-11 10:45:17 +01:00
|
|
|
await asyncio.sleep(0.03)
|
2018-02-19 09:36:40 +01:00
|
|
|
|
|
|
|
assert job_queue.jobs() == (job1, job2, job3)
|
2022-05-05 17:40:22 +02:00
|
|
|
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
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_job_run(self, app):
|
|
|
|
job = app.job_queue.run_repeating(self.job_run_once, 0.02)
|
2023-02-11 10:45:17 +01:00
|
|
|
await asyncio.sleep(0.05) # the job queue has not started yet
|
|
|
|
assert self.result == 0 # so the job will not run
|
|
|
|
await job.run(app) # but this will force it to run
|
2020-07-10 13:11:28 +02:00
|
|
|
assert self.result == 1
|
2020-03-30 18:10:27 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_enable_disable_job(self, job_queue):
|
|
|
|
job = job_queue.run_repeating(self.job_run_once, 0.2)
|
|
|
|
await asyncio.sleep(0.5)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert self.result == 2
|
|
|
|
job.enabled = False
|
|
|
|
assert not job.enabled
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.5)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert self.result == 2
|
|
|
|
job.enabled = True
|
|
|
|
assert job.enabled
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.5)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert self.result == 4
|
2020-04-18 15:08:16 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_remove_job(self, job_queue):
|
|
|
|
job = job_queue.run_repeating(self.job_run_once, 0.2)
|
|
|
|
await asyncio.sleep(0.5)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert self.result == 2
|
|
|
|
assert not job.removed
|
|
|
|
job.schedule_removal()
|
|
|
|
assert job.removed
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.5)
|
2020-04-18 15:08:16 +02:00
|
|
|
assert self.result == 2
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2022-09-28 21:33:15 +02:00
|
|
|
async def test_equality(self, job_queue):
|
2022-04-24 12:38:09 +02:00
|
|
|
job = job_queue.run_repeating(self.job_run_once, 0.2)
|
2022-09-28 21:33:15 +02:00
|
|
|
job_2 = job_queue.run_repeating(self.job_run_once, 0.2)
|
|
|
|
job_3 = Job(self.job_run_once, 0.2)
|
|
|
|
job_3._job = job.job
|
2023-06-29 11:38:09 +02:00
|
|
|
assert job == job # noqa: PLR0124
|
2022-09-28 21:33:15 +02:00
|
|
|
assert job != job_queue
|
|
|
|
assert job != job_2
|
|
|
|
assert job == job_3
|
|
|
|
|
|
|
|
assert hash(job) == hash(job)
|
|
|
|
assert hash(job) != hash(job_queue)
|
|
|
|
assert hash(job) != hash(job_2)
|
|
|
|
assert hash(job) == hash(job_3)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_process_error_context(self, job_queue, app):
|
|
|
|
app.add_error_handler(self.error_handler_context)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2022-07-11 07:54:03 +02:00
|
|
|
job = job_queue.run_once(self.job_with_exception, 0.1, chat_id=42, user_id=43)
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.15)
|
2022-05-05 17:40:22 +02:00
|
|
|
assert self.received_error[0] == "Test Error"
|
2021-10-03 20:00:54 +02:00
|
|
|
assert self.received_error[1] is job
|
2020-07-10 13:11:28 +02:00
|
|
|
self.received_error = None
|
2022-04-24 12:38:09 +02:00
|
|
|
await job.run(app)
|
2022-05-05 17:40:22 +02:00
|
|
|
assert self.received_error[0] == "Test Error"
|
2021-10-03 20:00:54 +02:00
|
|
|
assert self.received_error[1] is job
|
2022-07-11 07:54:03 +02:00
|
|
|
assert self.received_error[2] is app.user_data[43]
|
|
|
|
assert self.received_error[3] is app.chat_data[42]
|
2020-07-10 13:11:28 +02:00
|
|
|
|
|
|
|
# Remove handler
|
2022-04-24 12:38:09 +02:00
|
|
|
app.remove_error_handler(self.error_handler_context)
|
2020-07-10 13:11:28 +02:00
|
|
|
self.received_error = None
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
job = job_queue.run_once(self.job_with_exception, 0.1)
|
|
|
|
await asyncio.sleep(0.15)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert self.received_error is None
|
2022-04-24 12:38:09 +02:00
|
|
|
await job.run(app)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert self.received_error is None
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_process_error_that_raises_errors(self, job_queue, app, caplog):
|
|
|
|
app.add_error_handler(self.error_handler_raise_error)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
|
|
|
with caplog.at_level(logging.ERROR):
|
2022-04-24 12:38:09 +02:00
|
|
|
job = job_queue.run_once(self.job_with_exception, 0.1)
|
|
|
|
await asyncio.sleep(0.15)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert len(caplog.records) == 1
|
|
|
|
rec = caplog.records[-1]
|
2022-05-05 17:40:22 +02:00
|
|
|
assert "An error was raised and an uncaught" in rec.getMessage()
|
2020-07-10 13:11:28 +02:00
|
|
|
caplog.clear()
|
|
|
|
|
|
|
|
with caplog.at_level(logging.ERROR):
|
2022-04-24 12:38:09 +02:00
|
|
|
await job.run(app)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert len(caplog.records) == 1
|
|
|
|
rec = caplog.records[-1]
|
2022-05-05 17:40:22 +02:00
|
|
|
assert "uncaught error was raised while handling" in rec.getMessage()
|
2020-07-10 13:11:28 +02:00
|
|
|
caplog.clear()
|
|
|
|
|
|
|
|
# Remove handler
|
2022-04-24 12:38:09 +02:00
|
|
|
app.remove_error_handler(self.error_handler_raise_error)
|
2020-07-10 13:11:28 +02:00
|
|
|
self.received_error = None
|
|
|
|
|
|
|
|
with caplog.at_level(logging.ERROR):
|
2022-04-24 12:38:09 +02:00
|
|
|
job = job_queue.run_once(self.job_with_exception, 0.1)
|
|
|
|
await asyncio.sleep(0.15)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert len(caplog.records) == 1
|
|
|
|
rec = caplog.records[-1]
|
2022-05-05 17:40:22 +02:00
|
|
|
assert "No error handlers are registered" in rec.getMessage()
|
2020-07-10 13:11:28 +02:00
|
|
|
caplog.clear()
|
|
|
|
|
|
|
|
with caplog.at_level(logging.ERROR):
|
2022-04-24 12:38:09 +02:00
|
|
|
await job.run(app)
|
2020-07-10 13:11:28 +02:00
|
|
|
assert len(caplog.records) == 1
|
|
|
|
rec = caplog.records[-1]
|
2023-04-10 17:01:35 +02:00
|
|
|
assert rec.name == "telegram.ext.Application"
|
2022-05-05 17:40:22 +02:00
|
|
|
assert "No error handlers are registered" in rec.getMessage()
|
2021-06-06 10:37:53 +02:00
|
|
|
|
2023-10-16 20:25:25 +02:00
|
|
|
async def test_custom_context(self, bot):
|
2022-04-24 12:38:09 +02:00
|
|
|
application = (
|
|
|
|
ApplicationBuilder()
|
|
|
|
.token(bot.token)
|
2021-10-09 13:56:50 +02:00
|
|
|
.context_types(
|
|
|
|
ContextTypes(
|
|
|
|
context=CustomContext, bot_data=int, user_data=float, chat_data=complex
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.build()
|
2021-06-06 10:37:53 +02:00
|
|
|
)
|
2023-10-16 20:25:25 +02:00
|
|
|
job_queue = JobQueue()
|
2022-04-24 12:38:09 +02:00
|
|
|
job_queue.set_application(application)
|
2021-06-06 10:37:53 +02:00
|
|
|
|
2023-02-11 10:45:17 +01:00
|
|
|
async def callback(context):
|
2021-06-06 10:37:53 +02:00
|
|
|
self.result = (
|
|
|
|
type(context),
|
|
|
|
context.user_data,
|
|
|
|
context.chat_data,
|
|
|
|
type(context.bot_data),
|
|
|
|
)
|
|
|
|
|
2023-10-16 20:25:25 +02:00
|
|
|
await job_queue.start()
|
2021-06-06 10:37:53 +02:00
|
|
|
job_queue.run_once(callback, 0.1)
|
2022-04-24 12:38:09 +02:00
|
|
|
await asyncio.sleep(0.15)
|
2021-06-06 10:37:53 +02:00
|
|
|
assert self.result == (CustomContext, None, None, int)
|
2023-10-16 20:25:25 +02:00
|
|
|
await job_queue.stop()
|
2022-01-03 09:07:18 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_attribute_error(self):
|
2022-01-03 09:07:18 +01:00
|
|
|
job = Job(self.job_run_once)
|
|
|
|
with pytest.raises(
|
|
|
|
AttributeError, match="nor 'apscheduler.job.Job' has attribute 'error'"
|
|
|
|
):
|
|
|
|
job.error
|
2022-04-24 12:38:09 +02:00
|
|
|
|
2023-03-25 19:18:04 +01:00
|
|
|
@pytest.mark.parametrize("wait", [True, False])
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_wait_on_shut_down(self, job_queue, wait):
|
|
|
|
ready_event = asyncio.Event()
|
|
|
|
|
|
|
|
async def callback(_):
|
|
|
|
await ready_event.wait()
|
|
|
|
|
|
|
|
await job_queue.start()
|
|
|
|
job_queue.run_once(callback, when=0.1)
|
|
|
|
await asyncio.sleep(0.15)
|
|
|
|
|
|
|
|
task = asyncio.create_task(job_queue.stop(wait=wait))
|
|
|
|
if wait:
|
|
|
|
assert not task.done()
|
|
|
|
ready_event.set()
|
2023-02-11 10:45:17 +01:00
|
|
|
await asyncio.sleep(0.1) # no CancelledError (see source of JobQueue.stop for details)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert task.done()
|
|
|
|
else:
|
2023-02-11 10:45:17 +01:00
|
|
|
await asyncio.sleep(0.1) # unfortunately we will get a CancelledError here
|
2022-04-24 12:38:09 +02:00
|
|
|
assert task.done()
|
2023-06-02 22:17:46 +02:00
|
|
|
|
|
|
|
async def test_from_aps_job(self, job_queue):
|
|
|
|
job = job_queue.run_once(self.job_run_once, 0.1, name="test_job")
|
|
|
|
aps_job = job_queue.scheduler.get_job(job.id)
|
|
|
|
|
|
|
|
tg_job = Job.from_aps_job(aps_job)
|
|
|
|
assert tg_job is job
|
|
|
|
assert tg_job.job is aps_job
|
|
|
|
|
|
|
|
async def test_from_aps_job_missing_reference(self, job_queue):
|
|
|
|
"""We manually create a ext.Job and an aps job such that the former has no reference to the
|
|
|
|
latter. Then we test that Job.from_aps_job() still sets the reference correctly.
|
|
|
|
"""
|
|
|
|
job = Job(self.job_run_once)
|
|
|
|
aps_job = job_queue.scheduler.add_job(
|
|
|
|
func=job_queue.job_callback,
|
|
|
|
args=(job_queue, job),
|
|
|
|
trigger="interval",
|
|
|
|
seconds=2,
|
|
|
|
id="test_id",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert job.job is None
|
|
|
|
tg_job = Job.from_aps_job(aps_job)
|
|
|
|
assert tg_job is job
|
|
|
|
assert tg_job.job is aps_job
|
2024-05-20 15:25:56 +02:00
|
|
|
|
|
|
|
async def test_run_job_exception_in_building_context(
|
|
|
|
self, monkeypatch, job_queue, caplog, app
|
|
|
|
):
|
|
|
|
# Makes sure that exceptions in building the context don't stop the application
|
|
|
|
exception = ValueError("TestException")
|
|
|
|
original_from_job = CallbackContext.from_job
|
|
|
|
|
|
|
|
def raise_exception(job, application):
|
|
|
|
if job.data == 1:
|
|
|
|
raise exception
|
|
|
|
return original_from_job(job, application)
|
|
|
|
|
|
|
|
monkeypatch.setattr(CallbackContext, "from_job", raise_exception)
|
|
|
|
|
|
|
|
received_jobs = set()
|
|
|
|
|
|
|
|
async def job_callback(context):
|
|
|
|
received_jobs.add(context.job.data)
|
|
|
|
|
|
|
|
with caplog.at_level(logging.CRITICAL):
|
|
|
|
job_queue.run_once(job_callback, 0.1, data=1)
|
|
|
|
await asyncio.sleep(0.2)
|
|
|
|
|
|
|
|
assert received_jobs == set()
|
|
|
|
assert len(caplog.records) == 1
|
|
|
|
record = caplog.records[0]
|
|
|
|
assert record.name == "telegram.ext.JobQueue"
|
|
|
|
assert record.getMessage().startswith(
|
|
|
|
"Error while building CallbackContext for job job_callback"
|
|
|
|
)
|
|
|
|
assert record.levelno == logging.CRITICAL
|
|
|
|
|
|
|
|
# Let's also check that no critical log is produced when the exception is not raised
|
|
|
|
caplog.clear()
|
|
|
|
with caplog.at_level(logging.CRITICAL):
|
|
|
|
job_queue.run_once(job_callback, 0.1, data=2)
|
|
|
|
await asyncio.sleep(0.2)
|
|
|
|
|
|
|
|
assert received_jobs == {2}
|
|
|
|
assert len(caplog.records) == 0
|