1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Improved cache.tests.FileBasedCacheTests.test_touch to avoid flakiness due to slow file system access.

This commit is contained in:
Natalia 2023-12-15 13:50:05 -03:00 committed by Mariusz Felisiak
parent 2a2481f5a2
commit 7c26dbf6ef

22
tests/cache/tests.py vendored
View File

@ -1766,6 +1766,28 @@ class FileBasedCacheTests(BaseCacheTests, TestCase):
self.assertIs(cache.has_key("key"), False)
mocked_open.assert_called_once()
def test_touch(self):
"""Override to manually advance time since file access can be slow."""
class ManualTickingTime:
def __init__(self):
# Freeze time, calling `sleep` will manually advance it.
self._time = time.time()
def time(self):
return self._time
def sleep(self, seconds):
self._time += seconds
mocked_time = ManualTickingTime()
with (
mock.patch("django.core.cache.backends.filebased.time", new=mocked_time),
mock.patch("django.core.cache.backends.base.time", new=mocked_time),
mock.patch("cache.tests.time", new=mocked_time),
):
super().test_touch()
@unittest.skipUnless(RedisCache_params, "Redis backend not configured")
@override_settings(