1
0

test_event_chain.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. # Copyright 2020 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. from typing import Dict, List, Set, Tuple
  15. from twisted.test.proto_helpers import MemoryReactor
  16. from twisted.trial import unittest
  17. from synapse.api.constants import EventTypes
  18. from synapse.api.room_versions import RoomVersions
  19. from synapse.events import EventBase
  20. from synapse.events.snapshot import EventContext
  21. from synapse.rest import admin
  22. from synapse.rest.client import login, room
  23. from synapse.server import HomeServer
  24. from synapse.storage.database import LoggingTransaction
  25. from synapse.storage.databases.main.events import _LinkMap
  26. from synapse.storage.types import Cursor
  27. from synapse.types import create_requester
  28. from synapse.util import Clock
  29. from tests.unittest import HomeserverTestCase
  30. class EventChainStoreTestCase(HomeserverTestCase):
  31. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  32. self.store = hs.get_datastores().main
  33. self._next_stream_ordering = 1
  34. def test_simple(self) -> None:
  35. """Test that the example in `docs/auth_chain_difference_algorithm.md`
  36. works.
  37. """
  38. event_factory = self.hs.get_event_builder_factory()
  39. bob = "@creator:test"
  40. alice = "@alice:test"
  41. room_id = "!room:test"
  42. # Ensure that we have a rooms entry so that we generate the chain index.
  43. self.get_success(
  44. self.store.store_room(
  45. room_id=room_id,
  46. room_creator_user_id="",
  47. is_public=True,
  48. room_version=RoomVersions.V6,
  49. )
  50. )
  51. create = self.get_success(
  52. event_factory.for_room_version(
  53. RoomVersions.V6,
  54. {
  55. "type": EventTypes.Create,
  56. "state_key": "",
  57. "sender": bob,
  58. "room_id": room_id,
  59. "content": {"tag": "create"},
  60. },
  61. ).build(prev_event_ids=[], auth_event_ids=[])
  62. )
  63. bob_join = self.get_success(
  64. event_factory.for_room_version(
  65. RoomVersions.V6,
  66. {
  67. "type": EventTypes.Member,
  68. "state_key": bob,
  69. "sender": bob,
  70. "room_id": room_id,
  71. "content": {"tag": "bob_join"},
  72. },
  73. ).build(prev_event_ids=[], auth_event_ids=[create.event_id])
  74. )
  75. power = self.get_success(
  76. event_factory.for_room_version(
  77. RoomVersions.V6,
  78. {
  79. "type": EventTypes.PowerLevels,
  80. "state_key": "",
  81. "sender": bob,
  82. "room_id": room_id,
  83. "content": {"tag": "power"},
  84. },
  85. ).build(
  86. prev_event_ids=[],
  87. auth_event_ids=[create.event_id, bob_join.event_id],
  88. )
  89. )
  90. alice_invite = self.get_success(
  91. event_factory.for_room_version(
  92. RoomVersions.V6,
  93. {
  94. "type": EventTypes.Member,
  95. "state_key": alice,
  96. "sender": bob,
  97. "room_id": room_id,
  98. "content": {"tag": "alice_invite"},
  99. },
  100. ).build(
  101. prev_event_ids=[],
  102. auth_event_ids=[create.event_id, bob_join.event_id, power.event_id],
  103. )
  104. )
  105. alice_join = self.get_success(
  106. event_factory.for_room_version(
  107. RoomVersions.V6,
  108. {
  109. "type": EventTypes.Member,
  110. "state_key": alice,
  111. "sender": alice,
  112. "room_id": room_id,
  113. "content": {"tag": "alice_join"},
  114. },
  115. ).build(
  116. prev_event_ids=[],
  117. auth_event_ids=[create.event_id, alice_invite.event_id, power.event_id],
  118. )
  119. )
  120. power_2 = self.get_success(
  121. event_factory.for_room_version(
  122. RoomVersions.V6,
  123. {
  124. "type": EventTypes.PowerLevels,
  125. "state_key": "",
  126. "sender": bob,
  127. "room_id": room_id,
  128. "content": {"tag": "power_2"},
  129. },
  130. ).build(
  131. prev_event_ids=[],
  132. auth_event_ids=[create.event_id, bob_join.event_id, power.event_id],
  133. )
  134. )
  135. bob_join_2 = self.get_success(
  136. event_factory.for_room_version(
  137. RoomVersions.V6,
  138. {
  139. "type": EventTypes.Member,
  140. "state_key": bob,
  141. "sender": bob,
  142. "room_id": room_id,
  143. "content": {"tag": "bob_join_2"},
  144. },
  145. ).build(
  146. prev_event_ids=[],
  147. auth_event_ids=[create.event_id, bob_join.event_id, power.event_id],
  148. )
  149. )
  150. alice_join2 = self.get_success(
  151. event_factory.for_room_version(
  152. RoomVersions.V6,
  153. {
  154. "type": EventTypes.Member,
  155. "state_key": alice,
  156. "sender": alice,
  157. "room_id": room_id,
  158. "content": {"tag": "alice_join2"},
  159. },
  160. ).build(
  161. prev_event_ids=[],
  162. auth_event_ids=[
  163. create.event_id,
  164. alice_join.event_id,
  165. power_2.event_id,
  166. ],
  167. )
  168. )
  169. events = [
  170. create,
  171. bob_join,
  172. power,
  173. alice_invite,
  174. alice_join,
  175. bob_join_2,
  176. power_2,
  177. alice_join2,
  178. ]
  179. expected_links = [
  180. (bob_join, create),
  181. (power, create),
  182. (power, bob_join),
  183. (alice_invite, create),
  184. (alice_invite, power),
  185. (alice_invite, bob_join),
  186. (bob_join_2, power),
  187. (alice_join2, power_2),
  188. ]
  189. self.persist(events)
  190. chain_map, link_map = self.fetch_chains(events)
  191. # Check that the expected links and only the expected links have been
  192. # added.
  193. self.assertEqual(len(expected_links), len(list(link_map.get_additions())))
  194. for start, end in expected_links:
  195. start_id, start_seq = chain_map[start.event_id]
  196. end_id, end_seq = chain_map[end.event_id]
  197. self.assertIn(
  198. (start_seq, end_seq), list(link_map.get_links_between(start_id, end_id))
  199. )
  200. # Test that everything can reach the create event, but the create event
  201. # can't reach anything.
  202. for event in events[1:]:
  203. self.assertTrue(
  204. link_map.exists_path_from(
  205. chain_map[event.event_id], chain_map[create.event_id]
  206. ),
  207. )
  208. self.assertFalse(
  209. link_map.exists_path_from(
  210. chain_map[create.event_id],
  211. chain_map[event.event_id],
  212. ),
  213. )
  214. def test_out_of_order_events(self) -> None:
  215. """Test that we handle persisting events that we don't have the full
  216. auth chain for yet (which should only happen for out of band memberships).
  217. """
  218. event_factory = self.hs.get_event_builder_factory()
  219. bob = "@creator:test"
  220. alice = "@alice:test"
  221. room_id = "!room:test"
  222. # Ensure that we have a rooms entry so that we generate the chain index.
  223. self.get_success(
  224. self.store.store_room(
  225. room_id=room_id,
  226. room_creator_user_id="",
  227. is_public=True,
  228. room_version=RoomVersions.V6,
  229. )
  230. )
  231. # First persist the base room.
  232. create = self.get_success(
  233. event_factory.for_room_version(
  234. RoomVersions.V6,
  235. {
  236. "type": EventTypes.Create,
  237. "state_key": "",
  238. "sender": bob,
  239. "room_id": room_id,
  240. "content": {"tag": "create"},
  241. },
  242. ).build(prev_event_ids=[], auth_event_ids=[])
  243. )
  244. bob_join = self.get_success(
  245. event_factory.for_room_version(
  246. RoomVersions.V6,
  247. {
  248. "type": EventTypes.Member,
  249. "state_key": bob,
  250. "sender": bob,
  251. "room_id": room_id,
  252. "content": {"tag": "bob_join"},
  253. },
  254. ).build(prev_event_ids=[], auth_event_ids=[create.event_id])
  255. )
  256. power = self.get_success(
  257. event_factory.for_room_version(
  258. RoomVersions.V6,
  259. {
  260. "type": EventTypes.PowerLevels,
  261. "state_key": "",
  262. "sender": bob,
  263. "room_id": room_id,
  264. "content": {"tag": "power"},
  265. },
  266. ).build(
  267. prev_event_ids=[],
  268. auth_event_ids=[create.event_id, bob_join.event_id],
  269. )
  270. )
  271. self.persist([create, bob_join, power])
  272. # Now persist an invite and a couple of memberships out of order.
  273. alice_invite = self.get_success(
  274. event_factory.for_room_version(
  275. RoomVersions.V6,
  276. {
  277. "type": EventTypes.Member,
  278. "state_key": alice,
  279. "sender": bob,
  280. "room_id": room_id,
  281. "content": {"tag": "alice_invite"},
  282. },
  283. ).build(
  284. prev_event_ids=[],
  285. auth_event_ids=[create.event_id, bob_join.event_id, power.event_id],
  286. )
  287. )
  288. alice_join = self.get_success(
  289. event_factory.for_room_version(
  290. RoomVersions.V6,
  291. {
  292. "type": EventTypes.Member,
  293. "state_key": alice,
  294. "sender": alice,
  295. "room_id": room_id,
  296. "content": {"tag": "alice_join"},
  297. },
  298. ).build(
  299. prev_event_ids=[],
  300. auth_event_ids=[create.event_id, alice_invite.event_id, power.event_id],
  301. )
  302. )
  303. alice_join2 = self.get_success(
  304. event_factory.for_room_version(
  305. RoomVersions.V6,
  306. {
  307. "type": EventTypes.Member,
  308. "state_key": alice,
  309. "sender": alice,
  310. "room_id": room_id,
  311. "content": {"tag": "alice_join2"},
  312. },
  313. ).build(
  314. prev_event_ids=[],
  315. auth_event_ids=[create.event_id, alice_join.event_id, power.event_id],
  316. )
  317. )
  318. self.persist([alice_join])
  319. self.persist([alice_join2])
  320. self.persist([alice_invite])
  321. # The end result should be sane.
  322. events = [create, bob_join, power, alice_invite, alice_join]
  323. chain_map, link_map = self.fetch_chains(events)
  324. expected_links = [
  325. (bob_join, create),
  326. (power, create),
  327. (power, bob_join),
  328. (alice_invite, create),
  329. (alice_invite, power),
  330. (alice_invite, bob_join),
  331. ]
  332. # Check that the expected links and only the expected links have been
  333. # added.
  334. self.assertEqual(len(expected_links), len(list(link_map.get_additions())))
  335. for start, end in expected_links:
  336. start_id, start_seq = chain_map[start.event_id]
  337. end_id, end_seq = chain_map[end.event_id]
  338. self.assertIn(
  339. (start_seq, end_seq), list(link_map.get_links_between(start_id, end_id))
  340. )
  341. def persist(
  342. self,
  343. events: List[EventBase],
  344. ) -> None:
  345. """Persist the given events and check that the links generated match
  346. those given.
  347. """
  348. persist_events_store = self.hs.get_datastores().persist_events
  349. for e in events:
  350. e.internal_metadata.stream_ordering = self._next_stream_ordering
  351. self._next_stream_ordering += 1
  352. def _persist(txn: LoggingTransaction) -> None:
  353. # We need to persist the events to the events and state_events
  354. # tables.
  355. persist_events_store._store_event_txn(
  356. txn,
  357. [(e, EventContext(self.hs.get_storage_controllers())) for e in events],
  358. )
  359. # Actually call the function that calculates the auth chain stuff.
  360. persist_events_store._persist_event_auth_chain_txn(txn, events)
  361. self.get_success(
  362. persist_events_store.db_pool.runInteraction(
  363. "_persist",
  364. _persist,
  365. )
  366. )
  367. def fetch_chains(
  368. self, events: List[EventBase]
  369. ) -> Tuple[Dict[str, Tuple[int, int]], _LinkMap]:
  370. # Fetch the map from event ID -> (chain ID, sequence number)
  371. rows = self.get_success(
  372. self.store.db_pool.simple_select_many_batch(
  373. table="event_auth_chains",
  374. column="event_id",
  375. iterable=[e.event_id for e in events],
  376. retcols=("event_id", "chain_id", "sequence_number"),
  377. keyvalues={},
  378. )
  379. )
  380. chain_map = {
  381. row["event_id"]: (row["chain_id"], row["sequence_number"]) for row in rows
  382. }
  383. # Fetch all the links and pass them to the _LinkMap.
  384. rows = self.get_success(
  385. self.store.db_pool.simple_select_many_batch(
  386. table="event_auth_chain_links",
  387. column="origin_chain_id",
  388. iterable=[chain_id for chain_id, _ in chain_map.values()],
  389. retcols=(
  390. "origin_chain_id",
  391. "origin_sequence_number",
  392. "target_chain_id",
  393. "target_sequence_number",
  394. ),
  395. keyvalues={},
  396. )
  397. )
  398. link_map = _LinkMap()
  399. for row in rows:
  400. added = link_map.add_link(
  401. (row["origin_chain_id"], row["origin_sequence_number"]),
  402. (row["target_chain_id"], row["target_sequence_number"]),
  403. )
  404. # We shouldn't have persisted any redundant links
  405. self.assertTrue(added)
  406. return chain_map, link_map
  407. class LinkMapTestCase(unittest.TestCase):
  408. def test_simple(self) -> None:
  409. """Basic tests for the LinkMap."""
  410. link_map = _LinkMap()
  411. link_map.add_link((1, 1), (2, 1), new=False)
  412. self.assertCountEqual(link_map.get_links_between(1, 2), [(1, 1)])
  413. self.assertCountEqual(link_map.get_links_from((1, 1)), [(2, 1)])
  414. self.assertCountEqual(link_map.get_additions(), [])
  415. self.assertTrue(link_map.exists_path_from((1, 5), (2, 1)))
  416. self.assertFalse(link_map.exists_path_from((1, 5), (2, 2)))
  417. self.assertTrue(link_map.exists_path_from((1, 5), (1, 1)))
  418. self.assertFalse(link_map.exists_path_from((1, 1), (1, 5)))
  419. # Attempting to add a redundant link is ignored.
  420. self.assertFalse(link_map.add_link((1, 4), (2, 1)))
  421. self.assertCountEqual(link_map.get_links_between(1, 2), [(1, 1)])
  422. # Adding new non-redundant links works
  423. self.assertTrue(link_map.add_link((1, 3), (2, 3)))
  424. self.assertCountEqual(link_map.get_links_between(1, 2), [(1, 1), (3, 3)])
  425. self.assertTrue(link_map.add_link((2, 5), (1, 3)))
  426. self.assertCountEqual(link_map.get_links_between(2, 1), [(5, 3)])
  427. self.assertCountEqual(link_map.get_links_between(1, 2), [(1, 1), (3, 3)])
  428. self.assertCountEqual(link_map.get_additions(), [(1, 3, 2, 3), (2, 5, 1, 3)])
  429. class EventChainBackgroundUpdateTestCase(HomeserverTestCase):
  430. servlets = [
  431. admin.register_servlets,
  432. room.register_servlets,
  433. login.register_servlets,
  434. ]
  435. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  436. self.store = hs.get_datastores().main
  437. self.user_id = self.register_user("foo", "pass")
  438. self.token = self.login("foo", "pass")
  439. self.requester = create_requester(self.user_id)
  440. def _generate_room(self) -> Tuple[str, List[Set[str]]]:
  441. """Insert a room without a chain cover index."""
  442. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  443. # Mark the room as not having a chain cover index
  444. self.get_success(
  445. self.store.db_pool.simple_update(
  446. table="rooms",
  447. keyvalues={"room_id": room_id},
  448. updatevalues={"has_auth_chain_index": False},
  449. desc="test",
  450. )
  451. )
  452. # Create a fork in the DAG with different events.
  453. event_handler = self.hs.get_event_creation_handler()
  454. latest_event_ids = self.get_success(
  455. self.store.get_prev_events_for_room(room_id)
  456. )
  457. event, context = self.get_success(
  458. event_handler.create_event(
  459. self.requester,
  460. {
  461. "type": "some_state_type",
  462. "state_key": "",
  463. "content": {},
  464. "room_id": room_id,
  465. "sender": self.user_id,
  466. },
  467. prev_event_ids=latest_event_ids,
  468. )
  469. )
  470. self.get_success(
  471. event_handler.handle_new_client_event(
  472. self.requester, events_and_context=[(event, context)]
  473. )
  474. )
  475. state1 = set(self.get_success(context.get_current_state_ids()).values())
  476. event, context = self.get_success(
  477. event_handler.create_event(
  478. self.requester,
  479. {
  480. "type": "some_state_type",
  481. "state_key": "",
  482. "content": {},
  483. "room_id": room_id,
  484. "sender": self.user_id,
  485. },
  486. prev_event_ids=latest_event_ids,
  487. )
  488. )
  489. self.get_success(
  490. event_handler.handle_new_client_event(
  491. self.requester, events_and_context=[(event, context)]
  492. )
  493. )
  494. state2 = set(self.get_success(context.get_current_state_ids()).values())
  495. # Delete the chain cover info.
  496. def _delete_tables(txn: Cursor) -> None:
  497. txn.execute("DELETE FROM event_auth_chains")
  498. txn.execute("DELETE FROM event_auth_chain_links")
  499. self.get_success(self.store.db_pool.runInteraction("test", _delete_tables))
  500. return room_id, [state1, state2]
  501. def test_background_update_single_room(self) -> None:
  502. """Test that the background update to calculate auth chains for historic
  503. rooms works correctly.
  504. """
  505. # Create a room
  506. room_id, states = self._generate_room()
  507. # Insert and run the background update.
  508. self.get_success(
  509. self.store.db_pool.simple_insert(
  510. "background_updates",
  511. {"update_name": "chain_cover", "progress_json": "{}"},
  512. )
  513. )
  514. # Ugh, have to reset this flag
  515. self.store.db_pool.updates._all_done = False
  516. self.wait_for_background_updates()
  517. # Test that the `has_auth_chain_index` has been set
  518. self.assertTrue(self.get_success(self.store.has_auth_chain_index(room_id)))
  519. # Test that calculating the auth chain difference using the newly
  520. # calculated chain cover works.
  521. self.get_success(
  522. self.store.db_pool.runInteraction(
  523. "test",
  524. self.store._get_auth_chain_difference_using_cover_index_txn,
  525. room_id,
  526. states,
  527. )
  528. )
  529. def test_background_update_multiple_rooms(self) -> None:
  530. """Test that the background update to calculate auth chains for historic
  531. rooms works correctly.
  532. """
  533. # Create a room
  534. room_id1, states1 = self._generate_room()
  535. room_id2, states2 = self._generate_room()
  536. room_id3, states2 = self._generate_room()
  537. # Insert and run the background update.
  538. self.get_success(
  539. self.store.db_pool.simple_insert(
  540. "background_updates",
  541. {"update_name": "chain_cover", "progress_json": "{}"},
  542. )
  543. )
  544. # Ugh, have to reset this flag
  545. self.store.db_pool.updates._all_done = False
  546. self.wait_for_background_updates()
  547. # Test that the `has_auth_chain_index` has been set
  548. self.assertTrue(self.get_success(self.store.has_auth_chain_index(room_id1)))
  549. self.assertTrue(self.get_success(self.store.has_auth_chain_index(room_id2)))
  550. self.assertTrue(self.get_success(self.store.has_auth_chain_index(room_id3)))
  551. # Test that calculating the auth chain difference using the newly
  552. # calculated chain cover works.
  553. self.get_success(
  554. self.store.db_pool.runInteraction(
  555. "test",
  556. self.store._get_auth_chain_difference_using_cover_index_txn,
  557. room_id1,
  558. states1,
  559. )
  560. )
  561. def test_background_update_single_large_room(self) -> None:
  562. """Test that the background update to calculate auth chains for historic
  563. rooms works correctly.
  564. """
  565. # Create a room
  566. room_id, states = self._generate_room()
  567. # Add a bunch of state so that it takes multiple iterations of the
  568. # background update to process the room.
  569. for i in range(0, 150):
  570. self.helper.send_state(
  571. room_id, event_type="m.test", body={"index": i}, tok=self.token
  572. )
  573. # Insert and run the background update.
  574. self.get_success(
  575. self.store.db_pool.simple_insert(
  576. "background_updates",
  577. {"update_name": "chain_cover", "progress_json": "{}"},
  578. )
  579. )
  580. # Ugh, have to reset this flag
  581. self.store.db_pool.updates._all_done = False
  582. iterations = 0
  583. while not self.get_success(
  584. self.store.db_pool.updates.has_completed_background_updates()
  585. ):
  586. iterations += 1
  587. self.get_success(
  588. self.store.db_pool.updates.do_next_background_update(False), by=0.1
  589. )
  590. # Ensure that we did actually take multiple iterations to process the
  591. # room.
  592. self.assertGreater(iterations, 1)
  593. # Test that the `has_auth_chain_index` has been set
  594. self.assertTrue(self.get_success(self.store.has_auth_chain_index(room_id)))
  595. # Test that calculating the auth chain difference using the newly
  596. # calculated chain cover works.
  597. self.get_success(
  598. self.store.db_pool.runInteraction(
  599. "test",
  600. self.store._get_auth_chain_difference_using_cover_index_txn,
  601. room_id,
  602. states,
  603. )
  604. )
  605. def test_background_update_multiple_large_room(self) -> None:
  606. """Test that the background update to calculate auth chains for historic
  607. rooms works correctly.
  608. """
  609. # Create the rooms
  610. room_id1, _ = self._generate_room()
  611. room_id2, _ = self._generate_room()
  612. # Add a bunch of state so that it takes multiple iterations of the
  613. # background update to process the room.
  614. for i in range(0, 150):
  615. self.helper.send_state(
  616. room_id1, event_type="m.test", body={"index": i}, tok=self.token
  617. )
  618. for i in range(0, 150):
  619. self.helper.send_state(
  620. room_id2, event_type="m.test", body={"index": i}, tok=self.token
  621. )
  622. # Insert and run the background update.
  623. self.get_success(
  624. self.store.db_pool.simple_insert(
  625. "background_updates",
  626. {"update_name": "chain_cover", "progress_json": "{}"},
  627. )
  628. )
  629. # Ugh, have to reset this flag
  630. self.store.db_pool.updates._all_done = False
  631. iterations = 0
  632. while not self.get_success(
  633. self.store.db_pool.updates.has_completed_background_updates()
  634. ):
  635. iterations += 1
  636. self.get_success(
  637. self.store.db_pool.updates.do_next_background_update(False), by=0.1
  638. )
  639. # Ensure that we did actually take multiple iterations to process the
  640. # room.
  641. self.assertGreater(iterations, 1)
  642. # Test that the `has_auth_chain_index` has been set
  643. self.assertTrue(self.get_success(self.store.has_auth_chain_index(room_id1)))
  644. self.assertTrue(self.get_success(self.store.has_auth_chain_index(room_id2)))