diff --git a/chatmaild/src/chatmaild/metadata.py b/chatmaild/src/chatmaild/metadata.py index 1030715..21f1054 100644 --- a/chatmaild/src/chatmaild/metadata.py +++ b/chatmaild/src/chatmaild/metadata.py @@ -90,12 +90,12 @@ class Notifier: while True: self.thread_retry_one(requests_session, numtries) - def thread_retry_one(self, requests_session, numtries): + def thread_retry_one(self, requests_session, numtries, sleepfunc=time.sleep): retry_queue = self.retry_queues[numtries] when, token = retry_queue.get() wait_time = when - time.time() if wait_time > 0: - time.sleep(wait_time) + sleepfunc(wait_time) self.notify_one(requests_session, token, numtries) def notify_one(self, requests_session, token, numtries=0): diff --git a/chatmaild/src/chatmaild/tests/test_metadata.py b/chatmaild/src/chatmaild/tests/test_metadata.py index 823d447..3f7a55f 100644 --- a/chatmaild/src/chatmaild/tests/test_metadata.py +++ b/chatmaild/src/chatmaild/tests/test_metadata.py @@ -190,14 +190,26 @@ def test_notifier_thread_run(notifier, testaddr): @pytest.mark.parametrize("status", [requests.exceptions.RequestException(), 404, 500]) -def test_notifier_thread_connection_failures(notifier, testaddr, status): +def test_notifier_thread_connection_failures(notifier, testaddr, status, caplog): + """ test that tokens keep getting retried until they are given up. """ notifier.add_token(testaddr, "01234") notifier.new_message_for_addr(testaddr) - reqmock = get_mocked_requests([status]) - notifier.NOTIFICATION_RETRY_DELAY = 0.1 - notifier.thread_retry_one(reqmock, numtries=0) - assert notifier.retry_queues[1].get()[1] == "01234" - assert notifier.retry_queues[0].qsize() == 0 + notifier.NOTIFICATION_RETRY_DELAY = 5 + for i in range(notifier.MAX_NUMBER_OF_TRIES): + caplog.clear() + reqmock = get_mocked_requests([status]) + sleep_calls = [] + notifier.thread_retry_one(reqmock, numtries=i, sleepfunc=sleep_calls.append) + assert notifier.retry_queues[i].qsize() == 0 + assert "request failed" in caplog.records[0].msg + if i > 0: + assert len(sleep_calls) == 1 + if i + 1 < notifier.MAX_NUMBER_OF_TRIES: + assert notifier.retry_queues[i + 1].qsize() == 1 + assert len(caplog.records) == 1 + else: + assert len(caplog.records) == 2 + assert "giving up" in caplog.records[1].msg def test_multi_device_notifier(notifier, testaddr):