authentik.root.tests.test_cache
in_memory_cache tests
1"""in_memory_cache tests""" 2 3from unittest.mock import patch 4 5from django.test import TestCase 6 7from authentik.root.cache import in_memory_cache 8 9 10class TestInMemoryCache(TestCase): 11 def test_stale_while_revalidate(self): 12 """Fresh values are cached, stale values are served while refreshed in the background""" 13 calls = [] 14 cache = in_memory_cache(key=lambda x: x, timeout=10) 15 16 @cache 17 def counter(x): 18 calls.append(x) 19 return len(calls) 20 21 with patch("authentik.root.cache.monotonic", return_value=0): 22 self.assertEqual(counter(1), 1) 23 self.assertEqual(counter(1), 1) 24 with patch("authentik.root.cache.monotonic", return_value=100): 25 # stale value returned immediately, refreshed once in the background 26 self.assertEqual(counter(1), 1) 27 counter(1) 28 # single worker runs tasks in order, so this waits for queued refreshes 29 cache._executor.submit(lambda: None).result() 30 self.assertEqual(counter(1), 2) 31 self.assertEqual(len(calls), 2) 32 counter.cache_clear() 33 self.assertEqual(counter(1), 3)
class
TestInMemoryCache(django.test.testcases.TestCase):
11class TestInMemoryCache(TestCase): 12 def test_stale_while_revalidate(self): 13 """Fresh values are cached, stale values are served while refreshed in the background""" 14 calls = [] 15 cache = in_memory_cache(key=lambda x: x, timeout=10) 16 17 @cache 18 def counter(x): 19 calls.append(x) 20 return len(calls) 21 22 with patch("authentik.root.cache.monotonic", return_value=0): 23 self.assertEqual(counter(1), 1) 24 self.assertEqual(counter(1), 1) 25 with patch("authentik.root.cache.monotonic", return_value=100): 26 # stale value returned immediately, refreshed once in the background 27 self.assertEqual(counter(1), 1) 28 counter(1) 29 # single worker runs tasks in order, so this waits for queued refreshes 30 cache._executor.submit(lambda: None).result() 31 self.assertEqual(counter(1), 2) 32 self.assertEqual(len(calls), 2) 33 counter.cache_clear() 34 self.assertEqual(counter(1), 3)
Similar to TransactionTestCase, but use transaction.atomic() to achieve
test isolation.
In most situations, TestCase should be preferred to TransactionTestCase as it allows faster execution. However, there are some situations where using TransactionTestCase might be necessary (e.g. testing some transactional behavior).
On database backends with no transaction support, TestCase behaves as TransactionTestCase.
def
test_stale_while_revalidate(self):
12 def test_stale_while_revalidate(self): 13 """Fresh values are cached, stale values are served while refreshed in the background""" 14 calls = [] 15 cache = in_memory_cache(key=lambda x: x, timeout=10) 16 17 @cache 18 def counter(x): 19 calls.append(x) 20 return len(calls) 21 22 with patch("authentik.root.cache.monotonic", return_value=0): 23 self.assertEqual(counter(1), 1) 24 self.assertEqual(counter(1), 1) 25 with patch("authentik.root.cache.monotonic", return_value=100): 26 # stale value returned immediately, refreshed once in the background 27 self.assertEqual(counter(1), 1) 28 counter(1) 29 # single worker runs tasks in order, so this waits for queued refreshes 30 cache._executor.submit(lambda: None).result() 31 self.assertEqual(counter(1), 2) 32 self.assertEqual(len(calls), 2) 33 counter.cache_clear() 34 self.assertEqual(counter(1), 3)
Fresh values are cached, stale values are served while refreshed in the background