File: test_ratelimiter.py

package info (click to toggle)
python-telegram-bot 22.3-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 11,060 kB
  • sloc: python: 90,298; makefile: 176; sh: 4
file content (434 lines) | stat: -rw-r--r-- 16,909 bytes parent folder | download
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
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2025
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# 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/].

"""
We mostly test on directly on AIORateLimiter here, b/c BaseRateLimiter doesn't contain anything
notable
"""
import asyncio
import datetime as dtm
import json
import platform
import time
from collections import Counter
from http import HTTPStatus

import pytest

from telegram import BotCommand, Chat, Message, User
from telegram.constants import ParseMode
from telegram.error import RetryAfter
from telegram.ext import AIORateLimiter, BaseRateLimiter, Defaults, ExtBot
from telegram.request import BaseRequest, RequestData
from tests.auxil.envvars import GITHUB_ACTIONS, TEST_WITH_OPT_DEPS


@pytest.mark.skipif(
    TEST_WITH_OPT_DEPS, reason="Only relevant if the optional dependency is not installed"
)
class TestNoRateLimiter:
    def test_init(self):
        with pytest.raises(RuntimeError, match=r"python-telegram-bot\[rate-limiter\]"):
            AIORateLimiter()


class TestBaseRateLimiter:
    rl_received = None
    request_received = None

    async def test_no_rate_limiter(self, bot):
        with pytest.raises(ValueError, match="if a `ExtBot.rate_limiter` is set"):
            await bot.send_message(chat_id=42, text="test", rate_limit_args="something")

    async def test_argument_passing(self, bot_info, monkeypatch, bot):
        class TestRateLimiter(BaseRateLimiter):
            async def initialize(self) -> None:
                pass

            async def shutdown(self) -> None:
                pass

            async def process_request(
                self,
                callback,
                args,
                kwargs,
                endpoint,
                data,
                rate_limit_args,
            ):
                if TestBaseRateLimiter.rl_received is None:
                    TestBaseRateLimiter.rl_received = []
                TestBaseRateLimiter.rl_received.append((endpoint, data, rate_limit_args))
                return await callback(*args, **kwargs)

        class TestRequest(BaseRequest):
            async def initialize(self) -> None:
                pass

            async def shutdown(self) -> None:
                pass

            @property
            def read_timeout(self):
                return 1

            async def do_request(self, *args, **kwargs):
                if TestBaseRateLimiter.request_received is None:
                    TestBaseRateLimiter.request_received = []
                TestBaseRateLimiter.request_received.append((args, kwargs))
                # return bot.bot.to_dict() for the `get_me` call in `Bot.initialize`
                return 200, json.dumps({"ok": True, "result": bot.bot.to_dict()}).encode()

        defaults = Defaults(parse_mode=ParseMode.HTML)
        test_request = TestRequest()
        standard_bot = ExtBot(token=bot.token, defaults=defaults, request=test_request)
        rl_bot = ExtBot(
            token=bot.token,
            defaults=defaults,
            request=test_request,
            rate_limiter=TestRateLimiter(),
        )

        async with standard_bot:
            await standard_bot.set_my_commands(
                commands=[BotCommand("test", "test")],
                language_code="en",
                api_kwargs={"api": "kwargs"},
            )
        async with rl_bot:
            await rl_bot.set_my_commands(
                commands=[BotCommand("test", "test")],
                language_code="en",
                rate_limit_args=(43, "test-1"),
                api_kwargs={"api": "kwargs"},
            )

        assert len(self.rl_received) == 2
        assert self.rl_received[0] == ("getMe", {}, None)
        assert self.rl_received[1] == (
            "setMyCommands",
            {"commands": [BotCommand("test", "test")], "language_code": "en", "api": "kwargs"},
            (43, "test-1"),
        )
        assert len(self.request_received) == 4
        # self.request_received[i] = i-th received request
        # self.request_received[i][0] = i-th received request's args
        # self.request_received[i][1] = i-th received request's kwargs
        assert self.request_received[0][1]["url"].endswith("getMe")
        assert self.request_received[2][1]["url"].endswith("getMe")
        assert self.request_received[1][0] == self.request_received[3][0]
        assert self.request_received[1][1].keys() == self.request_received[3][1].keys()
        for key, value in self.request_received[1][1].items():
            if isinstance(value, RequestData):
                assert value.parameters == self.request_received[3][1][key].parameters
                assert value.parameters["api"] == "kwargs"
            else:
                assert value == self.request_received[3][1][key]


