From 9b76d465583b4811511719ae2f761a813779f66f Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 3 Apr 2024 18:50:20 +0200 Subject: [PATCH] refinements and fixes --- chatmaild/src/chatmaild/notifier.py | 6 +++++- chatmaild/src/chatmaild/tests/test_metadata.py | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/chatmaild/src/chatmaild/notifier.py b/chatmaild/src/chatmaild/notifier.py index 849d70a..2f49fe1 100644 --- a/chatmaild/src/chatmaild/notifier.py +++ b/chatmaild/src/chatmaild/notifier.py @@ -50,6 +50,7 @@ class PersistentQueueItem: @classmethod def create(cls, queue_dir, addr, start_ts, token): queue_id = uuid4().hex + start_ts = int(start_ts) path = queue_dir.joinpath(queue_id) tmp_path = path.with_name(path.name + ".tmp") tmp_path.write_text(f"{addr}\n{start_ts}\n{token}") @@ -61,6 +62,9 @@ class PersistentQueueItem: addr, start_ts, token = path.read_text().split("\n", maxsplit=2) return cls(path, addr, int(start_ts), token) + def __lt__(self, other): + return self.start_ts < other.start_ts + class Notifier: URL = "https://notifications.delta.chat/notify" @@ -95,7 +99,7 @@ class Notifier: def queue_for_retry(self, queue_item, retry_num=0): delay = self.compute_delay(retry_num) - when = time.time() + delay + when = int(time.time()) + delay deadline = queue_item.start_ts + self.DROP_DEADLINE if retry_num >= len(self.retry_queues) or when > deadline: queue_item.delete() diff --git a/chatmaild/src/chatmaild/tests/test_metadata.py b/chatmaild/src/chatmaild/tests/test_metadata.py index d7d156b..23028d8 100644 --- a/chatmaild/src/chatmaild/tests/test_metadata.py +++ b/chatmaild/src/chatmaild/tests/test_metadata.py @@ -238,7 +238,7 @@ def test_requeue_removes_tmp_files(notifier, metadata, testaddr, caplog): assert not p.exists() assert notifier2.retry_queues[0].qsize() == 1 when, queue_item = notifier2.retry_queues[0].get() - assert when >= int(time.time()) + assert when <= int(time.time()) assert queue_item.addr == testaddr @@ -295,3 +295,4 @@ def test_persistent_queue_items(tmp_path, testaddr, token): assert item2 == queue_item item2.delete() assert not item2.path.exists() + assert not queue_item < item2 and not item2 < queue_item