From ed1952089b7bd8e81f2bdb8d5f82c87d52f78d48 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 2 Dec 2021 16:06:01 +0000 Subject: [PATCH 01/10] Remove use of setup_test_homeserver and use HomeserverTestCase instead --- tests/test_visibility.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_visibility.py b/tests/test_visibility.py index 94b19788d737..3fc1603d1924 100644 --- a/tests/test_visibility.py +++ b/tests/test_visibility.py @@ -22,18 +22,18 @@ from synapse.events import FrozenEvent from synapse.visibility import filter_events_for_server -import tests.unittest -from tests.utils import create_room, setup_test_homeserver +from tests import unittest +from tests.utils import create_room logger = logging.getLogger(__name__) TEST_ROOM_ID = "!TEST:ROOM" -class FilterEventsForServerTestCase(tests.unittest.TestCase): +class FilterEventsForServerTestCase(unittest.HomeserverTestCase): @defer.inlineCallbacks def setUp(self): - self.hs = yield setup_test_homeserver(self.addCleanup) + super(FilterEventsForServerTestCase, self).setUp() self.event_creation_handler = self.hs.get_event_creation_handler() self.event_builder_factory = self.hs.get_event_builder_factory() self.storage = self.hs.get_storage() From c713e16885a4ee30c4edb884fd9ef6c3faa4b214 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 2 Dec 2021 16:08:47 +0000 Subject: [PATCH 02/10] Remove use of inlineCallbacks --- tests/test_visibility.py | 64 +++++++++++++++------------------------- 1 file changed, 24 insertions(+), 40 deletions(-) diff --git a/tests/test_visibility.py b/tests/test_visibility.py index 3fc1603d1924..0f3580da3b59 100644 --- a/tests/test_visibility.py +++ b/tests/test_visibility.py @@ -15,9 +15,6 @@ from typing import Optional from unittest.mock import Mock -from twisted.internet import defer -from twisted.internet.defer import succeed - from synapse.api.room_versions import RoomVersions from synapse.events import FrozenEvent from synapse.visibility import filter_events_for_server @@ -31,16 +28,14 @@ class FilterEventsForServerTestCase(unittest.HomeserverTestCase): - @defer.inlineCallbacks def setUp(self): super(FilterEventsForServerTestCase, self).setUp() self.event_creation_handler = self.hs.get_event_creation_handler() self.event_builder_factory = self.hs.get_event_builder_factory() self.storage = self.hs.get_storage() - yield defer.ensureDeferred(create_room(self.hs, TEST_ROOM_ID, "@someone:ROOM")) + self.get_success(create_room(self.hs, TEST_ROOM_ID, "@someone:ROOM")) - @defer.inlineCallbacks def test_filtering(self): # # The events to be filtered consist of 10 membership events (it doesn't @@ -51,18 +46,20 @@ def test_filtering(self): # # before we do that, we persist some other events to act as state. - yield self.inject_visibility("@admin:hs", "joined") + self.get_success(self.inject_visibility("@admin:hs", "joined")) for i in range(0, 10): - yield self.inject_room_member("@resident%i:hs" % i) + self.get_success(self.inject_room_member("@resident%i:hs" % i)) events_to_filter = [] for i in range(0, 10): user = "@user%i:%s" % (i, "test_server" if i == 5 else "other_server") - evt = yield self.inject_room_member(user, extra_content={"a": "b"}) + evt = self.get_success( + self.inject_room_member(user, extra_content={"a": "b"}) + ) events_to_filter.append(evt) - filtered = yield defer.ensureDeferred( + filtered = self.get_success( filter_events_for_server(self.storage, "test_server", events_to_filter) ) @@ -75,34 +72,31 @@ def test_filtering(self): self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id) self.assertEqual(filtered[i].content["a"], "b") - @defer.inlineCallbacks def test_erased_user(self): # 4 message events, from erased and unerased users, with a membership # change in the middle of them. events_to_filter = [] - evt = yield self.inject_message("@unerased:local_hs") + evt = self.get_success(self.inject_message("@unerased:local_hs")) events_to_filter.append(evt) - evt = yield self.inject_message("@erased:local_hs") + evt = self.get_success(self.inject_message("@erased:local_hs")) events_to_filter.append(evt) - evt = yield self.inject_room_member("@joiner:remote_hs") + evt = self.get_success(self.inject_room_member("@joiner:remote_hs")) events_to_filter.append(evt) - evt = yield self.inject_message("@unerased:local_hs") + evt = self.get_success(self.inject_message("@unerased:local_hs")) events_to_filter.append(evt) - evt = yield self.inject_message("@erased:local_hs") + evt = self.get_success(self.inject_message("@erased:local_hs")) events_to_filter.append(evt) # the erasey user gets erased - yield defer.ensureDeferred( - self.hs.get_datastore().mark_user_erased("@erased:local_hs") - ) + self.get_success(self.hs.get_datastore().mark_user_erased("@erased:local_hs")) # ... and the filtering happens. - filtered = yield defer.ensureDeferred( + filtered = self.get_success( filter_events_for_server(self.storage, "test_server", events_to_filter) ) @@ -123,7 +117,6 @@ def test_erased_user(self): for i in (1, 4): self.assertNotIn("body", filtered[i].content) - @defer.inlineCallbacks def inject_visibility(self, user_id, visibility): content = {"history_visibility": visibility} builder = self.event_builder_factory.for_room_version( @@ -137,15 +130,12 @@ def inject_visibility(self, user_id, visibility): }, ) - event, context = yield defer.ensureDeferred( + event, context = self.get_success( self.event_creation_handler.create_new_client_event(builder) ) - yield defer.ensureDeferred( - self.storage.persistence.persist_event(event, context) - ) + self.get_success(self.storage.persistence.persist_event(event, context)) return event - @defer.inlineCallbacks def inject_room_member( self, user_id, membership="join", extra_content: Optional[dict] = None ): @@ -162,16 +152,13 @@ def inject_room_member( }, ) - event, context = yield defer.ensureDeferred( + event, context = self.get_success( self.event_creation_handler.create_new_client_event(builder) ) - yield defer.ensureDeferred( - self.storage.persistence.persist_event(event, context) - ) + self.get_success(self.storage.persistence.persist_event(event, context)) return event - @defer.inlineCallbacks def inject_message(self, user_id, content=None): if content is None: content = {"body": "testytest", "msgtype": "m.text"} @@ -185,16 +172,13 @@ def inject_message(self, user_id, content=None): }, ) - event, context = yield defer.ensureDeferred( + event, context = self.get_success( self.event_creation_handler.create_new_client_event(builder) ) - yield defer.ensureDeferred( - self.storage.persistence.persist_event(event, context) - ) + self.get_success(self.storage.persistence.persist_event(event, context)) return event - @defer.inlineCallbacks def test_large_room(self): # see what happens when we have a large room with hundreds of thousands # of membership events @@ -273,7 +257,7 @@ def test_large_room(self): storage.main = test_store storage.state = test_store - filtered = yield defer.ensureDeferred( + filtered = self.get_success( filter_events_for_server(test_store, "test_server", events_to_filter) ) logger.info("Filtering took %f seconds", time.time() - start) @@ -339,10 +323,10 @@ def get_state_ids_for_events(self, events, types): hve = self.state_ids_for_events[event_id][k] res[event_id] = {k: hve} - return succeed(res) + return res def get_events(self, events): - return succeed({event_id: self.events[event_id] for event_id in events}) + return {event_id: self.events[event_id] for event_id in events} def are_users_erased(self, users): - return succeed({u: False for u in users}) + return {u: False for u in users} From 20b1ce9624f9515f2534b203ad7b4fd61a15a305 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 2 Dec 2021 16:37:50 +0000 Subject: [PATCH 03/10] Rename test helpers One of them conflicted with a method in the superclass. Thought it best to keep them consistent. --- tests/test_visibility.py | 44 +++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/tests/test_visibility.py b/tests/test_visibility.py index 0f3580da3b59..93903c4348ea 100644 --- a/tests/test_visibility.py +++ b/tests/test_visibility.py @@ -28,7 +28,7 @@ class FilterEventsForServerTestCase(unittest.HomeserverTestCase): - def setUp(self): + def setUp(self) -> None: super(FilterEventsForServerTestCase, self).setUp() self.event_creation_handler = self.hs.get_event_creation_handler() self.event_builder_factory = self.hs.get_event_builder_factory() @@ -36,7 +36,7 @@ def setUp(self): self.get_success(create_room(self.hs, TEST_ROOM_ID, "@someone:ROOM")) - def test_filtering(self): + def test_filtering(self) -> None: # # The events to be filtered consist of 10 membership events (it doesn't # really matter if they are joins or leaves, so let's make them joins). @@ -46,16 +46,16 @@ def test_filtering(self): # # before we do that, we persist some other events to act as state. - self.get_success(self.inject_visibility("@admin:hs", "joined")) + self.get_success(self._inject_visibility("@admin:hs", "joined")) for i in range(0, 10): - self.get_success(self.inject_room_member("@resident%i:hs" % i)) + self.get_success(self._inject_room_member("@resident%i:hs" % i)) events_to_filter = [] for i in range(0, 10): user = "@user%i:%s" % (i, "test_server" if i == 5 else "other_server") evt = self.get_success( - self.inject_room_member(user, extra_content={"a": "b"}) + self._inject_room_member(user, extra_content={"a": "b"}) ) events_to_filter.append(evt) @@ -72,24 +72,24 @@ def test_filtering(self): self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id) self.assertEqual(filtered[i].content["a"], "b") - def test_erased_user(self): + def test_erased_user(self) -> None: # 4 message events, from erased and unerased users, with a membership # change in the middle of them. events_to_filter = [] - evt = self.get_success(self.inject_message("@unerased:local_hs")) + evt = self.get_success(self._inject_message("@unerased:local_hs")) events_to_filter.append(evt) - evt = self.get_success(self.inject_message("@erased:local_hs")) + evt = self.get_success(self._inject_message("@erased:local_hs")) events_to_filter.append(evt) - evt = self.get_success(self.inject_room_member("@joiner:remote_hs")) + evt = self.get_success(self._inject_room_member("@joiner:remote_hs")) events_to_filter.append(evt) - evt = self.get_success(self.inject_message("@unerased:local_hs")) + evt = self.get_success(self._inject_message("@unerased:local_hs")) events_to_filter.append(evt) - evt = self.get_success(self.inject_message("@erased:local_hs")) + evt = self.get_success(self._inject_message("@erased:local_hs")) events_to_filter.append(evt) # the erasey user gets erased @@ -117,7 +117,7 @@ def test_erased_user(self): for i in (1, 4): self.assertNotIn("body", filtered[i].content) - def inject_visibility(self, user_id, visibility): + def _inject_visibility(self, user_id, visibility) -> EventBase: content = {"history_visibility": visibility} builder = self.event_builder_factory.for_room_version( RoomVersions.V1, @@ -136,7 +136,7 @@ def inject_visibility(self, user_id, visibility): self.get_success(self.storage.persistence.persist_event(event, context)) return event - def inject_room_member( + def _inject_room_member( self, user_id, membership="join", extra_content: Optional[dict] = None ): content = {"membership": membership} @@ -159,7 +159,7 @@ def inject_room_member( self.get_success(self.storage.persistence.persist_event(event, context)) return event - def inject_message(self, user_id, content=None): + def _inject_message(self, user_id, content=None) -> EventBase: if content is None: content = {"body": "testytest", "msgtype": "m.text"} builder = self.event_builder_factory.for_room_version( @@ -179,7 +179,7 @@ def inject_message(self, user_id, content=None): self.get_success(self.storage.persistence.persist_event(event, context)) return event - def test_large_room(self): + def test_large_room(self) -> None: # see what happens when we have a large room with hundreds of thousands # of membership events @@ -285,7 +285,7 @@ class _TestStore: """ - def __init__(self): + def __init__(self) -> None: # data for get_events: a map from event_id to event self.events = {} @@ -294,13 +294,15 @@ def __init__(self): # event self.state_ids_for_events = {} - def add_event(self, event): + def add_event(self, event) -> None: self.events[event.event_id] = event - def set_state_ids_for_event(self, event, state): + def set_state_ids_for_event(self, event, state) -> None: self.state_ids_for_events[event.event_id] = state - def get_state_ids_for_events(self, events, types): + def get_state_ids_for_events( + self, events, types + ) -> Dict[str, Dict[Tuple[str, str], str]]: res = {} include_memberships = False for (type, state_key) in types: @@ -325,8 +327,8 @@ def get_state_ids_for_events(self, events, types): return res - def get_events(self, events): + def get_events(self, events) -> Dict[str, EventBase]: return {event_id: self.events[event_id] for event_id in events} - def are_users_erased(self, users): + def are_users_erased(self, users) -> Dict[str, bool]: return {u: False for u in users} From 60b1515c2d074fa4d9f00e3b57ac0d5f489ca08e Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 2 Dec 2021 16:38:03 +0000 Subject: [PATCH 04/10] Annotate fields --- tests/test_visibility.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_visibility.py b/tests/test_visibility.py index 93903c4348ea..c0244040d2c6 100644 --- a/tests/test_visibility.py +++ b/tests/test_visibility.py @@ -287,12 +287,12 @@ class _TestStore: def __init__(self) -> None: # data for get_events: a map from event_id to event - self.events = {} + self.events: Dict[str, EventBase] = {} # data for get_state_ids_for_events mock: a map from event_id to - # a map from (type_state_key) -> event_id for the state at that + # a map from (type, state_key) -> event_id for the state at that # event - self.state_ids_for_events = {} + self.state_ids_for_events: Dict[str, Dict[Tuple[str, Optional[str]], str]] = {} def add_event(self, event) -> None: self.events[event.event_id] = event From 83d0ec4725b2f2c368c14ae1dc53911fe6354729 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 2 Dec 2021 16:43:46 +0000 Subject: [PATCH 05/10] Annotate some methods --- tests/test_visibility.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/tests/test_visibility.py b/tests/test_visibility.py index c0244040d2c6..a9ba4fb62c2d 100644 --- a/tests/test_visibility.py +++ b/tests/test_visibility.py @@ -12,11 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. import logging -from typing import Optional +from typing import Dict, List, Optional, Tuple, cast from unittest.mock import Mock from synapse.api.room_versions import RoomVersions -from synapse.events import FrozenEvent +from synapse.events import EventBase, FrozenEvent +from synapse.storage import Storage +from synapse.types import JsonDict from synapse.visibility import filter_events_for_server from tests import unittest @@ -117,7 +119,7 @@ def test_erased_user(self) -> None: for i in (1, 4): self.assertNotIn("body", filtered[i].content) - def _inject_visibility(self, user_id, visibility) -> EventBase: + def _inject_visibility(self, user_id: str, visibility: str) -> EventBase: content = {"history_visibility": visibility} builder = self.event_builder_factory.for_room_version( RoomVersions.V1, @@ -137,8 +139,11 @@ def _inject_visibility(self, user_id, visibility) -> EventBase: return event def _inject_room_member( - self, user_id, membership="join", extra_content: Optional[dict] = None - ): + self, + user_id: str, + membership: str = "join", + extra_content: Optional[JsonDict] = None, + ) -> EventBase: content = {"membership": membership} content.update(extra_content or {}) builder = self.event_builder_factory.for_room_version( @@ -159,7 +164,9 @@ def _inject_room_member( self.get_success(self.storage.persistence.persist_event(event, context)) return event - def _inject_message(self, user_id, content=None) -> EventBase: + def _inject_message( + self, user_id: str, content: Optional[JsonDict] = None + ) -> EventBase: if content is None: content = {"body": "testytest", "msgtype": "m.text"} builder = self.event_builder_factory.for_room_version( @@ -297,12 +304,14 @@ def __init__(self) -> None: def add_event(self, event) -> None: self.events[event.event_id] = event - def set_state_ids_for_event(self, event, state) -> None: + def set_state_ids_for_event( + self, event: EventBase, state: Dict[Tuple[str, Optional[str]], str] + ) -> None: self.state_ids_for_events[event.event_id] = state def get_state_ids_for_events( - self, events, types - ) -> Dict[str, Dict[Tuple[str, str], str]]: + self, events: List[str], types: List[Tuple[str, Optional[str]]] + ) -> Dict[str, Dict[Tuple[str, Optional[str]], str]]: res = {} include_memberships = False for (type, state_key) in types: @@ -327,8 +336,8 @@ def get_state_ids_for_events( return res - def get_events(self, events) -> Dict[str, EventBase]: + def get_events(self, events: List[str]) -> Dict[str, EventBase]: return {event_id: self.events[event_id] for event_id in events} - def are_users_erased(self, users) -> Dict[str, bool]: + def are_users_erased(self, users: List[str]) -> Dict[str, bool]: return {u: False for u in users} From e213bdc6a78718767b41b0c5a2f9b115609e398a Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 2 Dec 2021 16:44:24 +0000 Subject: [PATCH 06/10] Fix the typing but not the functionality of test_large_room --- tests/test_visibility.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/test_visibility.py b/tests/test_visibility.py index a9ba4fb62c2d..955cad2744c9 100644 --- a/tests/test_visibility.py +++ b/tests/test_visibility.py @@ -186,6 +186,7 @@ def _inject_message( self.get_success(self.storage.persistence.persist_event(event, context)) return event + # FIXME This test is broken! def test_large_room(self) -> None: # see what happens when we have a large room with hundreds of thousands # of membership events @@ -213,7 +214,8 @@ def test_large_room(self) -> None: "state_key": "", "room_id": TEST_ROOM_ID, "content": {"history_visibility": "joined"}, - } + }, + room_version=RoomVersions.V1, ) room_state.append(history_visibility_evt) test_store.add_event(history_visibility_evt) @@ -228,12 +230,13 @@ def test_large_room(self) -> None: "sender": user, "room_id": TEST_ROOM_ID, "content": {"membership": "join", "extra": "zzz,"}, - } + }, + room_version=RoomVersions.V1, ) room_state.append(evt) test_store.add_event(evt) - events_to_filter = [] + events_to_filter: List[EventBase] = [] for i in range(0, 10): user = "@user%i:%s" % (i, "test_server" if i == 5 else "other_server") evt = FrozenEvent( @@ -244,7 +247,8 @@ def test_large_room(self) -> None: "sender": user, "room_id": TEST_ROOM_ID, "content": {"membership": "join", "extra": "zzz"}, - } + }, + room_version=RoomVersions.V1, ) events_to_filter.append(evt) room_state.append(evt) @@ -265,7 +269,9 @@ def test_large_room(self) -> None: storage.state = test_store filtered = self.get_success( - filter_events_for_server(test_store, "test_server", events_to_filter) + filter_events_for_server( + cast(Storage, test_store), "test_server", events_to_filter + ) ) logger.info("Filtering took %f seconds", time.time() - start) @@ -283,7 +289,8 @@ def test_large_room(self) -> None: self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id) self.assertEqual(filtered[i].content["extra"], "zzz") - test_large_room.skip = "Disabled by default because it's slow" + # (Mypy is not happy about a function having a field, but we ignore it because it's fine here.) + test_large_room.skip = "Disabled by default because it's slow" # type: ignore[attr-defined] class _TestStore: From 5ce62587b898460ef38dcd6ff843989d3fd73f73 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 2 Dec 2021 16:44:43 +0000 Subject: [PATCH 07/10] Take test_visibility off the mypy list --- mypy.ini | 1 - 1 file changed, 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index 99b5c41ad665..1305dc236274 100644 --- a/mypy.ini +++ b/mypy.ini @@ -123,7 +123,6 @@ exclude = (?x) |tests/test_server.py |tests/test_state.py |tests/test_terms_auth.py - |tests/test_visibility.py |tests/unittest.py |tests/util/caches/test_cached_call.py |tests/util/caches/test_deferred_cache.py From 8c42109ad509845ed6776cca43794cb632aa82a9 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 2 Dec 2021 16:45:17 +0000 Subject: [PATCH 08/10] Newsfile Signed-off-by: Olivier Wilkinson (reivilibre) --- changelog.d/11495.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/11495.misc diff --git a/changelog.d/11495.misc b/changelog.d/11495.misc new file mode 100644 index 000000000000..5b52697fb4af --- /dev/null +++ b/changelog.d/11495.misc @@ -0,0 +1 @@ +Clean up `tests.test_visibility` to remove legacy code. \ No newline at end of file From 5c572650dd0d994332f3d8d67b67420cd07f77c3 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 2 Dec 2021 17:42:29 +0000 Subject: [PATCH 09/10] Remove unloved test_large_room test, which is broken and seems to have been untouched for a long time Several breaking changes seem to have happened since this test was last updated. It doesn't seem like anyone has been interested in running it for a long time. --- tests/test_visibility.py | 170 +-------------------------------------- 1 file changed, 2 insertions(+), 168 deletions(-) diff --git a/tests/test_visibility.py b/tests/test_visibility.py index 955cad2744c9..78cc402e731c 100644 --- a/tests/test_visibility.py +++ b/tests/test_visibility.py @@ -12,15 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. import logging -from typing import Dict, List, Optional, Tuple, cast -from unittest.mock import Mock +from typing import Optional from synapse.api.room_versions import RoomVersions -from synapse.events import EventBase, FrozenEvent -from synapse.storage import Storage +from synapse.events import EventBase from synapse.types import JsonDict from synapse.visibility import filter_events_for_server - from tests import unittest from tests.utils import create_room @@ -185,166 +182,3 @@ def _inject_message( self.get_success(self.storage.persistence.persist_event(event, context)) return event - - # FIXME This test is broken! - def test_large_room(self) -> None: - # see what happens when we have a large room with hundreds of thousands - # of membership events - - # As above, the events to be filtered consist of 10 membership events, - # where one of them is for a user on the server we are filtering for. - - import cProfile - import pstats - import time - - # we stub out the store, because building up all that state the normal - # way is very slow. - test_store = _TestStore() - - # our initial state is 100000 membership events and one - # history_visibility event. - room_state = [] - - history_visibility_evt = FrozenEvent( - { - "event_id": "$history_vis", - "type": "m.room.history_visibility", - "sender": "@resident_user_0:test.com", - "state_key": "", - "room_id": TEST_ROOM_ID, - "content": {"history_visibility": "joined"}, - }, - room_version=RoomVersions.V1, - ) - room_state.append(history_visibility_evt) - test_store.add_event(history_visibility_evt) - - for i in range(0, 100000): - user = "@resident_user_%i:test.com" % (i,) - evt = FrozenEvent( - { - "event_id": "$res_event_%i" % (i,), - "type": "m.room.member", - "state_key": user, - "sender": user, - "room_id": TEST_ROOM_ID, - "content": {"membership": "join", "extra": "zzz,"}, - }, - room_version=RoomVersions.V1, - ) - room_state.append(evt) - test_store.add_event(evt) - - events_to_filter: List[EventBase] = [] - for i in range(0, 10): - user = "@user%i:%s" % (i, "test_server" if i == 5 else "other_server") - evt = FrozenEvent( - { - "event_id": "$evt%i" % (i,), - "type": "m.room.member", - "state_key": user, - "sender": user, - "room_id": TEST_ROOM_ID, - "content": {"membership": "join", "extra": "zzz"}, - }, - room_version=RoomVersions.V1, - ) - events_to_filter.append(evt) - room_state.append(evt) - - test_store.add_event(evt) - test_store.set_state_ids_for_event( - evt, {(e.type, e.state_key): e.event_id for e in room_state} - ) - - pr = cProfile.Profile() - pr.enable() - - logger.info("Starting filtering") - start = time.time() - - storage = Mock() - storage.main = test_store - storage.state = test_store - - filtered = self.get_success( - filter_events_for_server( - cast(Storage, test_store), "test_server", events_to_filter - ) - ) - logger.info("Filtering took %f seconds", time.time() - start) - - pr.disable() - with open("filter_events_for_server.profile", "w+") as f: - ps = pstats.Stats(pr, stream=f).sort_stats("cumulative") - ps.print_stats() - - # the result should be 5 redacted events, and 5 unredacted events. - for i in range(0, 5): - self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id) - self.assertNotIn("extra", filtered[i].content) - - for i in range(5, 10): - self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id) - self.assertEqual(filtered[i].content["extra"], "zzz") - - # (Mypy is not happy about a function having a field, but we ignore it because it's fine here.) - test_large_room.skip = "Disabled by default because it's slow" # type: ignore[attr-defined] - - -class _TestStore: - """Implements a few methods of the DataStore, so that we can test - filter_events_for_server - - """ - - def __init__(self) -> None: - # data for get_events: a map from event_id to event - self.events: Dict[str, EventBase] = {} - - # data for get_state_ids_for_events mock: a map from event_id to - # a map from (type, state_key) -> event_id for the state at that - # event - self.state_ids_for_events: Dict[str, Dict[Tuple[str, Optional[str]], str]] = {} - - def add_event(self, event) -> None: - self.events[event.event_id] = event - - def set_state_ids_for_event( - self, event: EventBase, state: Dict[Tuple[str, Optional[str]], str] - ) -> None: - self.state_ids_for_events[event.event_id] = state - - def get_state_ids_for_events( - self, events: List[str], types: List[Tuple[str, Optional[str]]] - ) -> Dict[str, Dict[Tuple[str, Optional[str]], str]]: - res = {} - include_memberships = False - for (type, state_key) in types: - if type == "m.room.history_visibility": - continue - if type != "m.room.member" or state_key is not None: - raise RuntimeError( - "Unimplemented: get_state_ids with type (%s, %s)" - % (type, state_key) - ) - include_memberships = True - - if include_memberships: - for event_id in events: - res[event_id] = self.state_ids_for_events[event_id] - - else: - k = ("m.room.history_visibility", "") - for event_id in events: - hve = self.state_ids_for_events[event_id][k] - res[event_id] = {k: hve} - - return res - - def get_events(self, events: List[str]) -> Dict[str, EventBase]: - return {event_id: self.events[event_id] for event_id in events} - - def are_users_erased(self, users: List[str]) -> Dict[str, bool]: - return {u: False for u in users} From 4469113bf9606144608f69e99e01a2a29ae2e6cf Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 2 Dec 2021 17:45:19 +0000 Subject: [PATCH 10/10] Gah, isort! --- tests/test_visibility.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_visibility.py b/tests/test_visibility.py index 78cc402e731c..e0b08d67d435 100644 --- a/tests/test_visibility.py +++ b/tests/test_visibility.py @@ -18,6 +18,7 @@ from synapse.events import EventBase from synapse.types import JsonDict from synapse.visibility import filter_events_for_server + from tests import unittest from tests.utils import create_room