@pytest.mark.skipif(
    not TEST_WITH_OPT_DEPS, reason="Only relevant if the optional dependency is installed"
)
@pytest.mark.skipif(
    GITHUB_ACTIONS and platform.system() == "Darwin",
    reason="The timings are apparently rather inaccurate on MacOS.",
)
@pytest.mark.flaky(10, 1)  # Timings aren't quite perfect
class TestAIORateLimiter:
    count = 0
    apb_count = 0
    call_times = []
    apb_call_times = []

    class CountRequest(BaseRequest):
        def __init__(self, retry_after=None):
            self.retry_after = retry_after

        async def initialize(self) -> None:
            pass

        async def shutdown(self) -> None:
            pass

        @property
        def read_timeout(self):
            return 1

        async def do_request(self, *args, **kwargs):
            request_data = kwargs.get("request_data")
            allow_paid_broadcast = request_data.parameters.get("allow_paid_broadcast", False)

            if allow_paid_broadcast:
                TestAIORateLimiter.apb_count += 1
                TestAIORateLimiter.apb_call_times.append(time.time())
            else:
                TestAIORateLimiter.count += 1
                TestAIORateLimiter.call_times.append(time.time())

            if self.retry_after:
                raise RetryAfter(retry_after=1)

            url = kwargs.get("url").lower()
            if url.endswith("getme"):
                return (
                    HTTPStatus.OK,
                    json.dumps(
                        {"ok": True, "result": User(id=1, first_name="bot", is_bot=True).to_dict()}
                    ).encode(),
                )
            if url.endswith("sendmessage"):
                return (
                    HTTPStatus.OK,
                    json.dumps(
                        {
                            "ok": True,
                            "result": Message(
                                message_id=1, date=dtm.datetime.now(), chat=Chat(1, "chat")
                            ).to_dict(),
                        }
                    ).encode(),
                )
            return None

    @pytest.fixture(autouse=True)
    def _reset(self):
        TestAIORateLimiter.count = 0
        TestAIORateLimiter.call_times = []
        TestAIORateLimiter.apb_count = 0
        TestAIORateLimiter.apb_call_times = []

    @pytest.mark.parametrize("max_retries", [0, 1, 4])
    async def test_max_retries(self, bot, max_retries):
        bot = ExtBot(
            token=bot.token,
            request=self.CountRequest(retry_after=1),
            rate_limiter=AIORateLimiter(
                max_retries=max_retries, overall_max_rate=0, group_max_rate=0
            ),
        )
        with pytest.raises(RetryAfter):
            await bot.get_me()

        # Check that we retried the request the correct number of times
        assert TestAIORateLimiter.count == max_retries + 1

        # Check that the retries were delayed correctly
        times = TestAIORateLimiter.call_times
        if len(times) <= 1:
            return
        delays = [j - i for i, j in zip(times[:-1], times[1:])]
        assert delays == pytest.approx([1.1 for _ in range(max_retries)], rel=0.05)

    async def test_delay_all_pending_on_retry(self, bot):
        # Makes sure that a RetryAfter blocks *all* pending requests
        bot = ExtBot(
            token=bot.token,
            request=self.CountRequest(retry_after=1),
            rate_limiter=AIORateLimiter(max_retries=1, overall_max_rate=0, group_max_rate=0),
        )
        task_1 = asyncio.create_task(bot.get_me())
        await asyncio.sleep(0.1)
        task_2 = asyncio.create_task(bot.get_me())

        assert not task_1.done()
        assert not task_2.done()

        await asyncio.sleep(1.1)
        assert isinstance(task_1.exception(), RetryAfter)
        assert not task_2.done()

        await asyncio.sleep(1.1)
        assert isinstance(task_2.exception(), RetryAfter)

    @pytest.mark.parametrize("group_id", [-1, "-1", "@username"])
    @pytest.mark.parametrize("chat_id", [1, "1"])
    async def test_basic_rate_limiting(self, bot, group_id, chat_id):
        try:
            rl_bot = ExtBot(
                token=bot.token,
                request=self.CountRequest(retry_after=None),
                rate_limiter=AIORateLimiter(
                    overall_max_rate=1,
                    overall_time_period=1 / 4,
                    group_max_rate=1,
                    group_time_period=1 / 2,
                ),
            )

            async with rl_bot:
                non_group_tasks = {}
                group_tasks = {}
                for i in range(4):
                    group_tasks[i] = asyncio.create_task(
                        rl_bot.send_message(chat_id=group_id, text="test")
                    )
                for i in range(8):
                    non_group_tasks[i] = asyncio.create_task(
                        rl_bot.send_message(chat_id=chat_id, text="test")
                    )

                await asyncio.sleep(0.85)
                # We expect 5 requests:
                # 1: `get_me` from `async with rl_bot`
                # 2: `send_message` at time 0.00
                # 3: `send_message` at time 0.25
                # 4: `send_message` at time 0.50
                # 5: `send_message` at time 0.75
                assert TestAIORateLimiter.count == 5
                assert sum(1 for task in non_group_tasks.values() if task.done()) < 8
                assert sum(1 for task in group_tasks.values() if task.done()) < 4

                # 3 seconds after start
                await asyncio.sleep(3.1 - 0.85)
                assert all(task.done() for task in non_group_tasks.values())
                assert all(task.done() for task in group_tasks.values())

        finally:
            # cleanup
            await asyncio.gather(*non_group_tasks.values(), *group_tasks.values())
            TestAIORateLimiter.count = 0
            TestAIORateLimiter.call_times = []

    async def test_rate_limiting_no_chat_id(self, bot):
        try:
            rl_bot = ExtBot(
                token=bot.token,
                request=self.CountRequest(retry_after=None),
                rate_limiter=AIORateLimiter(
                    overall_max_rate=1,
                    overall_time_period=1 / 2,
                ),
            )

            async with rl_bot:
                non_chat_tasks = {}
                chat_tasks = {}
                for i in range(4):
                    chat_tasks[i] = asyncio.create_task(
                        rl_bot.send_message(chat_id=-1, text="test")
                    )
                for i in range(8):
                    non_chat_tasks[i] = asyncio.create_task(rl_bot.get_me())

                await asyncio.sleep(0.6)
                # We expect 11 requests:
                # 1: `get_me` from `async with rl_bot`
                # 2: `send_message` at time 0.00
                # 3: `send_message` at time 0.05
                # 4: 8 times `get_me`
                assert TestAIORateLimiter.count == 11
                assert sum(1 for task in non_chat_tasks.values() if task.done()) == 8
                assert sum(1 for task in chat_tasks.values() if task.done()) == 2

                # 1.6 seconds after start
                await asyncio.sleep(1.6 - 0.6)
                assert all(task.done() for task in non_chat_tasks.values())
                assert all(task.done() for task in chat_tasks.values())
        finally:
            # cleanup
            await asyncio.gather(*non_chat_tasks.values(), *chat_tasks.values())
            TestAIORateLimiter.count = 0
            TestAIORateLimiter.call_times = []

    @pytest.mark.parametrize("intermediate", [True, False])
    async def test_group_caching(self, bot, intermediate):
        try:
            max_rate = 1000
            rl_bot = ExtBot(
                token=bot.token,
                request=self.CountRequest(retry_after=None),
                rate_limiter=AIORateLimiter(
                    overall_max_rate=max_rate,
                    overall_time_period=1,
                    group_max_rate=max_rate,
                    group_time_period=1,
                ),
            )

            # Unfortunately, there is no reliable way to test this without checking the internals
            assert len(rl_bot.rate_limiter._group_limiters) == 0
            await asyncio.gather(
                *(rl_bot.send_message(chat_id=-(i + 1), text=f"{i}") for i in range(513))
            )
            if intermediate:
                await rl_bot.send_message(chat_id=-1, text="999")
                assert 1 <= len(rl_bot.rate_limiter._group_limiters) <= 513
            else:
                await asyncio.sleep(1)
                await rl_bot.send_message(chat_id=-1, text="999")
                assert len(rl_bot.rate_limiter._group_limiters) == 1
        finally:
            TestAIORateLimiter.count = 0
            TestAIORateLimiter.call_times = []

    async def test_allow_paid_broadcast(self, bot):
        try:
            rl_bot = ExtBot(
                token=bot.token,
                request=self.CountRequest(retry_after=None),
                rate_limiter=AIORateLimiter(),
            )

            async with rl_bot:
                apb_tasks = {}
                non_apb_tasks = {}
                for i in range(3000):
                    apb_tasks[i] = asyncio.create_task(
                        rl_bot.send_message(chat_id=-1, text="test", allow_paid_broadcast=True)
                    )

                number = 2
                for i in range(number):
                    non_apb_tasks[i] = asyncio.create_task(
                        rl_bot.send_message(chat_id=-1, text="test")
                    )
                    non_apb_tasks[i + number] = asyncio.create_task(
                        rl_bot.send_message(chat_id=-1, text="test", allow_paid_broadcast=False)
                    )

                await asyncio.sleep(0.1)
                # We expect 5 non-apb requests:
                # 1: `get_me` from `async with rl_bot`
                # 2-5: `send_message`
                assert TestAIORateLimiter.count == 5
                assert sum(1 for task in non_apb_tasks.values() if task.done()) == 4

                # ~2 second after start
                # We do the checks once all apb_tasks are done as apparently getting the timings
                # right to check after 1 second is hard
                await asyncio.sleep(2.1 - 0.1)
                assert all(task.done() for task in apb_tasks.values())

                apb_call_times = [
                    ct - TestAIORateLimiter.apb_call_times[0]
                    for ct in TestAIORateLimiter.apb_call_times
                ]
                apb_call_times_dict = Counter(map(int, apb_call_times))

                # We expect ~2000 apb requests after the first second
                # 2000 (>>1000), since we have a floating window logic such that an initial
                # burst is allowed that is hard to measure in the tests
                assert apb_call_times_dict[0] <= 2000
                assert apb_call_times_dict[0] + apb_call_times_dict[1] < 3000
                assert sum(apb_call_times_dict.values()) == 3000

        finally:
            # cleanup
            await asyncio.gather(*apb_tasks.values(), *non_apb_tasks.values())