Files
mtusi2026lab5/student-tests/test_retry_policy.py
T
2026-11-24 17:58:42 +03:00

51 lines
1.9 KiB
Python

import unittest
from unittest.mock import Mock, patch
from src.worker import handle_message, retry_delay
class RetryPolicyTests(unittest.TestCase):
def test_exponential_backoff_is_jittered_within_attempt_window(self):
with patch("src.worker.RETRY_BASE_SECONDS", 1), patch("src.worker.RETRY_MAX_SECONDS", 60):
delay = retry_delay(4)
self.assertGreaterEqual(delay, 4)
self.assertLessEqual(delay, 8)
def test_backoff_respects_configured_cap(self):
with patch("src.worker.RETRY_BASE_SECONDS", 1), patch("src.worker.RETRY_MAX_SECONDS", 20):
delay = retry_delay(10)
self.assertGreaterEqual(delay, 10)
self.assertLessEqual(delay, 20)
def test_malformed_job_is_dead_lettered_without_processing(self):
channel = Mock()
body = b'{"schema_version":1,"type":"attachment.process.v1","job_id":"bad","attachment_id":"bad","pipeline_version":1,"attempt":1,"created_at":"now"}'
with patch("src.worker.process_with_timeout") as process:
handle_message(channel, 7, body)
channel.basic_nack.assert_called_once_with(delivery_tag=7, requeue=False)
process.assert_not_called()
def test_valid_job_is_acknowledged_after_processing(self):
channel = Mock()
job_id = "1b671a64-40d5-491e-99b0-da01ff1f3341"
attachment_id = "0e8c4c49-6d3e-4d3e-8ca0-cf4ed9a36e9a"
body = (
'{"schema_version":1,"type":"attachment.process.v1",'
f'"job_id":"{job_id}","attachment_id":"{attachment_id}",'
'"pipeline_version":1,"attempt":1,"created_at":"2026-01-01T00:00:00Z"}'
).encode()
with patch("src.worker.process_with_timeout"):
handle_message(channel, 8, body)
channel.basic_ack.assert_called_once_with(delivery_tag=8)
channel.basic_nack.assert_not_called()
if __name__ == "__main__":
unittest.main()