From a9153302a3e641aa47de2ffcd9c8036a933a01b1 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 4 Oct 2023 12:57:56 -0700 Subject: [PATCH] URLFile: don't cache non-existent file's lengths (#30071) * urlfile empty * simplify with mock * better test name * PR cleanup * cleanup the length file old-commit-hash: fa51bbc2364c1024097dc085afb5982c42f9c524 --- tools/lib/tests/test_caching.py | 34 +++++++++++++++++++++++++++++++++ tools/lib/url_file.py | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/tools/lib/tests/test_caching.py b/tools/lib/tests/test_caching.py index b893d9815d..73ed843869 100755 --- a/tools/lib/tests/test_caching.py +++ b/tools/lib/tests/test_caching.py @@ -1,6 +1,12 @@ #!/usr/bin/env python3 import os import unittest + +from pathlib import Path +from parameterized import parameterized +from unittest import mock + +from openpilot.system.hardware.hw import Paths from openpilot.tools.lib.url_file import URLFile @@ -59,6 +65,34 @@ class TestFileDownload(unittest.TestCase): self.compare_loads(large_file_url, length - 100, 100) self.compare_loads(large_file_url) + @parameterized.expand([(True, ), (False, )]) + def test_recover_from_missing_file(self, cache_enabled): + os.environ["FILEREADER_CACHE"] = "1" if cache_enabled else "0" + + file_url = "http://localhost:5001/test.png" + + file_exists = False + + def get_length_online_mock(self): + if file_exists: + return 4 + return -1 + + patch_length = mock.patch.object(URLFile, "get_length_online", get_length_online_mock) + patch_length.start() + try: + length = URLFile(file_url).get_length() + self.assertEqual(length, -1) + + file_exists = True + length = URLFile(file_url).get_length() + self.assertEqual(length, 4) + finally: + tempfile_length = Path(Paths.download_cache_root()) / "ba2119904385654cb0105a2da174875f8e7648db175f202ecae6d6428b0e838f_length" + if tempfile_length.exists(): + tempfile_length.unlink() + patch_length.stop() + if __name__ == "__main__": unittest.main() diff --git a/tools/lib/url_file.py b/tools/lib/url_file.py index 98a52cae27..7612996223 100644 --- a/tools/lib/url_file.py +++ b/tools/lib/url_file.py @@ -73,7 +73,7 @@ class URLFile: return self._length self._length = self.get_length_online() - if not self._force_download: + if not self._force_download and self._length != -1: with atomic_write_in_dir(file_length_path, mode="w") as file_length: file_length.write(str(self._length)) return self._length