Skip to content

Commit

Permalink
Add another test showing the weak ref metadata not growing unchecked
Browse files Browse the repository at this point in the history
  • Loading branch information
realtyem committed Sep 25, 2023
1 parent 12ff11b commit 8fc6611
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/storage/databases/main/test_events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,41 @@ def test_event_ref(self) -> None:
# from the DB
self.assertEqual(ctx.get_resource_usage().evt_db_fetch_count, 0)

def test_invalidating_get_event_cache_also_removes_metadata(self) -> None:
"""
Test that when an event is removed from the get_event_cache, that the associated
entries in the metadata cache are also removed.
"""
# Reset the event cache
self.store._get_event_cache.clear()

# Retrieve the event base though we never use it. This will populate the caches
event_base = self.get_success(self.store.get_event(self.event_id)) # noqa: F841

self.assertIsNotNone(event_base)
cache_entry_1 = self.store._get_event_cache.get_local((self.event_id,), None)
self.assertIsNotNone(cache_entry_1)

cache_entry_2 = self.store._event_metadata[self.room].events.get(self.event_id, None)
self.assertIsNotNone(cache_entry_2)

# These are the same EventCacheEntry
self.assertIs(cache_entry_1, cache_entry_2)

# Remove the reference, so they won't hang around accidentally.
del cache_entry_1
del cache_entry_2

# Remove *only* the event in the get_event_cache(and of course the event_ref and
# current_fetches) but don't directly touch the metadata cache.
self.store._invalidate_local_get_event_cache(self.event_id)

# Let's see what is in the metadata cache at this point
cache_entry_3 = self.store._event_metadata[self.room].events.get(self.event_id, None)

# Poof! It's gone.
self.assertIsNone(cache_entry_3)

def test_dedupe(self) -> None:
"""Test that if we request the same event multiple times we only pull it
out once.
Expand Down

0 comments on commit 8fc6611

Please sign in to comment.