Track in memory events using weakrefs (#10533)
2 files changed, 26 insertions, 0 deletions
diff --git a/tests/handlers/test_sync.py b/tests/handlers/test_sync.py
index 865b8b7e47..db3302a4c7 100644
--- a/tests/handlers/test_sync.py
+++ b/tests/handlers/test_sync.py
@@ -160,6 +160,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
# Blow away caches (supported room versions can only change due to a restart).
self.store.get_rooms_for_user_with_stream_ordering.invalidate_all()
self.store._get_event_cache.clear()
+ self.store._event_ref.clear()
# The rooms should be excluded from the sync response.
# Get a new request key.
diff --git a/tests/storage/databases/main/test_events_worker.py b/tests/storage/databases/main/test_events_worker.py
index c237a8c7e2..38963ce4a7 100644
--- a/tests/storage/databases/main/test_events_worker.py
+++ b/tests/storage/databases/main/test_events_worker.py
@@ -154,6 +154,31 @@ class EventCacheTestCase(unittest.HomeserverTestCase):
# We should have fetched the event from the DB
self.assertEqual(ctx.get_resource_usage().evt_db_fetch_count, 1)
+ def test_event_ref(self):
+ """Test that we reuse events that are still in memory but have fallen
+ out of the cache, rather than requesting them from the DB.
+ """
+
+ # Reset the event cache
+ self.store._get_event_cache.clear()
+
+ with LoggingContext("test") as ctx:
+ # We keep hold of the event event though we never use it.
+ event = self.get_success(self.store.get_event(self.event_id)) # noqa: F841
+
+ # We should have fetched the event from the DB
+ self.assertEqual(ctx.get_resource_usage().evt_db_fetch_count, 1)
+
+ # Reset the event cache
+ self.store._get_event_cache.clear()
+
+ with LoggingContext("test") as ctx:
+ self.get_success(self.store.get_event(self.event_id))
+
+ # Since the event is still in memory we shouldn't have fetched it
+ # from the DB
+ self.assertEqual(ctx.get_resource_usage().evt_db_fetch_count, 0)
+
def test_dedupe(self):
"""Test that if we request the same event multiple times we only pull it
out once.
|