mirror of
https://github.com/chatmail/relay.git
synced 2026-05-18 20:08:21 +00:00
some refinements and extending the tests
This commit is contained in:
@@ -75,11 +75,14 @@ class Notifier:
|
|||||||
when += pow(self.NOTIFICATION_RETRY_DELAY, numtries)
|
when += pow(self.NOTIFICATION_RETRY_DELAY, numtries)
|
||||||
self.retry_queues[numtries].put((when, token))
|
self.retry_queues[numtries].put((when, token))
|
||||||
|
|
||||||
def start_notification_threads(self):
|
def requeue_persistent_pending_tokens(self):
|
||||||
for token_path in self.notification_dir.iterdir():
|
for token_path in self.notification_dir.iterdir():
|
||||||
self.add_token_for_retry(token_path.name)
|
self.add_token_for_retry(token_path.name)
|
||||||
|
|
||||||
# we start a thread for each retry-queue bucket
|
def start_notification_threads(self):
|
||||||
|
self.requeue_persistent_pending_tokens()
|
||||||
|
|
||||||
|
# start a thread for each retry-queue bucket
|
||||||
for numtries in range(len(self.retry_queues)):
|
for numtries in range(len(self.retry_queues)):
|
||||||
t = Thread(target=self.thread_retry_loop, args=(numtries,))
|
t = Thread(target=self.thread_retry_loop, args=(numtries,))
|
||||||
t.setDaemon(True)
|
t.setDaemon(True)
|
||||||
@@ -99,6 +102,7 @@ class Notifier:
|
|||||||
self.notify_one(requests_session, token, numtries)
|
self.notify_one(requests_session, token, numtries)
|
||||||
|
|
||||||
def notify_one(self, requests_session, token, numtries=0):
|
def notify_one(self, requests_session, token, numtries=0):
|
||||||
|
token_path = self.notification_dir.joinpath(token)
|
||||||
try:
|
try:
|
||||||
response = requests_session.post(
|
response = requests_session.post(
|
||||||
"https://notifications.delta.chat/notify",
|
"https://notifications.delta.chat/notify",
|
||||||
@@ -109,15 +113,12 @@ class Notifier:
|
|||||||
response = e
|
response = e
|
||||||
else:
|
else:
|
||||||
if response.status_code in (200, 410):
|
if response.status_code in (200, 410):
|
||||||
token_path = self.notification_dir.joinpath(token)
|
|
||||||
if response.status_code == 410:
|
if response.status_code == 410:
|
||||||
# 410 Gone: means the token is no longer valid.
|
# 410 Gone: means the token is no longer valid.
|
||||||
try:
|
try:
|
||||||
addr = token_path.read_text()
|
addr = token_path.read_text()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
logging.warning(
|
logging.warning("no address for token %r:", token)
|
||||||
"could not determine address for token %r:", token
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
self.remove_token(addr, token)
|
self.remove_token(addr, token)
|
||||||
token_path.unlink(missing_ok=True)
|
token_path.unlink(missing_ok=True)
|
||||||
@@ -128,6 +129,7 @@ class Notifier:
|
|||||||
if numtries < self.MAX_NUMBER_OF_TRIES:
|
if numtries < self.MAX_NUMBER_OF_TRIES:
|
||||||
self.add_token_for_retry(token, numtries=numtries)
|
self.add_token_for_retry(token, numtries=numtries)
|
||||||
else:
|
else:
|
||||||
|
token_path.unlink(missing_ok=True)
|
||||||
logging.warning(
|
logging.warning(
|
||||||
"giving up on token after %d tries: %r", numtries - 1, token
|
"giving up on token after %d tries: %r", numtries - 1, token
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -177,6 +177,8 @@ def test_notifier_thread_firstrun(notifier, testaddr):
|
|||||||
url, data, timeout = reqmock.requests[0]
|
url, data, timeout = reqmock.requests[0]
|
||||||
assert data == "01234"
|
assert data == "01234"
|
||||||
assert notifier.get_tokens(testaddr) == ["01234"]
|
assert notifier.get_tokens(testaddr) == ["01234"]
|
||||||
|
notifier.requeue_persistent_pending_tokens()
|
||||||
|
assert notifier.retry_queues[0].qsize() == 0
|
||||||
|
|
||||||
|
|
||||||
def test_notifier_thread_run(notifier, testaddr):
|
def test_notifier_thread_run(notifier, testaddr):
|
||||||
@@ -187,6 +189,8 @@ def test_notifier_thread_run(notifier, testaddr):
|
|||||||
url, data, timeout = reqmock.requests[0]
|
url, data, timeout = reqmock.requests[0]
|
||||||
assert data == "01234"
|
assert data == "01234"
|
||||||
assert notifier.get_tokens(testaddr) == ["01234"]
|
assert notifier.get_tokens(testaddr) == ["01234"]
|
||||||
|
notifier.requeue_persistent_pending_tokens()
|
||||||
|
assert notifier.retry_queues[0].qsize() == 0
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("status", [requests.exceptions.RequestException(), 404, 500])
|
@pytest.mark.parametrize("status", [requests.exceptions.RequestException(), 404, 500])
|
||||||
@@ -210,6 +214,8 @@ def test_notifier_thread_connection_failures(notifier, testaddr, status, caplog)
|
|||||||
else:
|
else:
|
||||||
assert len(caplog.records) == 2
|
assert len(caplog.records) == 2
|
||||||
assert "giving up" in caplog.records[1].msg
|
assert "giving up" in caplog.records[1].msg
|
||||||
|
notifier.requeue_persistent_pending_tokens()
|
||||||
|
assert notifier.retry_queues[0].qsize() == 0
|
||||||
|
|
||||||
|
|
||||||
def test_multi_device_notifier(notifier, testaddr):
|
def test_multi_device_notifier(notifier, testaddr):
|
||||||
|
|||||||
Reference in New Issue
Block a user