|
| 1 | +"""Tests for LocalFileMove retry limit functionality""" |
| 2 | +import unittest |
| 3 | +from unittest.mock import patch, MagicMock |
| 4 | +from queue import Queue |
| 5 | + |
| 6 | +from helioviewer.hvpull.downloader.localmove import LocalFileMove, MAX_RETRY_ATTEMPTS |
| 7 | + |
| 8 | + |
| 9 | +class TestLocalFileMoveRetry(unittest.TestCase): |
| 10 | + """Test that file move retries are limited to MAX_RETRY_ATTEMPTS""" |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + self.queue = Queue() |
| 14 | + self.mover = LocalFileMove("/tmp/incoming", self.queue) |
| 15 | + |
| 16 | + @patch('shutil.move') |
| 17 | + @patch('os.path.exists', return_value=True) |
| 18 | + def test_successful_move_does_not_retry(self, mock_exists, mock_move): |
| 19 | + """A successful move should not add anything to the queue""" |
| 20 | + self.mover.process(["server1", 100, "/source/file.jp2"]) |
| 21 | + |
| 22 | + mock_move.assert_called_once() |
| 23 | + self.assertTrue(self.queue.empty()) |
| 24 | + |
| 25 | + @patch('shutil.move', side_effect=IOError("File busy")) |
| 26 | + @patch('os.path.exists', return_value=True) |
| 27 | + def test_failed_move_retries_up_to_max(self, mock_exists, mock_move): |
| 28 | + """A failing move should retry MAX_RETRY_ATTEMPTS times then give up""" |
| 29 | + # First attempt (retry_count=0) |
| 30 | + self.mover.process(["server1", 100, "/source/file.jp2"]) |
| 31 | + |
| 32 | + # Should be re-queued with retry_count=1 |
| 33 | + self.assertEqual(self.queue.qsize(), 1) |
| 34 | + item = self.queue.get() |
| 35 | + self.assertEqual(item[3], 1) # retry_count = 1 |
| 36 | + |
| 37 | + # Second attempt (retry_count=1) |
| 38 | + self.mover.process(item) |
| 39 | + item = self.queue.get() |
| 40 | + self.assertEqual(item[3], 2) # retry_count = 2 |
| 41 | + |
| 42 | + # Third attempt (retry_count=2) |
| 43 | + self.mover.process(item) |
| 44 | + item = self.queue.get() |
| 45 | + self.assertEqual(item[3], 3) # retry_count = 3 |
| 46 | + |
| 47 | + # Fourth attempt (retry_count=3) - should give up, not re-queue |
| 48 | + self.mover.process(item) |
| 49 | + self.assertTrue(self.queue.empty(), "Should not retry after MAX_RETRY_ATTEMPTS") |
| 50 | + |
| 51 | + def test_max_retry_attempts_is_three(self): |
| 52 | + """Verify the constant is set to 3""" |
| 53 | + self.assertEqual(MAX_RETRY_ATTEMPTS, 3) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == '__main__': |
| 57 | + unittest.main() |
0 commit comments