summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSulabh Katila <sulabhkatila@gmail.com>2024-02-21 19:51:58 -0500
committerGitHub <noreply@github.com>2024-02-21 21:51:58 -0300
commiteceb5e2eea554ea0f9baf7abc1b1972459854cef (patch)
tree5327cc03f0462e3c2c3feeab939bf698534f95a9 /tests
parent6feaad9113fd38ba3970032d2b7856c77403e29e (diff)
Fixed #34806 -- Made cached_db session backend resilient to cache write errors.
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/cache/failing_cache.py7
-rw-r--r--tests/sessions_tests/tests.py16
2 files changed, 23 insertions, 0 deletions
diff --git a/tests/cache/failing_cache.py b/tests/cache/failing_cache.py
new file mode 100644
index 0000000000..e2f0043bb7
--- /dev/null
+++ b/tests/cache/failing_cache.py
@@ -0,0 +1,7 @@
+from django.core.cache.backends.locmem import LocMemCache
+
+
+class CacheClass(LocMemCache):
+
+ def set(self, *args, **kwargs):
+ raise Exception("Faked exception saving to cache")
diff --git a/tests/sessions_tests/tests.py b/tests/sessions_tests/tests.py
index 7e0677d08d..c8c556b4dc 100644
--- a/tests/sessions_tests/tests.py
+++ b/tests/sessions_tests/tests.py
@@ -517,6 +517,22 @@ class CacheDBSessionTests(SessionTestsMixin, TestCase):
with self.assertRaises(InvalidCacheBackendError):
self.backend()
+ @override_settings(
+ CACHES={"default": {"BACKEND": "cache.failing_cache.CacheClass"}}
+ )
+ def test_cache_set_failure_non_fatal(self):
+ """Failing to write to the cache does not raise errors."""
+ session = self.backend()
+ session["key"] = "val"
+
+ with self.assertLogs("django.contrib.sessions", "ERROR") as cm:
+ session.save()
+
+ # A proper ERROR log message was recorded.
+ log = cm.records[-1]
+ self.assertEqual(log.message, f"Error saving to cache ({session._cache})")
+ self.assertEqual(str(log.exc_info[1]), "Faked exception saving to cache")
+
@override_settings(USE_TZ=True)
class CacheDBSessionWithTimeZoneTests(CacheDBSessionTests):