1
0

test_room_batch.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import logging
  2. from typing import List, Tuple
  3. from unittest.mock import Mock, patch
  4. from twisted.test.proto_helpers import MemoryReactor
  5. from synapse.api.constants import EventContentFields, EventTypes
  6. from synapse.appservice import ApplicationService
  7. from synapse.rest import admin
  8. from synapse.rest.client import login, register, room, room_batch, sync
  9. from synapse.server import HomeServer
  10. from synapse.types import JsonDict, RoomStreamToken
  11. from synapse.util import Clock
  12. from tests import unittest
  13. logger = logging.getLogger(__name__)
  14. def _create_join_state_events_for_batch_send_request(
  15. virtual_user_ids: List[str],
  16. insert_time: int,
  17. ) -> List[JsonDict]:
  18. return [
  19. {
  20. "type": EventTypes.Member,
  21. "sender": virtual_user_id,
  22. "origin_server_ts": insert_time,
  23. "content": {
  24. "membership": "join",
  25. "displayname": "display-name-for-%s" % (virtual_user_id,),
  26. },
  27. "state_key": virtual_user_id,
  28. }
  29. for virtual_user_id in virtual_user_ids
  30. ]
  31. def _create_message_events_for_batch_send_request(
  32. virtual_user_id: str, insert_time: int, count: int
  33. ) -> List[JsonDict]:
  34. return [
  35. {
  36. "type": EventTypes.Message,
  37. "sender": virtual_user_id,
  38. "origin_server_ts": insert_time,
  39. "content": {
  40. "msgtype": "m.text",
  41. "body": "Historical %d" % (i),
  42. EventContentFields.MSC2716_HISTORICAL: True,
  43. },
  44. }
  45. for i in range(count)
  46. ]
  47. class RoomBatchTestCase(unittest.HomeserverTestCase):
  48. """Test importing batches of historical messages."""
  49. servlets = [
  50. admin.register_servlets_for_client_rest_resource,
  51. room_batch.register_servlets,
  52. room.register_servlets,
  53. register.register_servlets,
  54. login.register_servlets,
  55. sync.register_servlets,
  56. ]
  57. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  58. config = self.default_config()
  59. self.appservice = ApplicationService(
  60. token="i_am_an_app_service",
  61. id="1234",
  62. namespaces={"users": [{"regex": r"@as_user.*", "exclusive": True}]},
  63. # Note: this user does not have to match the regex above
  64. sender="@as_main:test",
  65. )
  66. mock_load_appservices = Mock(return_value=[self.appservice])
  67. with patch(
  68. "synapse.storage.databases.main.appservice.load_appservices",
  69. mock_load_appservices,
  70. ):
  71. hs = self.setup_test_homeserver(config=config)
  72. return hs
  73. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  74. self.clock = clock
  75. self._storage_controllers = hs.get_storage_controllers()
  76. self.virtual_user_id, _ = self.register_appservice_user(
  77. "as_user_potato", self.appservice.token
  78. )
  79. def _create_test_room(self) -> Tuple[str, str, str, str]:
  80. room_id = self.helper.create_room_as(
  81. self.appservice.sender, tok=self.appservice.token
  82. )
  83. res_a = self.helper.send_event(
  84. room_id=room_id,
  85. type=EventTypes.Message,
  86. content={
  87. "msgtype": "m.text",
  88. "body": "A",
  89. },
  90. tok=self.appservice.token,
  91. )
  92. event_id_a = res_a["event_id"]
  93. res_b = self.helper.send_event(
  94. room_id=room_id,
  95. type=EventTypes.Message,
  96. content={
  97. "msgtype": "m.text",
  98. "body": "B",
  99. },
  100. tok=self.appservice.token,
  101. )
  102. event_id_b = res_b["event_id"]
  103. res_c = self.helper.send_event(
  104. room_id=room_id,
  105. type=EventTypes.Message,
  106. content={
  107. "msgtype": "m.text",
  108. "body": "C",
  109. },
  110. tok=self.appservice.token,
  111. )
  112. event_id_c = res_c["event_id"]
  113. return room_id, event_id_a, event_id_b, event_id_c
  114. @unittest.override_config({"experimental_features": {"msc2716_enabled": True}})
  115. def test_same_state_groups_for_whole_historical_batch(self) -> None:
  116. """Make sure that when using the `/batch_send` endpoint to import a
  117. bunch of historical messages, it re-uses the same `state_group` across
  118. the whole batch. This is an easy optimization to make sure we're getting
  119. right because the state for the whole batch is contained in
  120. `state_events_at_start` and can be shared across everything.
  121. """
  122. time_before_room = int(self.clock.time_msec())
  123. room_id, event_id_a, _, _ = self._create_test_room()
  124. channel = self.make_request(
  125. "POST",
  126. "/_matrix/client/unstable/org.matrix.msc2716/rooms/%s/batch_send?prev_event_id=%s"
  127. % (room_id, event_id_a),
  128. content={
  129. "events": _create_message_events_for_batch_send_request(
  130. self.virtual_user_id, time_before_room, 3
  131. ),
  132. "state_events_at_start": _create_join_state_events_for_batch_send_request(
  133. [self.virtual_user_id], time_before_room
  134. ),
  135. },
  136. access_token=self.appservice.token,
  137. )
  138. self.assertEqual(channel.code, 200, channel.result)
  139. # Get the historical event IDs that we just imported
  140. historical_event_ids = channel.json_body["event_ids"]
  141. self.assertEqual(len(historical_event_ids), 3)
  142. # Fetch the state_groups
  143. state_group_map = self.get_success(
  144. self._storage_controllers.state.get_state_groups_ids(
  145. room_id, historical_event_ids
  146. )
  147. )
  148. # We expect all of the historical events to be using the same state_group
  149. # so there should only be a single state_group here!
  150. self.assertEqual(
  151. len(state_group_map.keys()),
  152. 1,
  153. "Expected a single state_group to be returned by saw state_groups=%s"
  154. % (state_group_map.keys(),),
  155. )
  156. @unittest.override_config({"experimental_features": {"msc2716_enabled": True}})
  157. def test_sync_while_batch_importing(self) -> None:
  158. """
  159. Make sure that /sync correctly returns full room state when a user joins
  160. during ongoing batch backfilling.
  161. See: https://github.com/matrix-org/synapse/issues/12281
  162. """
  163. # Create user who will be invited & join room
  164. user_id = self.register_user("beep", "test")
  165. user_tok = self.login("beep", "test")
  166. time_before_room = int(self.clock.time_msec())
  167. # Create a room with some events
  168. room_id, _, _, _ = self._create_test_room()
  169. # Invite the user
  170. self.helper.invite(
  171. room_id, src=self.appservice.sender, tok=self.appservice.token, targ=user_id
  172. )
  173. # Create another room, send a bunch of events to advance the stream token
  174. other_room_id = self.helper.create_room_as(
  175. self.appservice.sender, tok=self.appservice.token
  176. )
  177. for _ in range(5):
  178. self.helper.send_event(
  179. room_id=other_room_id,
  180. type=EventTypes.Message,
  181. content={"msgtype": "m.text", "body": "C"},
  182. tok=self.appservice.token,
  183. )
  184. # Join the room as the normal user
  185. self.helper.join(room_id, user_id, tok=user_tok)
  186. # Create an event to hang the historical batch from - In order to see
  187. # the failure case originally reported in #12281, the historical batch
  188. # must be hung from the most recent event in the room so the base
  189. # insertion event ends up with the highest `topogological_ordering`
  190. # (`depth`) in the room but will have a negative `stream_ordering`
  191. # because it's a `historical` event. Previously, when assembling the
  192. # `state` for the `/sync` response, the bugged logic would sort by
  193. # `topological_ordering` descending and pick up the base insertion
  194. # event because it has a negative `stream_ordering` below the given
  195. # pagination token. Now we properly sort by `stream_ordering`
  196. # descending which puts `historical` events with a negative
  197. # `stream_ordering` way at the bottom and aren't selected as expected.
  198. response = self.helper.send_event(
  199. room_id=room_id,
  200. type=EventTypes.Message,
  201. content={
  202. "msgtype": "m.text",
  203. "body": "C",
  204. },
  205. tok=self.appservice.token,
  206. )
  207. event_to_hang_id = response["event_id"]
  208. channel = self.make_request(
  209. "POST",
  210. "/_matrix/client/unstable/org.matrix.msc2716/rooms/%s/batch_send?prev_event_id=%s"
  211. % (room_id, event_to_hang_id),
  212. content={
  213. "events": _create_message_events_for_batch_send_request(
  214. self.virtual_user_id, time_before_room, 3
  215. ),
  216. "state_events_at_start": _create_join_state_events_for_batch_send_request(
  217. [self.virtual_user_id], time_before_room
  218. ),
  219. },
  220. access_token=self.appservice.token,
  221. )
  222. self.assertEqual(channel.code, 200, channel.result)
  223. # Now we need to find the invite + join events stream tokens so we can sync between
  224. main_store = self.hs.get_datastores().main
  225. events, next_key = self.get_success(
  226. main_store.get_recent_events_for_room(
  227. room_id,
  228. 50,
  229. end_token=main_store.get_room_max_token(),
  230. ),
  231. )
  232. invite_event_position = None
  233. for event in events:
  234. if (
  235. event.type == "m.room.member"
  236. and event.content["membership"] == "invite"
  237. ):
  238. invite_event_position = self.get_success(
  239. main_store.get_topological_token_for_event(event.event_id)
  240. )
  241. break
  242. assert invite_event_position is not None, "No invite event found"
  243. # Remove the topological order from the token by re-creating w/stream only
  244. invite_event_position = RoomStreamToken(None, invite_event_position.stream)
  245. # Sync everything after this token
  246. since_token = self.get_success(invite_event_position.to_string(main_store))
  247. sync_response = self.make_request(
  248. "GET",
  249. f"/sync?since={since_token}",
  250. access_token=user_tok,
  251. )
  252. # Assert that, for this room, the user was considered to have joined and thus
  253. # receives the full state history
  254. state_event_types = [
  255. event["type"]
  256. for event in sync_response.json_body["rooms"]["join"][room_id]["state"][
  257. "events"
  258. ]
  259. ]
  260. assert (
  261. "m.room.create" in state_event_types
  262. ), "Missing room full state in sync response"