Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Jun 21, 2021
1 parent 01afcf6 commit a4657df
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion tests/util/test_lrucache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from unittest.mock import Mock

from synapse.util.caches.lrucache import LruCache
from synapse.util.caches.lrucache import LruCache, setup_expire_lru_cache_entries
from synapse.util.caches.treecache import TreeCache

from tests import unittest
Expand Down Expand Up @@ -260,3 +260,47 @@ def test_evict(self):
self.assertEquals(cache["key3"], [3])
self.assertEquals(cache["key4"], [4])
self.assertEquals(cache["key5"], [5, 6])


class TimeEvictionTestCase(unittest.HomeserverTestCase):
"""Test that time based eviction works correctly."""

def default_config(self):
config = super().default_config()

config.setdefault("caches", {})["expiry_time"] = "30m"

return config

def test_evict(self):
setup_expire_lru_cache_entries(self.hs)

cache = LruCache(5, clock=self.hs.get_clock())

# Check that we evict entries we haven't accessed for 30 minutes.
cache["key1"] = 1
cache["key2"] = 2

self.reactor.advance(20 * 60)

self.assertEqual(cache.get("key1"), 1)

self.reactor.advance(20 * 60)

# We have only touched `key1` in the last 30m, so we expect that to
# still be in the cache while `key2` should have been evicted.
self.assertEqual(cache.get("key1"), 1)
self.assertEqual(cache.get("key2"), None)

# Check that re-adding an expired key works correctly.
cache["key2"] = 3
self.assertEqual(cache.get("key2"), 3)

self.reactor.advance(20 * 60)

self.assertEqual(cache.get("key2"), 3)

self.reactor.advance(20 * 60)

self.assertEqual(cache.get("key1"), None)
self.assertEqual(cache.get("key2"), 3)

0 comments on commit a4657df

Please sign in to comment.