1
0

test_cleanup_extrems.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. # Copyright 2019 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os.path
  15. from unittest.mock import Mock, patch
  16. import synapse.rest.admin
  17. from synapse.api.constants import EventTypes
  18. from synapse.rest.client import login, room
  19. from synapse.storage import prepare_database
  20. from synapse.types import UserID, create_requester
  21. from tests.unittest import HomeserverTestCase
  22. class CleanupExtremBackgroundUpdateStoreTestCase(HomeserverTestCase):
  23. """
  24. Test the background update to clean forward extremities table.
  25. """
  26. def prepare(self, reactor, clock, homeserver):
  27. self.store = homeserver.get_datastores().main
  28. self.room_creator = homeserver.get_room_creation_handler()
  29. # Create a test user and room
  30. self.user = UserID("alice", "test")
  31. self.requester = create_requester(self.user)
  32. info, _ = self.get_success(self.room_creator.create_room(self.requester, {}))
  33. self.room_id = info["room_id"]
  34. def run_background_update(self):
  35. """Re run the background update to clean up the extremities."""
  36. # Make sure we don't clash with in progress updates.
  37. self.assertTrue(
  38. self.store.db_pool.updates._all_done, "Background updates are still ongoing"
  39. )
  40. schema_path = os.path.join(
  41. prepare_database.schema_path,
  42. "main",
  43. "delta",
  44. "54",
  45. "delete_forward_extremities.sql",
  46. )
  47. def run_delta_file(txn):
  48. prepare_database.executescript(txn, schema_path)
  49. self.get_success(
  50. self.store.db_pool.runInteraction(
  51. "test_delete_forward_extremities", run_delta_file
  52. )
  53. )
  54. # Ugh, have to reset this flag
  55. self.store.db_pool.updates._all_done = False
  56. self.wait_for_background_updates()
  57. def add_extremity(self, room_id: str, event_id: str) -> None:
  58. """
  59. Add the given event as an extremity to the room.
  60. """
  61. self.get_success(
  62. self.hs.get_datastores().main.db_pool.simple_insert(
  63. table="event_forward_extremities",
  64. values={"room_id": room_id, "event_id": event_id},
  65. desc="test_add_extremity",
  66. )
  67. )
  68. self.hs.get_datastores().main.get_latest_event_ids_in_room.invalidate(
  69. (room_id,)
  70. )
  71. def test_soft_failed_extremities_handled_correctly(self):
  72. """Test that extremities are correctly calculated in the presence of
  73. soft failed events.
  74. Tests a graph like:
  75. A <- SF1 <- SF2 <- B
  76. Where SF* are soft failed.
  77. """
  78. # Create the room graph
  79. event_id_1 = self.create_and_send_event(self.room_id, self.user)
  80. event_id_2 = self.create_and_send_event(
  81. self.room_id, self.user, True, [event_id_1]
  82. )
  83. event_id_3 = self.create_and_send_event(
  84. self.room_id, self.user, True, [event_id_2]
  85. )
  86. event_id_4 = self.create_and_send_event(
  87. self.room_id, self.user, False, [event_id_3]
  88. )
  89. # Check the latest events are as expected
  90. latest_event_ids = self.get_success(
  91. self.store.get_latest_event_ids_in_room(self.room_id)
  92. )
  93. self.assertEqual(latest_event_ids, [event_id_4])
  94. def test_basic_cleanup(self):
  95. """Test that extremities are correctly calculated in the presence of
  96. soft failed events.
  97. Tests a graph like:
  98. A <- SF1 <- B
  99. Where SF* are soft failed, and with extremities of A and B
  100. """
  101. # Create the room graph
  102. event_id_a = self.create_and_send_event(self.room_id, self.user)
  103. event_id_sf1 = self.create_and_send_event(
  104. self.room_id, self.user, True, [event_id_a]
  105. )
  106. event_id_b = self.create_and_send_event(
  107. self.room_id, self.user, False, [event_id_sf1]
  108. )
  109. # Add the new extremity and check the latest events are as expected
  110. self.add_extremity(self.room_id, event_id_a)
  111. latest_event_ids = self.get_success(
  112. self.store.get_latest_event_ids_in_room(self.room_id)
  113. )
  114. self.assertEqual(set(latest_event_ids), {event_id_a, event_id_b})
  115. # Run the background update and check it did the right thing
  116. self.run_background_update()
  117. latest_event_ids = self.get_success(
  118. self.store.get_latest_event_ids_in_room(self.room_id)
  119. )
  120. self.assertEqual(latest_event_ids, [event_id_b])
  121. def test_chain_of_fail_cleanup(self):
  122. """Test that extremities are correctly calculated in the presence of
  123. soft failed events.
  124. Tests a graph like:
  125. A <- SF1 <- SF2 <- B
  126. Where SF* are soft failed, and with extremities of A and B
  127. """
  128. # Create the room graph
  129. event_id_a = self.create_and_send_event(self.room_id, self.user)
  130. event_id_sf1 = self.create_and_send_event(
  131. self.room_id, self.user, True, [event_id_a]
  132. )
  133. event_id_sf2 = self.create_and_send_event(
  134. self.room_id, self.user, True, [event_id_sf1]
  135. )
  136. event_id_b = self.create_and_send_event(
  137. self.room_id, self.user, False, [event_id_sf2]
  138. )
  139. # Add the new extremity and check the latest events are as expected
  140. self.add_extremity(self.room_id, event_id_a)
  141. latest_event_ids = self.get_success(
  142. self.store.get_latest_event_ids_in_room(self.room_id)
  143. )
  144. self.assertEqual(set(latest_event_ids), {event_id_a, event_id_b})
  145. # Run the background update and check it did the right thing
  146. self.run_background_update()
  147. latest_event_ids = self.get_success(
  148. self.store.get_latest_event_ids_in_room(self.room_id)
  149. )
  150. self.assertEqual(latest_event_ids, [event_id_b])
  151. def test_forked_graph_cleanup(self):
  152. r"""Test that extremities are correctly calculated in the presence of
  153. soft failed events.
  154. Tests a graph like, where time flows down the page:
  155. A B
  156. / \ /
  157. / \ /
  158. SF1 SF2
  159. | |
  160. SF3 |
  161. / \ |
  162. | \ |
  163. C SF4
  164. Where SF* are soft failed, and with them A, B and C marked as
  165. extremities. This should resolve to B and C being marked as extremity.
  166. """
  167. # Create the room graph
  168. event_id_a = self.create_and_send_event(self.room_id, self.user)
  169. event_id_b = self.create_and_send_event(self.room_id, self.user)
  170. event_id_sf1 = self.create_and_send_event(
  171. self.room_id, self.user, True, [event_id_a]
  172. )
  173. event_id_sf2 = self.create_and_send_event(
  174. self.room_id, self.user, True, [event_id_a, event_id_b]
  175. )
  176. event_id_sf3 = self.create_and_send_event(
  177. self.room_id, self.user, True, [event_id_sf1]
  178. )
  179. self.create_and_send_event(
  180. self.room_id, self.user, True, [event_id_sf2, event_id_sf3]
  181. ) # SF4
  182. event_id_c = self.create_and_send_event(
  183. self.room_id, self.user, False, [event_id_sf3]
  184. )
  185. # Add the new extremity and check the latest events are as expected
  186. self.add_extremity(self.room_id, event_id_a)
  187. latest_event_ids = self.get_success(
  188. self.store.get_latest_event_ids_in_room(self.room_id)
  189. )
  190. self.assertEqual(set(latest_event_ids), {event_id_a, event_id_b, event_id_c})
  191. # Run the background update and check it did the right thing
  192. self.run_background_update()
  193. latest_event_ids = self.get_success(
  194. self.store.get_latest_event_ids_in_room(self.room_id)
  195. )
  196. self.assertEqual(set(latest_event_ids), {event_id_b, event_id_c})
  197. class CleanupExtremDummyEventsTestCase(HomeserverTestCase):
  198. CONSENT_VERSION = "1"
  199. EXTREMITIES_COUNT = 50
  200. servlets = [
  201. synapse.rest.admin.register_servlets_for_client_rest_resource,
  202. login.register_servlets,
  203. room.register_servlets,
  204. ]
  205. def make_homeserver(self, reactor, clock):
  206. config = self.default_config()
  207. config["cleanup_extremities_with_dummy_events"] = True
  208. return self.setup_test_homeserver(config=config)
  209. def prepare(self, reactor, clock, homeserver):
  210. self.store = homeserver.get_datastores().main
  211. self.room_creator = homeserver.get_room_creation_handler()
  212. self.event_creator_handler = homeserver.get_event_creation_handler()
  213. # Create a test user and room
  214. self.user = UserID.from_string(self.register_user("user1", "password"))
  215. self.token1 = self.login("user1", "password")
  216. self.requester = create_requester(self.user)
  217. info, _ = self.get_success(
  218. self.room_creator.create_room(self.requester, {"visibility": "public"})
  219. )
  220. self.room_id = info["room_id"]
  221. self.event_creator = homeserver.get_event_creation_handler()
  222. homeserver.config.consent.user_consent_version = self.CONSENT_VERSION
  223. def test_send_dummy_event(self):
  224. self._create_extremity_rich_graph()
  225. # Pump the reactor repeatedly so that the background updates have a
  226. # chance to run.
  227. self.pump(20)
  228. latest_event_ids = self.get_success(
  229. self.store.get_latest_event_ids_in_room(self.room_id)
  230. )
  231. self.assertTrue(len(latest_event_ids) < 10, len(latest_event_ids))
  232. @patch("synapse.handlers.message._DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY", new=0)
  233. def test_send_dummy_events_when_insufficient_power(self):
  234. self._create_extremity_rich_graph()
  235. # Criple power levels
  236. self.helper.send_state(
  237. self.room_id,
  238. EventTypes.PowerLevels,
  239. body={"users": {str(self.user): -1}},
  240. tok=self.token1,
  241. )
  242. # Pump the reactor repeatedly so that the background updates have a
  243. # chance to run.
  244. self.pump(10 * 60)
  245. latest_event_ids = self.get_success(
  246. self.store.get_latest_event_ids_in_room(self.room_id)
  247. )
  248. # Check that the room has not been pruned
  249. self.assertTrue(len(latest_event_ids) > 10)
  250. # New user with regular levels
  251. user2 = self.register_user("user2", "password")
  252. token2 = self.login("user2", "password")
  253. self.helper.join(self.room_id, user2, tok=token2)
  254. self.pump(10 * 60)
  255. latest_event_ids = self.get_success(
  256. self.store.get_latest_event_ids_in_room(self.room_id)
  257. )
  258. self.assertTrue(len(latest_event_ids) < 10, len(latest_event_ids))
  259. @patch("synapse.handlers.message._DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY", new=250)
  260. def test_expiry_logic(self):
  261. """Simple test to ensure that _expire_rooms_to_exclude_from_dummy_event_insertion()
  262. expires old entries correctly.
  263. """
  264. self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion[
  265. "1"
  266. ] = 100000
  267. self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion[
  268. "2"
  269. ] = 200000
  270. self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion[
  271. "3"
  272. ] = 300000
  273. self.event_creator_handler._expire_rooms_to_exclude_from_dummy_event_insertion()
  274. # All entries within time frame
  275. self.assertEqual(
  276. len(
  277. self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion
  278. ),
  279. 3,
  280. )
  281. # Oldest room to expire
  282. self.pump(1.01)
  283. self.event_creator_handler._expire_rooms_to_exclude_from_dummy_event_insertion()
  284. self.assertEqual(
  285. len(
  286. self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion
  287. ),
  288. 2,
  289. )
  290. # All rooms to expire
  291. self.pump(2)
  292. self.assertEqual(
  293. len(
  294. self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion
  295. ),
  296. 0,
  297. )
  298. def _create_extremity_rich_graph(self):
  299. """Helper method to create bushy graph on demand"""
  300. event_id_start = self.create_and_send_event(self.room_id, self.user)
  301. for _ in range(self.EXTREMITIES_COUNT):
  302. self.create_and_send_event(
  303. self.room_id, self.user, prev_event_ids=[event_id_start]
  304. )
  305. latest_event_ids = self.get_success(
  306. self.store.get_latest_event_ids_in_room(self.room_id)
  307. )
  308. self.assertEqual(len(latest_event_ids), 50)
  309. def _enable_consent_checking(self):
  310. """Helper method to enable consent checking"""
  311. self.event_creator._block_events_without_consent_error = "No consent from user"
  312. consent_uri_builder = Mock()
  313. consent_uri_builder.build_user_consent_uri.return_value = "http://example.com"
  314. self.event_creator._consent_uri_builder = consent_uri_builder