test_room_batch.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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
  9. from synapse.server import HomeServer
  10. from synapse.types import JsonDict
  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. ]
  56. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  57. config = self.default_config()
  58. self.appservice = ApplicationService(
  59. token="i_am_an_app_service",
  60. hostname="test",
  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 = hs.get_storage()
  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):
  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.state.get_state_groups_ids(room_id, historical_event_ids)
  145. )
  146. # We expect all of the historical events to be using the same state_group
  147. # so there should only be a single state_group here!
  148. self.assertEqual(
  149. len(state_group_map.keys()),
  150. 1,
  151. "Expected a single state_group to be returned by saw state_groups=%s"
  152. % (state_group_map.keys(),),
  153. )