seitime-frappe/frappe/tests/test_client_cache.py
2025-01-06 18:57:57 +05:30

39 lines
1.3 KiB
Python

import time
import frappe
from frappe.tests import IntegrationTestCase
TEST_KEY = "42"
class TestClientCache(IntegrationTestCase):
def setUp(self) -> None:
frappe.client_cache.delete_value(TEST_KEY)
return super().setUp()
def test_client_cache_is_used(self):
frappe.client_cache.set_value(TEST_KEY, 42)
frappe.client_cache.get_value(TEST_KEY)
with self.assertRedisCallCounts(0):
frappe.client_cache.get_value(TEST_KEY)
def test_client_cache_is_updated_instantly_noloop(self):
val = frappe.generate_hash()
frappe.client_cache.set_value(TEST_KEY, val)
with self.assertRedisCallCounts(0): # Locally set value should not be invalidated.
self.assertEqual(frappe.client_cache.get_value(TEST_KEY), val)
def test_invalidation_from_another_client_works(self):
val = frappe.generate_hash()
frappe.client_cache.set_value(TEST_KEY, val)
self.assertEqual(frappe.client_cache.get_value(TEST_KEY), val)
# frappe.cache is our "another client"
val = frappe.generate_hash()
frappe.cache.set_value(TEST_KEY, val)
# This is almost instant, but obviously not as fast as running the next instruction in
# current thread. So we wait.
time.sleep(0.1)
with self.assertRedisCallCounts(1):
self.assertEqual(frappe.client_cache.get_value(TEST_KEY), val)