Skip to content

Commit

Permalink
Add test to reflect desired behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
realtyem committed Sep 25, 2023
1 parent 6785879 commit 12ff11b
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions tests/storage/test_purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,89 @@ def test_purge_room(self) -> None:
self.store._invalidate_local_get_event_cache(create_event.event_id)
self.get_failure(self.store.get_event(create_event.event_id), NotFoundError)
self.get_failure(self.store.get_event(first["event_id"]), NotFoundError)

def test_purge_one_room_will_not_uncache_events_from_another(self) -> None:
"""
Purging a room will delete everything about it, but leave other rooms still in
the event cache.
"""
# Create a second room to use for our testing. This room will have four messages
# as well, but they should stay in the get_event_cache
self.room_id2 = self.helper.create_room_as(self.user_id)

# Send four messages to the first room
r1_first = self.helper.send(self.room_id, body="test1")
r1_second = self.helper.send(self.room_id, body="test2")
r1_third = self.helper.send(self.room_id, body="test3")
r1_last = self.helper.send(self.room_id, body="test4")

# Send four messages to the second room
r2_first = self.helper.send(self.room_id2, body="test1_2")
r2_second = self.helper.send(self.room_id2, body="test2_2")
r2_third = self.helper.send(self.room_id2, body="test3_2")
r2_last = self.helper.send(self.room_id2, body="test4_2")

# Get the current room state for the first room(the one that will be deleted).
create_event = self.get_success(
self._storage_controllers.state.get_current_state_event(
self.room_id, "m.room.create", ""
)
)
assert create_event is not None

# Purge everything before this topological token
self.get_success(
self._storage_controllers.purge_events.purge_room(self.room_id)
)

# The room has been purged, the appropriate invalidations should be done.
# The events from the first room should not be in the event cache.
# Note: the function to call retrieves a Dict[str, EventCacheEntry], if the Dict
# is empty this is successful.
self.assertDictEqual(
self.store._get_events_from_local_cache(r1_first["event_id"]),
{},
f"Event found in cache: {r1_first}",
)
self.assertDictEqual(
self.store._get_events_from_local_cache(r1_second["event_id"]),
{},
f"Event found in cache: {r1_second}",
)
self.assertDictEqual(
self.store._get_events_from_local_cache(r1_third["event_id"]),
{},
f"Event found in cache: {r1_third}",
)
self.assertDictEqual(
self.store._get_events_from_local_cache(r1_last["event_id"]),
{},
f"Event found in cache: {r1_last}",
)

# The events from the second room should still be in the event cache. If the
# Dict retrieved has anything and isn't empty, this is a success. Might want to
# change this to check for a instance of class EventCacheEntry instead.
self.assertIsNot(
self.store._get_events_from_local_cache(r2_first["event_id"]),
{},
f"Event found in cache: {r2_first}",
)
self.assertIsNot(
self.store._get_events_from_local_cache(r2_second["event_id"]),
{},
f"Event found in cache: {r2_second}",
)
self.assertIsNot(
self.store._get_events_from_local_cache(r2_third["event_id"]),
{},
f"Event found in cache: {r2_third}",
)
self.assertIsNot(
self.store._get_events_from_local_cache(r2_last["event_id"]),
{},
f"Event found in cache: {r2_last}",
)

self.get_failure(self.store.get_event(create_event.event_id), NotFoundError)
self.get_failure(self.store.get_event(r1_first["event_id"]), NotFoundError)

0 comments on commit 12ff11b

Please sign in to comment.