From d9988e213574178ff12b2ed94159d9521a38f5de Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Mon, 8 Jun 2020 22:35:18 +0200 Subject: [PATCH 01/62] add new fuction to process the backlog using a timedelta spec --- telegram/ext/updater.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 02f2ed6e35c..b779ebf48a1 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -22,6 +22,7 @@ import ssl from threading import Thread, Lock, current_thread, Event from time import sleep +from datetime import datetime, timedelta, timezone from signal import signal, SIGINT, SIGTERM, SIGABRT from queue import Queue @@ -475,6 +476,31 @@ def bootstrap_clean_updates(): updates = self.bot.get_updates(updates[-1].update_id + 1) return False + def bootstrap_clean_updates_timedelta(**kwargs): + self.logger.debug('Cleaning updates from Telegram server with timedelta') + self.logger.debug('kwargs: "%s"', kwargs) + updates = self.bot.get_updates() + now = datetime.now(timezone.utc) + delta = kwargs['clean'] + + for up in reversed(updates): + self.logger.debug('msg update_id: "%s"', up.update_id) + msgdate = up.message.date.replace(tzinfo=None) + msgdate = up.message.date + if delta: + self.logger.debug('now: "%s"', now) + self.logger.debug('msg date: "%s"', msgdate) + self.logger.debug('now - msg date: "%s"', (now - msgdate)) + self.logger.debug('delta: "%s"', delta) + self.logger.debug('now - msg date >= delta (do not process msg): "%s"', (now - msgdate >= delta)) + if up.message and (now - msgdate >= delta): + # break out, we want to process this and all following msg's + self.logger.debug('return false (do not process msg)') + temp = self.bot.get_updates(up.update_id + 1) + return False + + return False + def bootstrap_set_webhook(): self.bot.set_webhook(url=webhook_url, certificate=cert, From deb251507f40ce29f05291ed4e43642a08744d83 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Mon, 8 Jun 2020 22:37:12 +0200 Subject: [PATCH 02/62] add selector to bootstrap, use type of var 'clean' to select the correct handler --- telegram/ext/updater.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index b779ebf48a1..e3e7575a3c5 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -526,11 +526,16 @@ def bootstrap_onerr_cb(exc): retries[0] = 0 # Clean pending messages, if requested. - if clean: + if isinstance(clean, bool) and clean : self._network_loop_retry(bootstrap_clean_updates, bootstrap_onerr_cb, 'bootstrap clean updates', bootstrap_interval) retries[0] = 0 sleep(1) + elif isinstance(clean, timedelta) and clean is not None: + self._network_loop_retry(bootstrap_clean_updates_timedelta, bootstrap_onerr_cb, + 'bootstrap clean updates', bootstrap_interval, **dict(clean=clean)) + retries[0] = 0 + sleep(1) # Restore/set webhook settings, if needed. Again, we don't know ahead if a webhook is set, # so we set it anyhow. From d7254b16d16a2c61f30e0c04a85bca79b9d297f0 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Mon, 8 Jun 2020 22:37:39 +0200 Subject: [PATCH 03/62] pass clean var down to handler --- telegram/ext/updater.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index e3e7575a3c5..80d95c5ee08 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -359,7 +359,7 @@ def polling_onerr_cb(exc): self._network_loop_retry(polling_action_cb, polling_onerr_cb, 'getting Updates', poll_interval) - def _network_loop_retry(self, action_cb, onerr_cb, description, interval): + def _network_loop_retry(self, action_cb, onerr_cb, description, interval, **kwargs): """Perform a loop calling `action_cb`, retrying after network errors. Stop condition for loop: `self.running` evaluates False or return value of `action_cb` @@ -378,7 +378,7 @@ def _network_loop_retry(self, action_cb, onerr_cb, description, interval): cur_interval = interval while self.running: try: - if not action_cb(): + if not action_cb(**kwargs): break except RetryAfter as e: self.logger.info('%s', e) From 158419db03116366621e8a091cb7c9a59dd6342b Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Mon, 8 Jun 2020 22:46:07 +0200 Subject: [PATCH 04/62] ensure we compare UTC vs UTC times --- telegram/ext/updater.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 80d95c5ee08..842beddd1ed 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -485,8 +485,7 @@ def bootstrap_clean_updates_timedelta(**kwargs): for up in reversed(updates): self.logger.debug('msg update_id: "%s"', up.update_id) - msgdate = up.message.date.replace(tzinfo=None) - msgdate = up.message.date + msgdate = up.message.date.replace(tzinfo=timezone.utc) if delta: self.logger.debug('now: "%s"', now) self.logger.debug('msg date: "%s"', msgdate) From bc5d964811ae7820bf2381e585b8a7a8ec9909cf Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Mon, 8 Jun 2020 22:50:25 +0200 Subject: [PATCH 05/62] clean up debugging logging --- telegram/ext/updater.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 842beddd1ed..a7a8739c976 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -477,24 +477,17 @@ def bootstrap_clean_updates(): return False def bootstrap_clean_updates_timedelta(**kwargs): - self.logger.debug('Cleaning updates from Telegram server with timedelta') - self.logger.debug('kwargs: "%s"', kwargs) + self.logger.debug('Cleaning updates from Telegram server with timedelta "%s"', kwargs['clean']) updates = self.bot.get_updates() now = datetime.now(timezone.utc) delta = kwargs['clean'] + # reversed as we just need to find the first msg that's too old for up in reversed(updates): - self.logger.debug('msg update_id: "%s"', up.update_id) msgdate = up.message.date.replace(tzinfo=timezone.utc) if delta: - self.logger.debug('now: "%s"', now) - self.logger.debug('msg date: "%s"', msgdate) - self.logger.debug('now - msg date: "%s"', (now - msgdate)) - self.logger.debug('delta: "%s"', delta) - self.logger.debug('now - msg date >= delta (do not process msg): "%s"', (now - msgdate >= delta)) if up.message and (now - msgdate >= delta): # break out, we want to process this and all following msg's - self.logger.debug('return false (do not process msg)') temp = self.bot.get_updates(up.update_id + 1) return False From b5db0c62d92bd3281eaea920dfc0b18777efcbd0 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Mon, 8 Jun 2020 22:51:42 +0200 Subject: [PATCH 06/62] include full 'timedelta' span as valide msg age --- telegram/ext/updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index a7a8739c976..eea3a890621 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -486,7 +486,7 @@ def bootstrap_clean_updates_timedelta(**kwargs): for up in reversed(updates): msgdate = up.message.date.replace(tzinfo=timezone.utc) if delta: - if up.message and (now - msgdate >= delta): + if up.message and (now - msgdate > delta): # break out, we want to process this and all following msg's temp = self.bot.get_updates(up.update_id + 1) return False From 62015f4bf37786d8d2bb06bb2615dbd3455d065d Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Mon, 8 Jun 2020 22:58:52 +0200 Subject: [PATCH 07/62] updated test 'TODO' comment --- tests/test_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 59009cb5236..4a0d9618416 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -98,7 +98,7 @@ def callback(self, bot, update): self.received = update.message.text self.cb_handler_called.set() - # TODO: test clean= argument of Updater._bootstrap + # TODO: test clean= argument, both bool and timedelta, of Updater._bootstrap @pytest.mark.parametrize(('error',), argvalues=[(TelegramError('Test Error 2'),), From 07e22b166bad55bf6ae21bf468387214decc20ad Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 9 Jun 2020 22:58:46 +0200 Subject: [PATCH 08/62] verify input 'clean' as timedelta, needs to be >=1. Throw error otherwise --- telegram/ext/updater.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index eea3a890621..209be5380bd 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -256,6 +256,8 @@ def start_polling(self, with self.__lock: if not self.running: self.running = True + if isinstance(clean, datetime.timedelta) and clean.total_seconds() < 1: + raise ValueError('Clean as timedelta needs to be >= 1 second') # Create & start threads self.job_queue.start() @@ -313,6 +315,8 @@ def start_webhook(self, with self.__lock: if not self.running: self.running = True + if isinstance(clean, datetime.timedelta) and clean.total_seconds() < 1: + raise ValueError('Clean as timedelta needs to be >= 1 second') # Create & start threads self.job_queue.start() @@ -523,7 +527,7 @@ def bootstrap_onerr_cb(exc): 'bootstrap clean updates', bootstrap_interval) retries[0] = 0 sleep(1) - elif isinstance(clean, timedelta) and clean is not None: + elif isinstance(clean, timedelta): self._network_loop_retry(bootstrap_clean_updates_timedelta, bootstrap_onerr_cb, 'bootstrap clean updates', bootstrap_interval, **dict(clean=clean)) retries[0] = 0 From 01cf3892406783ca0af7ff9c0235ebd57e8c2b08 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 9 Jun 2020 22:59:10 +0200 Subject: [PATCH 09/62] update documentation --- telegram/ext/updater.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 209be5380bd..237bccd954e 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -234,8 +234,9 @@ def start_polling(self, poll_interval (:obj:`float`, optional): Time to wait between polling updates from Telegram in seconds. Default is 0.0. timeout (:obj:`float`, optional): Passed to :attr:`telegram.Bot.get_updates`. - clean (:obj:`bool`, optional): Whether to clean any pending updates on Telegram servers - before actually starting to poll. Default is False. + clean (:obj:`bool` | :obj:`datetime.timedelta`, optional): Whether to clean any pending updates on Telegram servers + before actually starting to poll. Default is ``False``. + If a :obj:`datetime.timedelta` object is passed, pending updates older than :math:`now() - timedelta` are ignored. bootstrap_retries (:obj:`int`, optional): Whether the bootstrapping phase of the `Updater` will retry on failures on the Telegram server. @@ -252,6 +253,9 @@ def start_polling(self, Returns: :obj:`Queue`: The update queue that can be filled from the main thread. + Raises: + ValueError: If :attr:`clean` is set as :obj:`datetime.timedelta` and is < 1 second. + """ with self.__lock: if not self.running: @@ -294,8 +298,9 @@ def start_webhook(self, url_path (:obj:`str`, optional): Path inside url. cert (:obj:`str`, optional): Path to the SSL certificate file. key (:obj:`str`, optional): Path to the SSL key file. - clean (:obj:`bool`, optional): Whether to clean any pending updates on Telegram servers - before actually starting the webhook. Default is ``False``. + clean (:obj:`bool` | :obj:`datetime.timedelta`, optional): Whether to clean any pending updates on Telegram servers + before actually starting to poll. Default is ``False``. + If a :obj:`datetime.timedelta` object is passed, pending updates older than :math:`now() - timedelta` are ignored. bootstrap_retries (:obj:`int`, optional): Whether the bootstrapping phase of the `Updater` will retry on failures on the Telegram server. @@ -311,6 +316,9 @@ def start_webhook(self, Returns: :obj:`Queue`: The update queue that can be filled from the main thread. + Raises: + ValueError: If :attr:`clean` is set as :obj:`datetime.timedelta` and is < 1 second. + """ with self.__lock: if not self.running: From 766cbe6db6145d8ef0b55695900c7ceb0687c559 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 9 Jun 2020 23:13:09 +0200 Subject: [PATCH 10/62] clean up, fix isinstance calls --- telegram/ext/updater.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 237bccd954e..9227f6fed83 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -234,9 +234,10 @@ def start_polling(self, poll_interval (:obj:`float`, optional): Time to wait between polling updates from Telegram in seconds. Default is 0.0. timeout (:obj:`float`, optional): Passed to :attr:`telegram.Bot.get_updates`. - clean (:obj:`bool` | :obj:`datetime.timedelta`, optional): Whether to clean any pending updates on Telegram servers - before actually starting to poll. Default is ``False``. - If a :obj:`datetime.timedelta` object is passed, pending updates older than :math:`now() - timedelta` are ignored. + clean (:obj:`bool` | :obj:`datetime.timedelta`, optional): Whether to clean any pending + updates on Telegram servers before actually starting to poll. Default is ``False``. + If a :obj:`datetime.timedelta` object is passed, pending updates older + than :math:`now() - timedelta` are ignored. bootstrap_retries (:obj:`int`, optional): Whether the bootstrapping phase of the `Updater` will retry on failures on the Telegram server. @@ -260,7 +261,7 @@ def start_polling(self, with self.__lock: if not self.running: self.running = True - if isinstance(clean, datetime.timedelta) and clean.total_seconds() < 1: + if isinstance(clean, timedelta) and clean.total_seconds() < 1: raise ValueError('Clean as timedelta needs to be >= 1 second') # Create & start threads @@ -323,7 +324,7 @@ def start_webhook(self, with self.__lock: if not self.running: self.running = True - if isinstance(clean, datetime.timedelta) and clean.total_seconds() < 1: + if isinstance(clean, timedelta) and clean.total_seconds() < 1: raise ValueError('Clean as timedelta needs to be >= 1 second') # Create & start threads @@ -489,7 +490,8 @@ def bootstrap_clean_updates(): return False def bootstrap_clean_updates_timedelta(**kwargs): - self.logger.debug('Cleaning updates from Telegram server with timedelta "%s"', kwargs['clean']) + self.logger.debug('Cleaning updates from Telegram server with timedelta "%s"', + kwargs['clean']) updates = self.bot.get_updates() now = datetime.now(timezone.utc) delta = kwargs['clean'] @@ -530,14 +532,15 @@ def bootstrap_onerr_cb(exc): retries[0] = 0 # Clean pending messages, if requested. - if isinstance(clean, bool) and clean : + if isinstance(clean, bool) and clean: self._network_loop_retry(bootstrap_clean_updates, bootstrap_onerr_cb, 'bootstrap clean updates', bootstrap_interval) retries[0] = 0 sleep(1) elif isinstance(clean, timedelta): self._network_loop_retry(bootstrap_clean_updates_timedelta, bootstrap_onerr_cb, - 'bootstrap clean updates', bootstrap_interval, **dict(clean=clean)) + 'bootstrap clean updates', bootstrap_interval, + **dict(clean=clean)) retries[0] = 0 sleep(1) From 663aece1bfe498cf897eb1eecabf095efcb0e191 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 9 Jun 2020 23:27:19 +0200 Subject: [PATCH 11/62] clean up no.2 --- telegram/ext/updater.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 9227f6fed83..c5420ab418b 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -299,9 +299,10 @@ def start_webhook(self, url_path (:obj:`str`, optional): Path inside url. cert (:obj:`str`, optional): Path to the SSL certificate file. key (:obj:`str`, optional): Path to the SSL key file. - clean (:obj:`bool` | :obj:`datetime.timedelta`, optional): Whether to clean any pending updates on Telegram servers - before actually starting to poll. Default is ``False``. - If a :obj:`datetime.timedelta` object is passed, pending updates older than :math:`now() - timedelta` are ignored. + clean (:obj:`bool` | :obj:`datetime.timedelta`, optional): Whether to clean any pending + updates on Telegram servers before actually starting to poll. Default is ``False``. + If a :obj:`datetime.timedelta` object is passed, pending updates older + than :math:`now() - timedelta` are ignored. bootstrap_retries (:obj:`int`, optional): Whether the bootstrapping phase of the `Updater` will retry on failures on the Telegram server. From 684280f775004b8f3a5d234d3096e7a016828742 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 10 Jun 2020 09:08:47 +0200 Subject: [PATCH 12/62] fix comment, change varname --- telegram/ext/updater.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index c5420ab418b..f910131922c 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -502,8 +502,8 @@ def bootstrap_clean_updates_timedelta(**kwargs): msgdate = up.message.date.replace(tzinfo=timezone.utc) if delta: if up.message and (now - msgdate > delta): - # break out, we want to process this and all following msg's - temp = self.bot.get_updates(up.update_id + 1) + # break out, we want to process the 'next' and all following msg's + updates = self.bot.get_updates(up.update_id + 1) return False return False From f766b011333e81abfde9adf6c9e940ac5dc499b2 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 10 Jun 2020 09:37:06 +0200 Subject: [PATCH 13/62] msg.date already utc --- telegram/ext/updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index f910131922c..1eb8062729b 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -499,7 +499,7 @@ def bootstrap_clean_updates_timedelta(**kwargs): # reversed as we just need to find the first msg that's too old for up in reversed(updates): - msgdate = up.message.date.replace(tzinfo=timezone.utc) + msgdate = up.message.date.replace() if delta: if up.message and (now - msgdate > delta): # break out, we want to process the 'next' and all following msg's From 1541d64215e70c7ff20c0fd6f18f394e80b88bf7 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 10 Jun 2020 19:51:05 +0200 Subject: [PATCH 14/62] remove 'kwargs' and use partial to pass var clean --- telegram/ext/updater.py | 45 +++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 1eb8062729b..0b4167d0105 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -25,6 +25,7 @@ from datetime import datetime, timedelta, timezone from signal import signal, SIGINT, SIGTERM, SIGABRT from queue import Queue +from functools import partial from telegram import Bot, TelegramError from telegram.ext import Dispatcher, JobQueue @@ -261,8 +262,14 @@ def start_polling(self, with self.__lock: if not self.running: self.running = True - if isinstance(clean, timedelta) and clean.total_seconds() < 1: - raise ValueError('Clean as timedelta needs to be >= 1 second') + if isinstance(clean, timedelta): + if clean.total_seconds() < 1: + raise ValueError('Clean as timedelta needs to be >= 1 second') + else: + clean = datetime.now() - clean + if isinstance(clean, datetime) and clean > (datetime.now() - timedelta(seconds=1)): + raise ValueError('Clean as datetime ("%s") needs to be at least 1 second older' + 'than "now"("%s")', clean, datetime.now()) # Create & start threads self.job_queue.start() @@ -325,8 +332,14 @@ def start_webhook(self, with self.__lock: if not self.running: self.running = True - if isinstance(clean, timedelta) and clean.total_seconds() < 1: - raise ValueError('Clean as timedelta needs to be >= 1 second') + if isinstance(clean, timedelta): + if clean.total_seconds() < 1: + raise ValueError('Clean as timedelta needs to be >= 1 second') + else: + clean = datetime.now() - clean + if isinstance(clean, datetime) and clean > (datetime.now() - timedelta(seconds=1)): + raise ValueError('Clean as datetime ("%s") needs to be at least 1 second older' + 'than "now"("%s")', clean, datetime.now()) # Create & start threads self.job_queue.start() @@ -373,7 +386,7 @@ def polling_onerr_cb(exc): self._network_loop_retry(polling_action_cb, polling_onerr_cb, 'getting Updates', poll_interval) - def _network_loop_retry(self, action_cb, onerr_cb, description, interval, **kwargs): + def _network_loop_retry(self, action_cb, onerr_cb, description, interval): """Perform a loop calling `action_cb`, retrying after network errors. Stop condition for loop: `self.running` evaluates False or return value of `action_cb` @@ -392,7 +405,7 @@ def _network_loop_retry(self, action_cb, onerr_cb, description, interval, **kwar cur_interval = interval while self.running: try: - if not action_cb(**kwargs): + if not action_cb(): break except RetryAfter as e: self.logger.info('%s', e) @@ -490,18 +503,15 @@ def bootstrap_clean_updates(): updates = self.bot.get_updates(updates[-1].update_id + 1) return False - def bootstrap_clean_updates_timedelta(**kwargs): - self.logger.debug('Cleaning updates from Telegram server with timedelta "%s"', - kwargs['clean']) + def bootstrap_clean_updates_datetime(clean): + self.logger.debug('Cleaning updates from Telegram server with datetime "%s"', + datetime_cutoff) updates = self.bot.get_updates() - now = datetime.now(timezone.utc) - delta = kwargs['clean'] # reversed as we just need to find the first msg that's too old for up in reversed(updates): - msgdate = up.message.date.replace() if delta: - if up.message and (now - msgdate > delta): + if up.message and (up.message.date < datetime_cutoff): # break out, we want to process the 'next' and all following msg's updates = self.bot.get_updates(up.update_id + 1) return False @@ -538,10 +548,11 @@ def bootstrap_onerr_cb(exc): 'bootstrap clean updates', bootstrap_interval) retries[0] = 0 sleep(1) - elif isinstance(clean, timedelta): - self._network_loop_retry(bootstrap_clean_updates_timedelta, bootstrap_onerr_cb, - 'bootstrap clean updates', bootstrap_interval, - **dict(clean=clean)) + elif isinstance(clean, datetime): + bootstrap_clean_updates_datetime_p = partial(bootstrap_clean_updates_datetime, + datetime_cutoff=clean) + self._network_loop_retry(bootstrap_clean_updates_datetime_p, bootstrap_onerr_cb, + 'bootstrap clean updates datetime', bootstrap_interval) retries[0] = 0 sleep(1) From 4804b8bd4166c41721868829f7b9a85adee72702 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 10 Jun 2020 19:51:25 +0200 Subject: [PATCH 15/62] add debugging info --- telegram/ext/updater.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 0b4167d0105..da16f351e3d 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -262,14 +262,24 @@ def start_polling(self, with self.__lock: if not self.running: self.running = True + self.logger.debug('clean: "%s"', type(clean)) + self.logger.debug('clean: "%s"', clean) + self.logger.debug('now: "%s"', datetime.now()) + a=(datetime.now() -timedelta(seconds=1))) + self.logger.debug('now - delta: "%s"', (a) + self.logger.debug('clean > (now - delta): "%s"', (clean > a))) if isinstance(clean, timedelta): if clean.total_seconds() < 1: raise ValueError('Clean as timedelta needs to be >= 1 second') else: + self.logger.debug('clean as delta: "%s"', clean) clean = datetime.now() - clean + self.logger.debug('clean as datetime: "%s"', clean) if isinstance(clean, datetime) and clean > (datetime.now() - timedelta(seconds=1)): raise ValueError('Clean as datetime ("%s") needs to be at least 1 second older' 'than "now"("%s")', clean, datetime.now()) + self.logger.debug('clean after: "%s"', clean) + # Create & start threads self.job_queue.start() @@ -332,14 +342,6 @@ def start_webhook(self, with self.__lock: if not self.running: self.running = True - if isinstance(clean, timedelta): - if clean.total_seconds() < 1: - raise ValueError('Clean as timedelta needs to be >= 1 second') - else: - clean = datetime.now() - clean - if isinstance(clean, datetime) and clean > (datetime.now() - timedelta(seconds=1)): - raise ValueError('Clean as datetime ("%s") needs to be at least 1 second older' - 'than "now"("%s")', clean, datetime.now()) # Create & start threads self.job_queue.start() @@ -511,6 +513,9 @@ def bootstrap_clean_updates_datetime(clean): # reversed as we just need to find the first msg that's too old for up in reversed(updates): if delta: + self.logger.debug('cutoff: "%s"', datetime_cutoff) + self.logger.debug('msg date: "%s"', up.message.date) + self.logger.debug('msg date < cutoff: "%s"', (up.message.date < datetime_cutoff)) if up.message and (up.message.date < datetime_cutoff): # break out, we want to process the 'next' and all following msg's updates = self.bot.get_updates(up.update_id + 1) From 21e0226b9779c0d0bd1f629c4406e3ea36933006 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 10 Jun 2020 21:59:24 +0200 Subject: [PATCH 16/62] forgot to rename var in function def... --- telegram/ext/updater.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index da16f351e3d..cb84563d00b 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -505,21 +505,20 @@ def bootstrap_clean_updates(): updates = self.bot.get_updates(updates[-1].update_id + 1) return False - def bootstrap_clean_updates_datetime(clean): + def bootstrap_clean_updates_datetime(datetime_cutoff): self.logger.debug('Cleaning updates from Telegram server with datetime "%s"', datetime_cutoff) updates = self.bot.get_updates() # reversed as we just need to find the first msg that's too old for up in reversed(updates): - if delta: - self.logger.debug('cutoff: "%s"', datetime_cutoff) - self.logger.debug('msg date: "%s"', up.message.date) - self.logger.debug('msg date < cutoff: "%s"', (up.message.date < datetime_cutoff)) - if up.message and (up.message.date < datetime_cutoff): - # break out, we want to process the 'next' and all following msg's - updates = self.bot.get_updates(up.update_id + 1) - return False + self.logger.debug('cutoff: "%s"', datetime_cutoff) + self.logger.debug('msg date: "%s"', up.message.date) + self.logger.debug('msg date < cutoff: "%s"', (up.message.date < datetime_cutoff)) + if up.message and (up.message.date < datetime_cutoff): + # break out, we want to process the 'next' and all following msg's + updates = self.bot.get_updates(up.update_id + 1) + return False return False From cafb18816f7c9d0d930c21d165843515f87178ec Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 10 Jun 2020 22:03:09 +0200 Subject: [PATCH 17/62] if timedelta is passed, convert to datetime. If datetime is passed, verify timezone is set, fix it if not. Throw ValueError is timedelta < 1 sec or datetime not < '1 sec' from now. Still has a lot of debugging stuff in there. --- telegram/ext/updater.py | 48 ++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index cb84563d00b..27577f96170 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -262,24 +262,46 @@ def start_polling(self, with self.__lock: if not self.running: self.running = True - self.logger.debug('clean: "%s"', type(clean)) - self.logger.debug('clean: "%s"', clean) - self.logger.debug('now: "%s"', datetime.now()) - a=(datetime.now() -timedelta(seconds=1))) - self.logger.debug('now - delta: "%s"', (a) - self.logger.debug('clean > (now - delta): "%s"', (clean > a))) + + self.logger.debug('clean: "%s"', type(clean)) + self.logger.debug('clean: "%s"', clean) + self.logger.debug('now: "%s"', datetime.now(tz=timezone.utc)) + if isinstance(clean, timedelta): if clean.total_seconds() < 1: raise ValueError('Clean as timedelta needs to be >= 1 second') else: - self.logger.debug('clean as delta: "%s"', clean) - clean = datetime.now() - clean - self.logger.debug('clean as datetime: "%s"', clean) - if isinstance(clean, datetime) and clean > (datetime.now() - timedelta(seconds=1)): - raise ValueError('Clean as datetime ("%s") needs to be at least 1 second older' - 'than "now"("%s")', clean, datetime.now()) - self.logger.debug('clean after: "%s"', clean) + a=(datetime.now(tz=timezone.utc) - clean) + self.logger.debug('now - delta: "%s"', a) + self.logger.debug('now - delta type: "%s"', type(a)) + self.logger.debug('clean as delta: "%s"', clean) + # convert to datetime + clean = datetime.now(tz=timezone.utc) - clean + self.logger.debug('clean as datetime: "%s"', clean) + elif isinstance(clean, datetime): + + self.logger.debug('clean tz: "%s"', clean.tzname()) + + if ( + clean.tzinfo is None or + (clean.tzinfo is not None and clean.tzinfo.utcoffset(clean) is None) + ): + # we need a tz as msg.date is aware and we want to compare + # no tz passed so we assume it's UTC + self.logger.warning('"clean" passed as datetime object ("%s") but' + ' has no timezone set, we\'re assuming it\'s UTC!' + , clean) + clean=clean.replace(tzinfo=timezone.utc) + + self.logger.debug('clean > (now - 1sec): "%s"', + (clean > (datetime.now(tz=timezone.utc) - timedelta(seconds=1)))) + + if clean > (datetime.now(tz=timezone.utc) - timedelta(seconds=1)): + raise ValueError('Clean as datetime ("%s") needs to be at least 1 second' + ' older than "now"("%s")' % (clean, + datetime.now(tz=timezone.utc))) + self.logger.debug('clean after: "%s"', clean) # Create & start threads self.job_queue.start() From 531b4849945caf07058932c3bbd3e99fc6860e5a Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Thu, 11 Jun 2020 21:07:45 +0200 Subject: [PATCH 18/62] add comments this if for debugging --- telegram/ext/updater.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 27577f96170..1151362687f 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -262,25 +262,30 @@ def start_polling(self, with self.__lock: if not self.running: self.running = True - + ### for my debugging will be removed in cleanup ### self.logger.debug('clean: "%s"', type(clean)) self.logger.debug('clean: "%s"', clean) self.logger.debug('now: "%s"', datetime.now(tz=timezone.utc)) + ### for my debugging will be removed in cleanup ### if isinstance(clean, timedelta): if clean.total_seconds() < 1: raise ValueError('Clean as timedelta needs to be >= 1 second') else: + ### for my debugging will be removed in cleanup ### a=(datetime.now(tz=timezone.utc) - clean) self.logger.debug('now - delta: "%s"', a) self.logger.debug('now - delta type: "%s"', type(a)) self.logger.debug('clean as delta: "%s"', clean) + ### for my debugging will be removed in cleanup ### + # convert to datetime clean = datetime.now(tz=timezone.utc) - clean self.logger.debug('clean as datetime: "%s"', clean) elif isinstance(clean, datetime): - + ### for my debugging will be removed in cleanup ### self.logger.debug('clean tz: "%s"', clean.tzname()) + ### for my debugging will be removed in cleanup ### if ( clean.tzinfo is None or @@ -293,15 +298,19 @@ def start_polling(self, , clean) clean=clean.replace(tzinfo=timezone.utc) + ### for my debugging will be removed in cleanup ### self.logger.debug('clean > (now - 1sec): "%s"', (clean > (datetime.now(tz=timezone.utc) - timedelta(seconds=1)))) + ### for my debugging will be removed in cleanup ### if clean > (datetime.now(tz=timezone.utc) - timedelta(seconds=1)): raise ValueError('Clean as datetime ("%s") needs to be at least 1 second' ' older than "now"("%s")' % (clean, datetime.now(tz=timezone.utc))) + ### for my debugging will be removed in cleanup ### self.logger.debug('clean after: "%s"', clean) + ### for my debugging will be removed in cleanup ### # Create & start threads self.job_queue.start() @@ -534,9 +543,12 @@ def bootstrap_clean_updates_datetime(datetime_cutoff): # reversed as we just need to find the first msg that's too old for up in reversed(updates): + ### for my debugging will be removed in cleanup ### self.logger.debug('cutoff: "%s"', datetime_cutoff) self.logger.debug('msg date: "%s"', up.message.date) self.logger.debug('msg date < cutoff: "%s"', (up.message.date < datetime_cutoff)) + ### for my debugging will be removed in cleanup ### + if up.message and (up.message.date < datetime_cutoff): # break out, we want to process the 'next' and all following msg's updates = self.bot.get_updates(up.update_id + 1) From ce35971f8e99aa130591edc55f083270094a1d8a Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Thu, 11 Jun 2020 21:09:50 +0200 Subject: [PATCH 19/62] clean up, remove all extra logger.debug's --- telegram/ext/updater.py | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 1151362687f..c8a23008e44 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -262,30 +262,14 @@ def start_polling(self, with self.__lock: if not self.running: self.running = True - ### for my debugging will be removed in cleanup ### - self.logger.debug('clean: "%s"', type(clean)) - self.logger.debug('clean: "%s"', clean) - self.logger.debug('now: "%s"', datetime.now(tz=timezone.utc)) - ### for my debugging will be removed in cleanup ### if isinstance(clean, timedelta): if clean.total_seconds() < 1: raise ValueError('Clean as timedelta needs to be >= 1 second') else: - ### for my debugging will be removed in cleanup ### - a=(datetime.now(tz=timezone.utc) - clean) - self.logger.debug('now - delta: "%s"', a) - self.logger.debug('now - delta type: "%s"', type(a)) - self.logger.debug('clean as delta: "%s"', clean) - ### for my debugging will be removed in cleanup ### - # convert to datetime clean = datetime.now(tz=timezone.utc) - clean - self.logger.debug('clean as datetime: "%s"', clean) elif isinstance(clean, datetime): - ### for my debugging will be removed in cleanup ### - self.logger.debug('clean tz: "%s"', clean.tzname()) - ### for my debugging will be removed in cleanup ### if ( clean.tzinfo is None or @@ -298,20 +282,11 @@ def start_polling(self, , clean) clean=clean.replace(tzinfo=timezone.utc) - ### for my debugging will be removed in cleanup ### - self.logger.debug('clean > (now - 1sec): "%s"', - (clean > (datetime.now(tz=timezone.utc) - timedelta(seconds=1)))) - ### for my debugging will be removed in cleanup ### - if clean > (datetime.now(tz=timezone.utc) - timedelta(seconds=1)): raise ValueError('Clean as datetime ("%s") needs to be at least 1 second' ' older than "now"("%s")' % (clean, datetime.now(tz=timezone.utc))) - ### for my debugging will be removed in cleanup ### - self.logger.debug('clean after: "%s"', clean) - ### for my debugging will be removed in cleanup ### - # Create & start threads self.job_queue.start() dispatcher_ready = Event() @@ -543,12 +518,6 @@ def bootstrap_clean_updates_datetime(datetime_cutoff): # reversed as we just need to find the first msg that's too old for up in reversed(updates): - ### for my debugging will be removed in cleanup ### - self.logger.debug('cutoff: "%s"', datetime_cutoff) - self.logger.debug('msg date: "%s"', up.message.date) - self.logger.debug('msg date < cutoff: "%s"', (up.message.date < datetime_cutoff)) - ### for my debugging will be removed in cleanup ### - if up.message and (up.message.date < datetime_cutoff): # break out, we want to process the 'next' and all following msg's updates = self.bot.get_updates(up.update_id + 1) From 33410d38220f82e02fe42b954428417589d4174d Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Thu, 11 Jun 2020 21:33:02 +0200 Subject: [PATCH 20/62] check if timedelta is negative, if it is convert to positive --- telegram/ext/updater.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index c8a23008e44..02f5963e2c5 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -264,6 +264,9 @@ def start_polling(self, self.running = True if isinstance(clean, timedelta): + if clean.total_seconds() < 0: + clean = clean * -1 + if clean.total_seconds() < 1: raise ValueError('Clean as timedelta needs to be >= 1 second') else: From bd216992fae38abf49d98566cb5eab8e638670b5 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Thu, 11 Jun 2020 22:01:25 +0200 Subject: [PATCH 21/62] update docstring --- telegram/ext/updater.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 02f5963e2c5..77222cd848c 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -235,10 +235,18 @@ def start_polling(self, poll_interval (:obj:`float`, optional): Time to wait between polling updates from Telegram in seconds. Default is 0.0. timeout (:obj:`float`, optional): Passed to :attr:`telegram.Bot.get_updates`. - clean (:obj:`bool` | :obj:`datetime.timedelta`, optional): Whether to clean any pending - updates on Telegram servers before actually starting to poll. Default is ``False``. - If a :obj:`datetime.timedelta` object is passed, pending updates older - than :math:`now() - timedelta` are ignored. + clean (:obj:`bool` | :obj:`datetime.timedelta` | :obj:`datetime.timedelta`, optional): + Whether to clean any pending updates on Telegram servers before actually starting + to poll. This parameter will be interpreted depending on its type. + + * :obj:`bool` ``True`` cleans all update. Default is ``False``. + * :obj:`datetime.timedelta` will be interpreted as "time before now" cut off. + Pending updates older than the cut off will be cleaned up. timedelta is sign + independent, both positive and negative deltas are interpreted as "in the past". + * :obj:`datetime.datetime` will be interpreted as a specific date and time as + cut off. Pending updates older than the cut off will be cleaned up. + If the timezone (``datetime.tzinfo``) is ``None``, UTC will be assumed. + bootstrap_retries (:obj:`int`, optional): Whether the bootstrapping phase of the `Updater` will retry on failures on the Telegram server. From 99dd3770b776b49b21c8e112aff721d187838fbe Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Thu, 11 Jun 2020 22:04:41 +0200 Subject: [PATCH 22/62] remove warning when no tz is set, doc string has warning --- telegram/ext/updater.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 77222cd848c..8e5e69aef8e 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -286,11 +286,6 @@ def start_polling(self, clean.tzinfo is None or (clean.tzinfo is not None and clean.tzinfo.utcoffset(clean) is None) ): - # we need a tz as msg.date is aware and we want to compare - # no tz passed so we assume it's UTC - self.logger.warning('"clean" passed as datetime object ("%s") but' - ' has no timezone set, we\'re assuming it\'s UTC!' - , clean) clean=clean.replace(tzinfo=timezone.utc) if clean > (datetime.now(tz=timezone.utc) - timedelta(seconds=1)): From cb7bb3b134240545f10f352140c124b97567e0f2 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Thu, 11 Jun 2020 22:12:04 +0200 Subject: [PATCH 23/62] change .message to .effective_message to catch all types --- telegram/ext/updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 8e5e69aef8e..f4de112146d 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -524,7 +524,7 @@ def bootstrap_clean_updates_datetime(datetime_cutoff): # reversed as we just need to find the first msg that's too old for up in reversed(updates): - if up.message and (up.message.date < datetime_cutoff): + if up.effective_message and (up.effective_message.date < datetime_cutoff): # break out, we want to process the 'next' and all following msg's updates = self.bot.get_updates(up.update_id + 1) return False From 6c44b67bb92123e6937bab43631d456ef68d461b Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Thu, 11 Jun 2020 22:55:32 +0200 Subject: [PATCH 24/62] check if msg.date is None, if yes stop cleaning and let all updates pass --- telegram/ext/updater.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index f4de112146d..5e7b58f3d63 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -524,7 +524,10 @@ def bootstrap_clean_updates_datetime(datetime_cutoff): # reversed as we just need to find the first msg that's too old for up in reversed(updates): - if up.effective_message and (up.effective_message.date < datetime_cutoff): + if up.effective_message.date is None: + # break out and leave all updates as is + return False + elif up.effective_message and (up.effective_message.date < datetime_cutoff): # break out, we want to process the 'next' and all following msg's updates = self.bot.get_updates(up.update_id + 1) return False From 51953a6ade6af143b4c27ce75199e463ea2bc233 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Thu, 11 Jun 2020 23:49:51 +0200 Subject: [PATCH 25/62] update start_polling doc string --- telegram/ext/updater.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 5e7b58f3d63..93ec2938378 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -241,11 +241,18 @@ def start_polling(self, * :obj:`bool` ``True`` cleans all update. Default is ``False``. * :obj:`datetime.timedelta` will be interpreted as "time before now" cut off. - Pending updates older than the cut off will be cleaned up. timedelta is sign - independent, both positive and negative deltas are interpreted as "in the past". + Pending updates older than the cut off will be cleaned up. + :obj:`datetime.timedelta` is sign independent, both positive and negative deltas + are interpreted as "in the past". * :obj:`datetime.datetime` will be interpreted as a specific date and time as - cut off. Pending updates older than the cut off will be cleaned up. - If the timezone (``datetime.tzinfo``) is ``None``, UTC will be assumed. + cut off. Pending updates older than the cut off will be cleaned up. + If the timezone (``datetime.tzinfo``) is ``None``, UTC will be assumed. + + Note: + If :attr:`clean` is :obj:`datetime.timedelta` or :obj:`datetime.datetime` and + if a :class:`telegram.Update.effective_message` is found with + :attr:`telegram.Message.date` is ``None``, before the :obj:`datetime.timedelta` + or :obj:`datetime.datetime` condition is met, all updates will pass through. bootstrap_retries (:obj:`int`, optional): Whether the bootstrapping phase of the `Updater` will retry on failures on the Telegram server. @@ -264,7 +271,9 @@ def start_polling(self, :obj:`Queue`: The update queue that can be filled from the main thread. Raises: - ValueError: If :attr:`clean` is set as :obj:`datetime.timedelta` and is < 1 second. + ValueError: if :attr:`clean` is :obj:`datetime.timedelta` and is < 1 second. + ValueError: if :attr:`clean` is :obj:`datetime.datetime` is not a least 1 second older + than `now()`. """ with self.__lock: From e3f46b8eb30de7c2c7fad13d494d86e0619a0e92 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Thu, 11 Jun 2020 23:55:12 +0200 Subject: [PATCH 26/62] apply code to start_webhook --- telegram/ext/updater.py | 48 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 93ec2938378..09a09f7fc21 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -337,10 +337,25 @@ def start_webhook(self, url_path (:obj:`str`, optional): Path inside url. cert (:obj:`str`, optional): Path to the SSL certificate file. key (:obj:`str`, optional): Path to the SSL key file. - clean (:obj:`bool` | :obj:`datetime.timedelta`, optional): Whether to clean any pending - updates on Telegram servers before actually starting to poll. Default is ``False``. - If a :obj:`datetime.timedelta` object is passed, pending updates older - than :math:`now() - timedelta` are ignored. + clean (:obj:`bool` | :obj:`datetime.timedelta` | :obj:`datetime.timedelta`, optional): + Whether to clean any pending updates on Telegram servers before actually starting + to poll. This parameter will be interpreted depending on its type. + + * :obj:`bool` ``True`` cleans all update. Default is ``False``. + * :obj:`datetime.timedelta` will be interpreted as "time before now" cut off. + Pending updates older than the cut off will be cleaned up. + :obj:`datetime.timedelta` is sign independent, both positive and negative deltas + are interpreted as "in the past". + * :obj:`datetime.datetime` will be interpreted as a specific date and time as + cut off. Pending updates older than the cut off will be cleaned up. + If the timezone (``datetime.tzinfo``) is ``None``, UTC will be assumed. + + Note: + If :attr:`clean` is :obj:`datetime.timedelta` or :obj:`datetime.datetime` and + if a :class:`telegram.Update.effective_message` is found with + :attr:`telegram.Message.date` is ``None``, before the :obj:`datetime.timedelta` + or :obj:`datetime.datetime` condition is met, all updates will pass through. + bootstrap_retries (:obj:`int`, optional): Whether the bootstrapping phase of the `Updater` will retry on failures on the Telegram server. @@ -357,13 +372,36 @@ def start_webhook(self, :obj:`Queue`: The update queue that can be filled from the main thread. Raises: - ValueError: If :attr:`clean` is set as :obj:`datetime.timedelta` and is < 1 second. + ValueError: if :attr:`clean` is :obj:`datetime.timedelta` and is < 1 second. + ValueError: if :attr:`clean` is :obj:`datetime.datetime` is not a least 1 second older + than `now()`. """ with self.__lock: if not self.running: self.running = True + if isinstance(clean, timedelta): + if clean.total_seconds() < 0: + clean = clean * -1 + + if clean.total_seconds() < 1: + raise ValueError('Clean as timedelta needs to be >= 1 second') + else: + # convert to datetime + clean = datetime.now(tz=timezone.utc) - clean + elif isinstance(clean, datetime): + if ( + clean.tzinfo is None or + (clean.tzinfo is not None and clean.tzinfo.utcoffset(clean) is None) + ): + clean=clean.replace(tzinfo=timezone.utc) + + if clean > (datetime.now(tz=timezone.utc) - timedelta(seconds=1)): + raise ValueError('Clean as datetime ("%s") needs to be at least 1 second' + ' older than "now"("%s")' % (clean, + datetime.now(tz=timezone.utc))) + # Create & start threads self.job_queue.start() self._init_thread(self.dispatcher.start, "dispatcher"), From 73adbdd07a81f9dfe95580ae1a29f494e6ead379 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Thu, 11 Jun 2020 23:56:18 +0200 Subject: [PATCH 27/62] typo --- telegram/ext/updater.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 09a09f7fc21..7659786e87f 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -239,7 +239,7 @@ def start_polling(self, Whether to clean any pending updates on Telegram servers before actually starting to poll. This parameter will be interpreted depending on its type. - * :obj:`bool` ``True`` cleans all update. Default is ``False``. + * :obj:`bool` ``True`` cleans all updates. Default is ``False``. * :obj:`datetime.timedelta` will be interpreted as "time before now" cut off. Pending updates older than the cut off will be cleaned up. :obj:`datetime.timedelta` is sign independent, both positive and negative deltas @@ -341,7 +341,7 @@ def start_webhook(self, Whether to clean any pending updates on Telegram servers before actually starting to poll. This parameter will be interpreted depending on its type. - * :obj:`bool` ``True`` cleans all update. Default is ``False``. + * :obj:`bool` ``True`` cleans all updates. Default is ``False``. * :obj:`datetime.timedelta` will be interpreted as "time before now" cut off. Pending updates older than the cut off will be cleaned up. :obj:`datetime.timedelta` is sign independent, both positive and negative deltas From 907e092894f47937722b3568a520d26bb2461827 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Sat, 13 Jun 2020 23:07:19 +0200 Subject: [PATCH 28/62] remove stale 'sleep' that was just in there to wait for the cleaning process to be done --- telegram/ext/updater.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 7659786e87f..ba80e3315c1 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -610,14 +610,12 @@ def bootstrap_onerr_cb(exc): self._network_loop_retry(bootstrap_clean_updates, bootstrap_onerr_cb, 'bootstrap clean updates', bootstrap_interval) retries[0] = 0 - sleep(1) elif isinstance(clean, datetime): bootstrap_clean_updates_datetime_p = partial(bootstrap_clean_updates_datetime, datetime_cutoff=clean) self._network_loop_retry(bootstrap_clean_updates_datetime_p, bootstrap_onerr_cb, 'bootstrap clean updates datetime', bootstrap_interval) retries[0] = 0 - sleep(1) # Restore/set webhook settings, if needed. Again, we don't know ahead if a webhook is set, # so we set it anyhow. From c887ac37b7538bba25c5b1097c5605aa9b3a2b1e Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 16 Jun 2020 21:42:48 +0200 Subject: [PATCH 29/62] fix bug - mismatched ident --- telegram/ext/updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index ba80e3315c1..897d5fefa97 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -381,7 +381,7 @@ def start_webhook(self, if not self.running: self.running = True - if isinstance(clean, timedelta): + if isinstance(clean, timedelta): if clean.total_seconds() < 0: clean = clean * -1 From eae79ec15ebb340cfc14fab4d2123a8fe6ac8570 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 16 Jun 2020 21:51:09 +0200 Subject: [PATCH 30/62] remove slow test functions --- tests/test_updater.py | 60 ++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 4a0d9618416..99b624a3e26 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -301,6 +301,19 @@ def test_webhook_tornado_win_py38_workaround(self, updater, monkeypatch): updater.stop() + + + + + + + + + + + + + @pytest.mark.parametrize(('error',), argvalues=[(TelegramError(''),)], ids=('TelegramError',)) @@ -337,6 +350,21 @@ def attempt(*args, **kwargs): updater._bootstrap(retries, False, 'path', None, bootstrap_interval=0) assert self.attempts == attempts + + + + + + + + + + + + + + + @flaky(3, 1) def test_webhook_invalid_posts(self, updater): ip = '127.0.0.1' @@ -409,38 +437,6 @@ def signal_sender(self, updater): os.kill(os.getpid(), signal.SIGTERM) - @signalskip - def test_idle(self, updater, caplog): - updater.start_polling(0.01) - Thread(target=partial(self.signal_sender, updater=updater)).start() - - with caplog.at_level(logging.INFO): - updater.idle() - - rec = caplog.records[-1] - assert rec.msg.startswith('Received signal {}'.format(signal.SIGTERM)) - assert rec.levelname == 'INFO' - - # If we get this far, idle() ran through - sleep(.5) - assert updater.running is False - - @signalskip - def test_user_signal(self, updater): - temp_var = {'a': 0} - - def user_signal_inc(signum, frame): - temp_var['a'] = 1 - - updater.user_sig_handler = user_signal_inc - updater.start_polling(0.01) - Thread(target=partial(self.signal_sender, updater=updater)).start() - updater.idle() - # If we get this far, idle() ran through - sleep(.5) - assert updater.running is False - assert temp_var['a'] != 0 - def test_create_bot(self): updater = Updater('123:abcd') assert updater.bot is not None From 30e854f4eeceb86ccc323e3208f6c5778a82e91a Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 16 Jun 2020 23:23:40 +0200 Subject: [PATCH 31/62] test function for clean attr --- tests/test_updater.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/test_updater.py b/tests/test_updater.py index 99b624a3e26..7042b151aa0 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -350,6 +350,45 @@ def attempt(*args, **kwargs): updater._bootstrap(retries, False, 'path', None, bootstrap_interval=0) assert self.attempts == attempts + @pytest.mark.parametrize(('error', 'clean'), + argvalues=[(TelegramError(''), 2), + (Unauthorized(''), 1), + (InvalidToken(), 1)], + ids=('TelegramError', 'Unauthorized', 'InvalidToken')) + def test_bootstrap_clean_bool(self, monkeypatch, updater, error, clean): + clean = True + retries =1 + + def attempt(*args, **kwargs): + self.attempts += 1 + raise error + + def updates(*args, **kwargs): + # we're hitting this func twice + # 1. no args, expecting list of updates + # 2. with arg, int, expecting list args, delete all updates with updated_id < int + + # case 2 + if len(args) > 0: + self.attempts + raise error + + # case 1 + # return list of dict's + i=0 + ls = [] + while i < 4: + ls.append({"update_id": i}) + i+=1 + return ls + + monkeypatch.setattr(updater.bot, 'get_updates', updates) + + updater.running = True + with pytest.raises(type(error)): + updater._bootstrap(retries, clean, None, None, bootstrap_interval=0) + assert self.attempts == retries + From 091b7a9a6238c03a47153a9a7fe1539150e2e7e0 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 16 Jun 2020 23:38:03 +0200 Subject: [PATCH 32/62] create fake class to set attribute, deepcopy to ensure we have unique objects --- tests/test_updater.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 7042b151aa0..95595dff666 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -21,6 +21,7 @@ import signal import sys import asyncio +import copy from flaky import flaky from functools import partial from queue import Queue @@ -372,13 +373,18 @@ def updates(*args, **kwargs): if len(args) > 0: self.attempts raise error + + class fakeUpdate(object): + pass # case 1 # return list of dict's i=0 ls = [] while i < 4: - ls.append({"update_id": i}) + o = fakeUpdate() + o.update_id = i + ls.append(copy.deepcopy(o)) i+=1 return ls From 72098b705d70fdf9beaa801dae399f021e5ff85b Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 16 Jun 2020 23:40:46 +0200 Subject: [PATCH 33/62] remove all existing testing fucntions to reduce test time --- tests/test_updater.py | 331 ------------------------------------------ 1 file changed, 331 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 95595dff666..2d506c83454 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -101,210 +101,6 @@ def callback(self, bot, update): # TODO: test clean= argument, both bool and timedelta, of Updater._bootstrap - @pytest.mark.parametrize(('error',), - argvalues=[(TelegramError('Test Error 2'),), - (Unauthorized('Test Unauthorized'),)], - ids=('TelegramError', 'Unauthorized')) - def test_get_updates_normal_err(self, monkeypatch, updater, error): - def test(*args, **kwargs): - raise error - - monkeypatch.setattr(updater.bot, 'get_updates', test) - monkeypatch.setattr(updater.bot, 'set_webhook', lambda *args, **kwargs: True) - updater.dispatcher.add_error_handler(self.error_handler) - updater.start_polling(0.01) - - # Make sure that the error handler was called - self.err_handler_called.wait() - assert self.received == error.message - - # Make sure that Updater polling thread keeps running - self.err_handler_called.clear() - self.err_handler_called.wait() - - def test_get_updates_bailout_err(self, monkeypatch, updater, caplog): - error = InvalidToken() - - def test(*args, **kwargs): - raise error - - with caplog.at_level(logging.DEBUG): - monkeypatch.setattr(updater.bot, 'get_updates', test) - monkeypatch.setattr(updater.bot, 'set_webhook', lambda *args, **kwargs: True) - updater.dispatcher.add_error_handler(self.error_handler) - updater.start_polling(0.01) - assert self.err_handler_called.wait(1) is not True - - sleep(1) - # NOTE: This test might hit a race condition and fail (though the 1 seconds delay above - # should work around it). - # NOTE: Checking Updater.running is problematic because it is not set to False when there's - # an unhandled exception. - # TODO: We should have a way to poll Updater status and decide if it's running or not. - import pprint - pprint.pprint([rec.getMessage() for rec in caplog.get_records('call')]) - assert any('unhandled exception in Bot:{}:updater'.format(updater.bot.id) in - rec.getMessage() for rec in caplog.get_records('call')) - - @pytest.mark.parametrize(('error',), - argvalues=[(RetryAfter(0.01),), - (TimedOut(),)], - ids=('RetryAfter', 'TimedOut')) - def test_get_updates_retries(self, monkeypatch, updater, error): - event = Event() - - def test(*args, **kwargs): - event.set() - raise error - - monkeypatch.setattr(updater.bot, 'get_updates', test) - monkeypatch.setattr(updater.bot, 'set_webhook', lambda *args, **kwargs: True) - updater.dispatcher.add_error_handler(self.error_handler) - updater.start_polling(0.01) - - # Make sure that get_updates was called, but not the error handler - event.wait() - assert self.err_handler_called.wait(0.5) is not True - assert self.received != error.message - - # Make sure that Updater polling thread keeps running - event.clear() - event.wait() - assert self.err_handler_called.wait(0.5) is not True - - def test_webhook(self, monkeypatch, updater): - q = Queue() - monkeypatch.setattr(updater.bot, 'set_webhook', lambda *args, **kwargs: True) - 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 - updater.start_webhook( - ip, - port, - url_path='TOKEN') - sleep(.2) - try: - # Now, we send an update to the server via urlopen - update = Update(1, message=Message(1, User(1, '', False), None, Chat(1, ''), - text='Webhook')) - self._send_webhook_msg(ip, port, update.to_json(), 'TOKEN') - sleep(.2) - assert q.get(False) == update - - # Returns 404 if path is incorrect - with pytest.raises(HTTPError) as excinfo: - self._send_webhook_msg(ip, port, None, 'webookhandler.py') - assert excinfo.value.code == 404 - - with pytest.raises(HTTPError) as excinfo: - self._send_webhook_msg(ip, port, None, 'webookhandler.py', - get_method=lambda: 'HEAD') - assert excinfo.value.code == 404 - - # Test multiple shutdown() calls - updater.httpd.shutdown() - finally: - updater.httpd.shutdown() - sleep(.2) - assert not updater.httpd.is_running - updater.stop() - - def test_webhook_ssl(self, monkeypatch, updater): - monkeypatch.setattr(updater.bot, 'set_webhook', lambda *args, **kwargs: True) - monkeypatch.setattr(updater.bot, 'delete_webhook', lambda *args, **kwargs: True) - ip = '127.0.0.1' - port = randrange(1024, 49152) # Select random port - tg_err = False - try: - updater._start_webhook( - ip, - port, - url_path='TOKEN', - cert='./tests/test_updater.py', - key='./tests/test_updater.py', - bootstrap_retries=0, - clean=False, - webhook_url=None, - allowed_updates=None) - except TelegramError: - tg_err = True - assert tg_err - - def test_webhook_no_ssl(self, monkeypatch, updater): - q = Queue() - monkeypatch.setattr(updater.bot, 'set_webhook', lambda *args, **kwargs: True) - 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 - updater.start_webhook(ip, port, webhook_url=None) - sleep(.2) - - # Now, we send an update to the server via urlopen - update = Update(1, message=Message(1, User(1, '', False), None, Chat(1, ''), - text='Webhook 2')) - self._send_webhook_msg(ip, port, update.to_json()) - sleep(.2) - assert q.get(False) == update - updater.stop() - - def test_webhook_default_quote(self, monkeypatch, updater): - updater._default_quote = True - q = Queue() - monkeypatch.setattr(updater.bot, 'set_webhook', lambda *args, **kwargs: True) - 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 - updater.start_webhook( - ip, - port, - url_path='TOKEN') - sleep(.2) - - # Now, we send an update to the server via urlopen - update = Update(1, message=Message(1, User(1, '', False), None, Chat(1, ''), - text='Webhook')) - self._send_webhook_msg(ip, port, update.to_json(), 'TOKEN') - sleep(.2) - # assert q.get(False) == update - assert q.get(False).message.default_quote is True - updater.stop() - - @pytest.mark.skipif(not (sys.platform.startswith("win") and sys.version_info >= (3, 8)), - reason="only relevant on win with py>=3.8") - def test_webhook_tornado_win_py38_workaround(self, updater, monkeypatch): - updater._default_quote = True - q = Queue() - monkeypatch.setattr(updater.bot, 'set_webhook', lambda *args, **kwargs: True) - 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 - updater.start_webhook( - ip, - port, - url_path='TOKEN') - sleep(.2) - - try: - from asyncio import (WindowsSelectorEventLoopPolicy) - except ImportError: - pass - # not affected - else: - assert isinstance(asyncio.get_event_loop_policy(), WindowsSelectorEventLoopPolicy) - - updater.stop() - - - - @@ -395,130 +191,3 @@ class fakeUpdate(object): updater._bootstrap(retries, clean, None, None, bootstrap_interval=0) assert self.attempts == retries - - - - - - - - - - - - - - - - @flaky(3, 1) - def test_webhook_invalid_posts(self, updater): - ip = '127.0.0.1' - port = randrange(1024, 49152) # select random port for travis - thr = Thread( - target=updater._start_webhook, - args=(ip, port, '', None, None, 0, False, None, None)) - thr.start() - - sleep(.2) - - try: - with pytest.raises(HTTPError) as excinfo: - self._send_webhook_msg(ip, port, 'data', - content_type='application/xml') - assert excinfo.value.code == 403 - - with pytest.raises(HTTPError) as excinfo: - self._send_webhook_msg(ip, port, 'dummy-payload', content_len=-2) - assert excinfo.value.code == 500 - - # TODO: prevent urllib or the underlying from adding content-length - # with pytest.raises(HTTPError) as excinfo: - # self._send_webhook_msg(ip, port, 'dummy-payload', content_len=None) - # assert excinfo.value.code == 411 - - with pytest.raises(HTTPError): - self._send_webhook_msg(ip, port, 'dummy-payload', content_len='not-a-number') - assert excinfo.value.code == 500 - - finally: - updater.httpd.shutdown() - thr.join() - - def _send_webhook_msg(self, - ip, - port, - payload_str, - url_path='', - content_len=-1, - content_type='application/json', - get_method=None): - headers = {'content-type': content_type, } - - if not payload_str: - content_len = None - payload = None - else: - payload = bytes(payload_str, encoding='utf-8') - - if content_len == -1: - content_len = len(payload) - - if content_len is not None: - headers['content-length'] = str(content_len) - - url = 'http://{ip}:{port}/{path}'.format(ip=ip, port=port, path=url_path) - - req = Request(url, data=payload, headers=headers) - - if get_method is not None: - req.get_method = get_method - - return urlopen(req) - - def signal_sender(self, updater): - sleep(0.2) - while not updater.running: - sleep(0.2) - - os.kill(os.getpid(), signal.SIGTERM) - - def test_create_bot(self): - updater = Updater('123:abcd') - assert updater.bot is not None - - def test_mutual_exclude_token_bot(self): - bot = Bot('123:zyxw') - with pytest.raises(ValueError): - Updater(token='123:abcd', bot=bot) - - def test_no_token_or_bot_or_dispatcher(self): - with pytest.raises(ValueError): - Updater() - - def test_mutual_exclude_bot_private_key(self): - bot = Bot('123:zyxw') - with pytest.raises(ValueError): - Updater(bot=bot, private_key=b'key') - - def test_mutual_exclude_bot_dispatcher(self): - dispatcher = Dispatcher(None, None) - bot = Bot('123:zyxw') - with pytest.raises(ValueError): - Updater(bot=bot, dispatcher=dispatcher) - - def test_mutual_exclude_persistence_dispatcher(self): - dispatcher = Dispatcher(None, None) - persistence = DictPersistence() - with pytest.raises(ValueError): - Updater(dispatcher=dispatcher, persistence=persistence) - - def test_mutual_exclude_workers_dispatcher(self): - dispatcher = Dispatcher(None, None) - with pytest.raises(ValueError): - Updater(dispatcher=dispatcher, workers=8) - - def test_mutual_exclude_use_context_dispatcher(self): - dispatcher = Dispatcher(None, None) - use_context = not dispatcher.use_context - with pytest.raises(ValueError): - Updater(dispatcher=dispatcher, use_context=use_context) From 5f6074143265ce42d7136d5a641ab97f121c6c78 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 16 Jun 2020 23:42:44 +0200 Subject: [PATCH 34/62] fix bug --- tests/test_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 2d506c83454..a1d9d08f55d 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -167,7 +167,7 @@ def updates(*args, **kwargs): # case 2 if len(args) > 0: - self.attempts + self.attempts+=1 raise error class fakeUpdate(object): From 237858774ecb5da1975a89ee29d42d64ebe0ec7d Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 16 Jun 2020 23:45:01 +0200 Subject: [PATCH 35/62] fix bugg --- tests/test_updater.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_updater.py b/tests/test_updater.py index a1d9d08f55d..96df28ccbc3 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -155,6 +155,7 @@ def attempt(*args, **kwargs): def test_bootstrap_clean_bool(self, monkeypatch, updater, error, clean): clean = True retries =1 + self.attempts = 0 def attempt(*args, **kwargs): self.attempts += 1 From 233ba3c69e7afaaec03c75e80894d1fcb4634364 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 16 Jun 2020 23:46:59 +0200 Subject: [PATCH 36/62] remove old func --- tests/test_updater.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 96df28ccbc3..eeb7dd70651 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -157,10 +157,6 @@ def test_bootstrap_clean_bool(self, monkeypatch, updater, error, clean): retries =1 self.attempts = 0 - def attempt(*args, **kwargs): - self.attempts += 1 - raise error - def updates(*args, **kwargs): # we're hitting this func twice # 1. no args, expecting list of updates From cd7afcafecdcc25fdedfbce9063c5dcce085af43 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 00:03:03 +0200 Subject: [PATCH 37/62] test higher arg value --- tests/test_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index eeb7dd70651..bfa9fd6ee1c 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -148,7 +148,7 @@ def attempt(*args, **kwargs): assert self.attempts == attempts @pytest.mark.parametrize(('error', 'clean'), - argvalues=[(TelegramError(''), 2), + argvalues=[(TelegramError(''), 3), (Unauthorized(''), 1), (InvalidToken(), 1)], ids=('TelegramError', 'Unauthorized', 'InvalidToken')) From 2c8990e4c8e32072ff47c578f96aefb9994bea18 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 00:03:37 +0200 Subject: [PATCH 38/62] add catch for increased attempts var --- tests/test_updater.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_updater.py b/tests/test_updater.py index bfa9fd6ee1c..791c16a0d26 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -162,6 +162,10 @@ def updates(*args, **kwargs): # 1. no args, expecting list of updates # 2. with arg, int, expecting list args, delete all updates with updated_id < int + # case ??? + if self.attempts>0: + raise error + # case 2 if len(args) > 0: self.attempts+=1 From 641e1e7827465da471605f62954804d7df8b946b Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 00:08:36 +0200 Subject: [PATCH 39/62] change selector --- tests/test_updater.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 791c16a0d26..940c5395c98 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -163,12 +163,12 @@ def updates(*args, **kwargs): # 2. with arg, int, expecting list args, delete all updates with updated_id < int # case ??? - if self.attempts>0: - raise error + #~ if self.attempts>0: + #~ raise error # case 2 if len(args) > 0: - self.attempts+=1 + self.attempts=1 raise error class fakeUpdate(object): From 5b6f52293d83aa27535e718557e2a7cfc02cadd2 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 00:12:30 +0200 Subject: [PATCH 40/62] be specific on expected argument --- tests/test_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 940c5395c98..3256c6a2bd1 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -167,7 +167,7 @@ def updates(*args, **kwargs): #~ raise error # case 2 - if len(args) > 0: + if len(args) > 0 and isinstance(args[0], int) and args[0] == 3: self.attempts=1 raise error From 60dad8e834219fa18ac0bedda8ea938ea71c4d60 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 00:13:59 +0200 Subject: [PATCH 41/62] add safety against inf loop --- tests/test_updater.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 3256c6a2bd1..829968736ec 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -163,14 +163,18 @@ def updates(*args, **kwargs): # 2. with arg, int, expecting list args, delete all updates with updated_id < int # case ??? - #~ if self.attempts>0: - #~ raise error + if self.attempts>10: + raise error # case 2 - if len(args) > 0 and isinstance(args[0], int) and args[0] == 3: + if len(args) > 0 and isinstance(args[0], int) and args[0] == 4: self.attempts=1 raise error + if len(args) > 0: + self.attempts+=1 + print(args[0]) + class fakeUpdate(object): pass From 5bf813cf392855ee812216d61ce316f360e7aa20 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 00:27:39 +0200 Subject: [PATCH 42/62] change var name --- tests/test_updater.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 829968736ec..8dceb05e232 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -82,6 +82,7 @@ class TestUpdater(object): attempts = 0 err_handler_called = Event() cb_handler_called = Event() + update_id = 0 @pytest.fixture(autouse=True) def reset(self): @@ -154,25 +155,25 @@ def attempt(*args, **kwargs): ids=('TelegramError', 'Unauthorized', 'InvalidToken')) def test_bootstrap_clean_bool(self, monkeypatch, updater, error, clean): clean = True - retries =1 - self.attempts = 0 + expected_id = 4 # max 9 def updates(*args, **kwargs): # we're hitting this func twice - # 1. no args, expecting list of updates - # 2. with arg, int, expecting list args, delete all updates with updated_id < int + # 1. no args, return list of updates + # 2. with arg, int = 4, expecting list args, delete all updates with updated_id < int # case ??? - if self.attempts>10: + if self.update_id>10: raise error # case 2 - if len(args) > 0 and isinstance(args[0], int) and args[0] == 4: - self.attempts=1 + if len(args) > 0: + # we expect to get int(4) + self.update_id = int(arg[0]) raise error if len(args) > 0: - self.attempts+=1 + self.update_id+=1 print(args[0]) class fakeUpdate(object): @@ -180,9 +181,9 @@ class fakeUpdate(object): # case 1 # return list of dict's - i=0 + i=1 ls = [] - while i < 4: + while i < (expected_id): o = fakeUpdate() o.update_id = i ls.append(copy.deepcopy(o)) @@ -194,5 +195,5 @@ class fakeUpdate(object): updater.running = True with pytest.raises(type(error)): updater._bootstrap(retries, clean, None, None, bootstrap_interval=0) - assert self.attempts == retries + assert self.update_id == retries From cac4bf49a3b70a0ca8dd24331f6c1971d1f9cb6c Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 00:28:06 +0200 Subject: [PATCH 43/62] fix bug --- tests/test_updater.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 8dceb05e232..00ae2e0991b 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -169,7 +169,7 @@ def updates(*args, **kwargs): # case 2 if len(args) > 0: # we expect to get int(4) - self.update_id = int(arg[0]) + self.update_id = int(args[0]) raise error if len(args) > 0: @@ -194,6 +194,6 @@ class fakeUpdate(object): updater.running = True with pytest.raises(type(error)): - updater._bootstrap(retries, clean, None, None, bootstrap_interval=0) - assert self.update_id == retries + updater._bootstrap(1, clean, None, None, bootstrap_interval=0) + assert self.update_id == expected_id From 030b9ad9480d2d162476a0bc0fde86ed857602bc Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 00:30:16 +0200 Subject: [PATCH 44/62] test with clean = False --- tests/test_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 00ae2e0991b..a9d2a9da99a 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -154,7 +154,7 @@ def attempt(*args, **kwargs): (InvalidToken(), 1)], ids=('TelegramError', 'Unauthorized', 'InvalidToken')) def test_bootstrap_clean_bool(self, monkeypatch, updater, error, clean): - clean = True + clean = False expected_id = 4 # max 9 def updates(*args, **kwargs): From d3479c0ead17a5fbabea2b1f0ce4df657914e97b Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 00:33:10 +0200 Subject: [PATCH 45/62] test --- tests/test_updater.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_updater.py b/tests/test_updater.py index a9d2a9da99a..3e99be56b72 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -156,6 +156,7 @@ def attempt(*args, **kwargs): def test_bootstrap_clean_bool(self, monkeypatch, updater, error, clean): clean = False expected_id = 4 # max 9 + print(error) def updates(*args, **kwargs): # we're hitting this func twice From afe7b24c46486432522c2224a02913547f4fb6a6 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 00:36:07 +0200 Subject: [PATCH 46/62] test --- tests/test_updater.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 3e99be56b72..8d4d2468c84 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -83,6 +83,7 @@ class TestUpdater(object): err_handler_called = Event() cb_handler_called = Event() update_id = 0 + expected_id = 0 @pytest.fixture(autouse=True) def reset(self): @@ -154,9 +155,11 @@ def attempt(*args, **kwargs): (InvalidToken(), 1)], ids=('TelegramError', 'Unauthorized', 'InvalidToken')) def test_bootstrap_clean_bool(self, monkeypatch, updater, error, clean): - clean = False - expected_id = 4 # max 9 - print(error) + #~ print(clean) + clean = True + self.expected_id = 4 # max 9 + self.update_id = 0 + #~ print(error) def updates(*args, **kwargs): # we're hitting this func twice @@ -184,7 +187,7 @@ class fakeUpdate(object): # return list of dict's i=1 ls = [] - while i < (expected_id): + while i < (self.expected_id): o = fakeUpdate() o.update_id = i ls.append(copy.deepcopy(o)) @@ -196,5 +199,9 @@ class fakeUpdate(object): updater.running = True with pytest.raises(type(error)): updater._bootstrap(1, clean, None, None, bootstrap_interval=0) - assert self.update_id == expected_id + + print ('update-id: "%s"', self.update_id) + print ('expected-id: "%s"', self.expected_id) + + assert self.update_id == self.expected_id From 08a47171bd13cce2f08847e84562d42def21d30a Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:10:50 +0200 Subject: [PATCH 47/62] Revert "test" This reverts commit afe7b24c46486432522c2224a02913547f4fb6a6. --- tests/test_updater.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 8d4d2468c84..3e99be56b72 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -83,7 +83,6 @@ class TestUpdater(object): err_handler_called = Event() cb_handler_called = Event() update_id = 0 - expected_id = 0 @pytest.fixture(autouse=True) def reset(self): @@ -155,11 +154,9 @@ def attempt(*args, **kwargs): (InvalidToken(), 1)], ids=('TelegramError', 'Unauthorized', 'InvalidToken')) def test_bootstrap_clean_bool(self, monkeypatch, updater, error, clean): - #~ print(clean) - clean = True - self.expected_id = 4 # max 9 - self.update_id = 0 - #~ print(error) + clean = False + expected_id = 4 # max 9 + print(error) def updates(*args, **kwargs): # we're hitting this func twice @@ -187,7 +184,7 @@ class fakeUpdate(object): # return list of dict's i=1 ls = [] - while i < (self.expected_id): + while i < (expected_id): o = fakeUpdate() o.update_id = i ls.append(copy.deepcopy(o)) @@ -199,9 +196,5 @@ class fakeUpdate(object): updater.running = True with pytest.raises(type(error)): updater._bootstrap(1, clean, None, None, bootstrap_interval=0) - - print ('update-id: "%s"', self.update_id) - print ('expected-id: "%s"', self.expected_id) - - assert self.update_id == self.expected_id + assert self.update_id == expected_id From 0040c885cc6129f147a40c8518346d80a5580fa1 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:12:13 +0200 Subject: [PATCH 48/62] Revert "test with clean = False" This reverts commit 030b9ad9480d2d162476a0bc0fde86ed857602bc. --- tests/test_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 3e99be56b72..1b8f6b5f7a7 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -154,7 +154,7 @@ def attempt(*args, **kwargs): (InvalidToken(), 1)], ids=('TelegramError', 'Unauthorized', 'InvalidToken')) def test_bootstrap_clean_bool(self, monkeypatch, updater, error, clean): - clean = False + clean = True expected_id = 4 # max 9 print(error) From 97f3197d47ee809fb621e602d4c3f64fb29d64cb Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:14:50 +0200 Subject: [PATCH 49/62] Revert "test" This reverts commit d3479c0ead17a5fbabea2b1f0ce4df657914e97b. --- tests/test_updater.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 1b8f6b5f7a7..00ae2e0991b 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -156,7 +156,6 @@ def attempt(*args, **kwargs): def test_bootstrap_clean_bool(self, monkeypatch, updater, error, clean): clean = True expected_id = 4 # max 9 - print(error) def updates(*args, **kwargs): # we're hitting this func twice From 82f8ceeb2b7a5ae0160bb995e402e018407d3960 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:18:19 +0200 Subject: [PATCH 50/62] remove unused args --- tests/test_updater.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 00ae2e0991b..ee9b8c55990 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -148,12 +148,10 @@ def attempt(*args, **kwargs): updater._bootstrap(retries, False, 'path', None, bootstrap_interval=0) assert self.attempts == attempts - @pytest.mark.parametrize(('error', 'clean'), - argvalues=[(TelegramError(''), 3), - (Unauthorized(''), 1), - (InvalidToken(), 1)], - ids=('TelegramError', 'Unauthorized', 'InvalidToken')) - def test_bootstrap_clean_bool(self, monkeypatch, updater, error, clean): + @pytest.mark.parametrize(('error', ), + argvalues=[(TelegramError(''),)], + ids=('TelegramError', )) + def test_bootstrap_clean_bool(self, monkeypatch, updater, error): clean = True expected_id = 4 # max 9 From 92346a55616934ce75dfe7fed15be32fd627c088 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:20:13 +0200 Subject: [PATCH 51/62] add second function handler --- tests/test_updater.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/test_updater.py b/tests/test_updater.py index ee9b8c55990..27c44c7b9c5 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -155,6 +155,44 @@ def test_bootstrap_clean_bool(self, monkeypatch, updater, error): clean = True expected_id = 4 # max 9 + def updates(uid, *args, **kwargs): + # we're hitting this func twice + # 1. no args, return list of updates + # 2. with arg, int = 4, expecting list args, delete all updates with updated_id < int + + # case ??? + if uid: + print('uid: "%s"', uid) + raise error + + # case ??? + if self.update_id>10: + raise error + + # case 2 + if len(args) > 0: + # we expect to get int(4) + self.update_id = int(args[0]) + raise error + + if len(args) > 0: + self.update_id+=1 + print(args[0]) + + class fakeUpdate(object): + pass + + # case 1 + # return list of dict's + i=1 + ls = [] + while i < (expected_id): + o = fakeUpdate() + o.update_id = i + ls.append(copy.deepcopy(o)) + i+=1 + return ls + def updates(*args, **kwargs): # we're hitting this func twice # 1. no args, return list of updates From 9f5a8ce047e5a7554aa0e1e6cda6b618a553b222 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:20:56 +0200 Subject: [PATCH 52/62] test --- tests/test_updater.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 27c44c7b9c5..d34bcc892fb 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -205,6 +205,7 @@ def updates(*args, **kwargs): # case 2 if len(args) > 0: # we expect to get int(4) + print ("in here") self.update_id = int(args[0]) raise error @@ -231,5 +232,5 @@ class fakeUpdate(object): updater.running = True with pytest.raises(type(error)): updater._bootstrap(1, clean, None, None, bootstrap_interval=0) - assert self.update_id == expected_id + assert self.update_id == expected_id+1 From 178720228abed21620c412dd4d74df572a6dfe19 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:22:51 +0200 Subject: [PATCH 53/62] Revert "test" This reverts commit 9f5a8ce047e5a7554aa0e1e6cda6b618a553b222. --- tests/test_updater.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index d34bcc892fb..27c44c7b9c5 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -205,7 +205,6 @@ def updates(*args, **kwargs): # case 2 if len(args) > 0: # we expect to get int(4) - print ("in here") self.update_id = int(args[0]) raise error @@ -232,5 +231,5 @@ class fakeUpdate(object): updater.running = True with pytest.raises(type(error)): updater._bootstrap(1, clean, None, None, bootstrap_interval=0) - assert self.update_id == expected_id+1 + assert self.update_id == expected_id From 0c4208c56e2ae458007eac3be8aa9f53004be064 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:22:53 +0200 Subject: [PATCH 54/62] Revert "add second function handler" This reverts commit 92346a55616934ce75dfe7fed15be32fd627c088. --- tests/test_updater.py | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 27c44c7b9c5..ee9b8c55990 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -155,44 +155,6 @@ def test_bootstrap_clean_bool(self, monkeypatch, updater, error): clean = True expected_id = 4 # max 9 - def updates(uid, *args, **kwargs): - # we're hitting this func twice - # 1. no args, return list of updates - # 2. with arg, int = 4, expecting list args, delete all updates with updated_id < int - - # case ??? - if uid: - print('uid: "%s"', uid) - raise error - - # case ??? - if self.update_id>10: - raise error - - # case 2 - if len(args) > 0: - # we expect to get int(4) - self.update_id = int(args[0]) - raise error - - if len(args) > 0: - self.update_id+=1 - print(args[0]) - - class fakeUpdate(object): - pass - - # case 1 - # return list of dict's - i=1 - ls = [] - while i < (expected_id): - o = fakeUpdate() - o.update_id = i - ls.append(copy.deepcopy(o)) - i+=1 - return ls - def updates(*args, **kwargs): # we're hitting this func twice # 1. no args, return list of updates From 204333520045a284743a7556e19a2692f4f8e5a3 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:30:48 +0200 Subject: [PATCH 55/62] add comment, create inf loop protection --- tests/test_updater.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index ee9b8c55990..7ead60140dc 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -153,16 +153,16 @@ def attempt(*args, **kwargs): ids=('TelegramError', )) def test_bootstrap_clean_bool(self, monkeypatch, updater, error): clean = True - expected_id = 4 # max 9 + expected_id = 4 # max 9 otherwise we hit our inf loop protection def updates(*args, **kwargs): # we're hitting this func twice # 1. no args, return list of updates # 2. with arg, int = 4, expecting list args, delete all updates with updated_id < int - # case ??? + # case inf loop protection if self.update_id>10: - raise error + raise ValueError # case 2 if len(args) > 0: @@ -170,15 +170,18 @@ def updates(*args, **kwargs): self.update_id = int(args[0]) raise error - if len(args) > 0: - self.update_id+=1 - print(args[0]) - class fakeUpdate(object): pass # case 1 - # return list of dict's + # return list of obj's + + # inf loop protection + self.update_id+=1 + + # build list of fake updates + # results in list of 3 objects with + # update_id's 1, 2 and 3 i=1 ls = [] while i < (expected_id): From e4140d91ee7811a6576d24739998a416f6a76a67 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:32:54 +0200 Subject: [PATCH 56/62] improve comment --- tests/test_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 7ead60140dc..94e92b99ec8 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -158,7 +158,7 @@ def test_bootstrap_clean_bool(self, monkeypatch, updater, error): def updates(*args, **kwargs): # we're hitting this func twice # 1. no args, return list of updates - # 2. with arg, int = 4, expecting list args, delete all updates with updated_id < int + # 2. with arg, int => if int == expected_id => test successful # case inf loop protection if self.update_id>10: From 0e96ed518ee636a1812de9bdea25898c8f46fd9a Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:33:12 +0200 Subject: [PATCH 57/62] test inf loop protection --- tests/test_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 94e92b99ec8..e7216a7ad3f 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -165,7 +165,7 @@ def updates(*args, **kwargs): raise ValueError # case 2 - if len(args) > 0: + if len(args) > 0 and int(args[0]) > 10: # we expect to get int(4) self.update_id = int(args[0]) raise error From d06ecf9d9615e1004240f3eccf2be714b614ab9e Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:34:10 +0200 Subject: [PATCH 58/62] Revert "test inf loop protection" This reverts commit 0e96ed518ee636a1812de9bdea25898c8f46fd9a. --- tests/test_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index e7216a7ad3f..94e92b99ec8 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -165,7 +165,7 @@ def updates(*args, **kwargs): raise ValueError # case 2 - if len(args) > 0 and int(args[0]) > 10: + if len(args) > 0: # we expect to get int(4) self.update_id = int(args[0]) raise error From 534ec262cf41116cb45fbc2a1809b698392eaae7 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:38:00 +0200 Subject: [PATCH 59/62] improve comment --- tests/test_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index 94e92b99ec8..17e99f84059 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -158,7 +158,7 @@ def test_bootstrap_clean_bool(self, monkeypatch, updater, error): def updates(*args, **kwargs): # we're hitting this func twice # 1. no args, return list of updates - # 2. with arg, int => if int == expected_id => test successful + # 2. with 1 arg, int => if int == expected_id => test successful # case inf loop protection if self.update_id>10: From fb082a7e38247c4ad5ad4d6302776830dcbe85eb Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:38:30 +0200 Subject: [PATCH 60/62] set var to 0, just in case --- tests/test_updater.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_updater.py b/tests/test_updater.py index 17e99f84059..e5b80053682 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -154,6 +154,7 @@ def attempt(*args, **kwargs): def test_bootstrap_clean_bool(self, monkeypatch, updater, error): clean = True expected_id = 4 # max 9 otherwise we hit our inf loop protection + self.update_id = 0 def updates(*args, **kwargs): # we're hitting this func twice From 7dce85dcd48616879c19dd190e5f85172a15997f Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 09:39:41 +0200 Subject: [PATCH 61/62] improve comment --- tests/test_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index e5b80053682..f86b1eb3548 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -181,7 +181,7 @@ class fakeUpdate(object): self.update_id+=1 # build list of fake updates - # results in list of 3 objects with + # returns list of 3 objects with # update_id's 1, 2 and 3 i=1 ls = [] From e1144a966186a7460d772a7e0410628020d02bf8 Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Wed, 17 Jun 2020 17:17:09 +0200 Subject: [PATCH 62/62] remove deep copy --- tests/test_updater.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index f86b1eb3548..9c400853f9e 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -21,7 +21,6 @@ import signal import sys import asyncio -import copy from flaky import flaky from functools import partial from queue import Queue @@ -188,7 +187,7 @@ class fakeUpdate(object): while i < (expected_id): o = fakeUpdate() o.update_id = i - ls.append(copy.deepcopy(o)) + ls.append(o) i+=1 return ls