|
| 1 | +"""Tests for download retry behavior.""" |
| 2 | + |
| 3 | +__author__ = "Tom Goetz" |
| 4 | +__copyright__ = "Copyright Tom Goetz" |
| 5 | +__license__ = "GPL" |
| 6 | + |
| 7 | +import datetime |
| 8 | +import unittest |
| 9 | +from unittest import mock |
| 10 | + |
| 11 | +from garmindb.download import Download |
| 12 | + |
| 13 | + |
| 14 | +class FakeConfig: |
| 15 | + """Minimal config stub for Download tests.""" |
| 16 | + |
| 17 | + def get_session_file(self): |
| 18 | + return "/tmp/garth_session" |
| 19 | + |
| 20 | + def get_garmin_base_domain(self): |
| 21 | + return "garmin.com" |
| 22 | + |
| 23 | + |
| 24 | +class TestDownload(unittest.TestCase): |
| 25 | + def setUp(self): |
| 26 | + self.download = Download(FakeConfig()) |
| 27 | + |
| 28 | + def test_get_stat_retries_until_success(self): |
| 29 | + attempts = [] |
| 30 | + |
| 31 | + def flaky_stat(directory, day, overwrite): |
| 32 | + attempts.append((directory, day, overwrite)) |
| 33 | + if len(attempts) < 3: |
| 34 | + raise RuntimeError("temporary HRV gap") |
| 35 | + |
| 36 | + start_date = datetime.date(2026, 3, 1) |
| 37 | + with mock.patch("garmindb.download.time.sleep"): |
| 38 | + self.download._Download__get_stat(flaky_stat, "/tmp/hrv", start_date, 1, False) |
| 39 | + |
| 40 | + self.assertEqual(len(attempts), 3) |
| 41 | + self.assertEqual(attempts[0][0], "/tmp/hrv") |
| 42 | + self.assertEqual(attempts[0][1], start_date) |
| 43 | + |
| 44 | + def test_get_stat_stops_after_five_failures(self): |
| 45 | + attempts = [] |
| 46 | + |
| 47 | + def always_fail(directory, day, overwrite): |
| 48 | + attempts.append((directory, day, overwrite)) |
| 49 | + raise RuntimeError("persistent HRV gap") |
| 50 | + |
| 51 | + start_date = datetime.date(2026, 3, 1) |
| 52 | + with mock.patch("garmindb.download.time.sleep"): |
| 53 | + self.download._Download__get_stat(always_fail, "/tmp/hrv", start_date, 1, False) |
| 54 | + |
| 55 | + self.assertEqual(len(attempts), 5) |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + unittest.main(verbosity=2) |
0 commit comments