From 5da1dd7ce90b6ceb959bd4550ccfd4e672733317 Mon Sep 17 00:00:00 2001 From: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com> Date: Sun, 6 Jun 2021 12:57:45 +0530 Subject: [PATCH] Add max_connections Parameter to Updater.start_webhook (#2547) * Include max_connections args * Update docs & add test Co-authored-by: Hinrich Mahler --- telegram/ext/updater.py | 11 +++++++++++ tests/test_updater.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 551bafe6d..e9975d86f 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -363,6 +363,7 @@ class Updater: force_event_loop: bool = None, drop_pending_updates: bool = None, ip_address: str = None, + max_connections: int = 40, ) -> Optional[Queue]: """ Starts a small http server to listen for updates via webhook. If :attr:`cert` @@ -411,6 +412,11 @@ class Updater: Since version 13.6, ``tornade>=6.1`` is required, which resolves the former issue. + max_connections (:obj:`int`, optional): Passed to + :meth:`telegram.Bot.set_webhook`. + + .. versionadded:: 13.6 + Returns: :obj:`Queue`: The update queue that can be filled from the main thread. @@ -459,6 +465,7 @@ class Updater: allowed_updates, ready=webhook_ready, ip_address=ip_address, + max_connections=max_connections, ) self.logger.debug('Waiting for Dispatcher and Webhook to start') @@ -592,6 +599,7 @@ class Updater: allowed_updates, ready=None, ip_address=None, + max_connections: int = 40, ): self.logger.debug('Updater thread started (webhook)') @@ -632,6 +640,7 @@ class Updater: allowed_updates=allowed_updates, cert=cert_file, ip_address=ip_address, + max_connections=max_connections, ) if cert_file is not None: cert_file.close() @@ -652,6 +661,7 @@ class Updater: cert=None, bootstrap_interval=5, ip_address=None, + max_connections: int = 40, ): retries = [0] @@ -672,6 +682,7 @@ class Updater: allowed_updates=allowed_updates, ip_address=ip_address, drop_pending_updates=drop_pending_updates, + max_connections=max_connections, ) return False diff --git a/tests/test_updater.py b/tests/test_updater.py index 862004d18..16c3c611a 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -319,6 +319,42 @@ class TestUpdater: updater.stop() assert self.test_flag == [True, True] + @pytest.mark.parametrize('pass_max_connections', [True, False]) + def test_webhook_max_connections(self, monkeypatch, updater, pass_max_connections): + q = Queue() + max_connections = 42 + + def set_webhook(**kwargs): + print(kwargs) + self.test_flag = kwargs.get('max_connections') == ( + max_connections if pass_max_connections else 40 + ) + return True + + monkeypatch.setattr(updater.bot, 'set_webhook', set_webhook) + monkeypatch.setattr(updater.bot, 'delete_webhook', lambda *args, **kwargs: True) + monkeypatch.setattr('telegram.ext.Dispatcher.process_update', lambda _, u: q.put(u)) + + ip = '127.0.0.1' + port = randrange(1024, 49152) # Select random port + if pass_max_connections: + updater.start_webhook(ip, port, webhook_url=None, max_connections=max_connections) + else: + updater.start_webhook(ip, port, webhook_url=None) + + sleep(0.2) + + # Now, we send an update to the server via urlopen + update = Update( + 1, + message=Message(1, None, Chat(1, ''), from_user=User(1, '', False), text='Webhook 2'), + ) + self._send_webhook_msg(ip, port, update.to_json()) + sleep(0.2) + assert q.get(False) == update + updater.stop() + assert self.test_flag is True + @pytest.mark.parametrize(('error',), argvalues=[(TelegramError(''),)], ids=('TelegramError',)) def test_bootstrap_retries_success(self, monkeypatch, updater, error): retries = 2