mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-06-30 02:52:04 +08:00
Tests: test_caching reduce global state (#29681)
old-commit-hash: dd9b0231c696607c14a8c10adeeb4d047c5ff782
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import tempfile
|
||||
|
||||
from unittest import mock
|
||||
|
||||
def temporary_cache_dir(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
cache_dir_patch = mock.patch("openpilot.tools.lib.url_file.CACHE_DIR", temp_dir)
|
||||
cache_dir_patch.start()
|
||||
func(*args, **kwargs)
|
||||
cache_dir_patch.stop()
|
||||
return wrapper
|
||||
@@ -1,18 +1,14 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import shutil
|
||||
import unittest
|
||||
|
||||
os.environ["COMMA_CACHE"] = "/tmp/__test_cache__"
|
||||
from openpilot.tools.lib.url_file import URLFile, CACHE_DIR
|
||||
from openpilot.tools.lib.url_file import URLFile
|
||||
from tools.lib.tests import temporary_cache_dir
|
||||
|
||||
|
||||
class TestFileDownload(unittest.TestCase):
|
||||
|
||||
def compare_loads(self, url, start=0, length=None):
|
||||
"""Compares range between cached and non cached version"""
|
||||
shutil.rmtree(CACHE_DIR)
|
||||
|
||||
file_cached = URLFile(url, cache=True)
|
||||
file_downloaded = URLFile(url, cache=False)
|
||||
|
||||
@@ -35,6 +31,7 @@ class TestFileDownload(unittest.TestCase):
|
||||
self.assertEqual(file_cached.get_length(), file_downloaded.get_length())
|
||||
self.assertEqual(response_cached, response_downloaded)
|
||||
|
||||
@temporary_cache_dir
|
||||
def test_small_file(self):
|
||||
# Make sure we don't force cache
|
||||
os.environ["FILEREADER_CACHE"] = "0"
|
||||
@@ -55,6 +52,7 @@ class TestFileDownload(unittest.TestCase):
|
||||
for i in range(length // 100):
|
||||
self.compare_loads(small_file_url, 100 * i, 100)
|
||||
|
||||
@temporary_cache_dir
|
||||
def test_large_file(self):
|
||||
large_file_url = "https://commadataci.blob.core.windows.net/openpilotci/0375fdf7b1ce594d/2019-06-13--08-32-25/3/qlog.bz2"
|
||||
# Load the end 100 bytes of both files
|
||||
|
||||
Reference in New Issue
Block a user