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: fa51bbc236
laptop
Justin Newberry 2 years ago committed by GitHub
parent 26d822b661
commit a9153302a3
  1. 34
      tools/lib/tests/test_caching.py
  2. 2
      tools/lib/url_file.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()

@ -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

Loading…
Cancel
Save