test_state.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. # Copyright 2014-2016 OpenMarket Ltd
  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 Collection, Dict, List, Optional
  15. from unittest.mock import Mock
  16. from twisted.internet import defer
  17. from synapse.api.auth import Auth
  18. from synapse.api.constants import EventTypes, Membership
  19. from synapse.api.room_versions import RoomVersions
  20. from synapse.events import make_event_from_dict
  21. from synapse.events.snapshot import EventContext
  22. from synapse.state import StateHandler, StateResolutionHandler
  23. from tests import unittest
  24. from .utils import MockClock, default_config
  25. _next_event_id = 1000
  26. def create_event(
  27. name=None,
  28. type=None,
  29. state_key=None,
  30. depth=2,
  31. event_id=None,
  32. prev_events: Optional[List[str]] = None,
  33. **kwargs,
  34. ):
  35. global _next_event_id
  36. if not event_id:
  37. _next_event_id += 1
  38. event_id = "$%s:test" % (_next_event_id,)
  39. if not name:
  40. if state_key is not None:
  41. name = "<%s-%s, %s>" % (type, state_key, event_id)
  42. else:
  43. name = "<%s, %s>" % (type, event_id)
  44. d = {
  45. "event_id": event_id,
  46. "type": type,
  47. "sender": "@user_id:example.com",
  48. "room_id": "!room_id:example.com",
  49. "depth": depth,
  50. "prev_events": prev_events or [],
  51. }
  52. if state_key is not None:
  53. d["state_key"] = state_key
  54. d.update(kwargs)
  55. event = make_event_from_dict(d)
  56. return event
  57. class _DummyStore:
  58. def __init__(self):
  59. self._event_to_state_group = {}
  60. self._group_to_state = {}
  61. self._event_id_to_event = {}
  62. self._next_group = 1
  63. async def get_state_groups_ids(self, room_id, event_ids):
  64. groups = {}
  65. for event_id in event_ids:
  66. group = self._event_to_state_group.get(event_id)
  67. if group:
  68. groups[group] = self._group_to_state[group]
  69. return groups
  70. async def get_state_ids_for_group(self, state_group, state_filter=None):
  71. return self._group_to_state[state_group]
  72. async def store_state_group(
  73. self, event_id, room_id, prev_group, delta_ids, current_state_ids
  74. ):
  75. state_group = self._next_group
  76. self._next_group += 1
  77. self._group_to_state[state_group] = dict(current_state_ids)
  78. return state_group
  79. async def get_events(self, event_ids, **kwargs):
  80. return {
  81. e_id: self._event_id_to_event[e_id]
  82. for e_id in event_ids
  83. if e_id in self._event_id_to_event
  84. }
  85. async def get_partial_state_events(
  86. self, event_ids: Collection[str]
  87. ) -> Dict[str, bool]:
  88. return {e: False for e in event_ids}
  89. async def get_state_group_delta(self, name):
  90. return None, None
  91. def register_events(self, events):
  92. for e in events:
  93. self._event_id_to_event[e.event_id] = e
  94. def register_event_context(self, event, context):
  95. self._event_to_state_group[event.event_id] = context.state_group
  96. def register_event_id_state_group(self, event_id, state_group):
  97. self._event_to_state_group[event_id] = state_group
  98. async def get_room_version_id(self, room_id):
  99. return RoomVersions.V1.identifier
  100. async def get_state_group_for_events(self, event_ids):
  101. res = {}
  102. for event in event_ids:
  103. res[event] = self._event_to_state_group[event]
  104. return res
  105. async def get_state_for_groups(self, groups):
  106. res = {}
  107. for group in groups:
  108. state = self._group_to_state[group]
  109. res[group] = state
  110. return res
  111. class DictObj(dict):
  112. def __init__(self, **kwargs):
  113. super().__init__(kwargs)
  114. self.__dict__ = self
  115. class Graph:
  116. def __init__(self, nodes, edges):
  117. events = {}
  118. clobbered = set(events.keys())
  119. for event_id, fields in nodes.items():
  120. refs = edges.get(event_id)
  121. if refs:
  122. clobbered.difference_update(refs)
  123. prev_events = [(r, {}) for r in refs]
  124. else:
  125. prev_events = []
  126. events[event_id] = create_event(
  127. event_id=event_id, prev_events=prev_events, **fields
  128. )
  129. self._leaves = clobbered
  130. self._events = sorted(events.values(), key=lambda e: e.depth)
  131. def walk(self):
  132. return iter(self._events)
  133. def get_leaves(self):
  134. return (self._events[i] for i in self._leaves)
  135. class StateTestCase(unittest.TestCase):
  136. def setUp(self):
  137. self.dummy_store = _DummyStore()
  138. storage = Mock(main=self.dummy_store, state=self.dummy_store)
  139. hs = Mock(
  140. spec_set=[
  141. "config",
  142. "get_datastores",
  143. "get_storage",
  144. "get_auth",
  145. "get_state_handler",
  146. "get_clock",
  147. "get_state_resolution_handler",
  148. "get_account_validity_handler",
  149. "hostname",
  150. ]
  151. )
  152. hs.config = default_config("tesths", True)
  153. hs.get_datastores.return_value = Mock(main=self.dummy_store)
  154. hs.get_state_handler.return_value = None
  155. hs.get_clock.return_value = MockClock()
  156. hs.get_auth.return_value = Auth(hs)
  157. hs.get_state_resolution_handler = lambda: StateResolutionHandler(hs)
  158. hs.get_storage.return_value = storage
  159. self.state = StateHandler(hs)
  160. self.event_id = 0
  161. @defer.inlineCallbacks
  162. def test_branch_no_conflict(self):
  163. graph = Graph(
  164. nodes={
  165. "START": DictObj(
  166. type=EventTypes.Create, state_key="", content={}, depth=1
  167. ),
  168. "A": DictObj(type=EventTypes.Message, depth=2),
  169. "B": DictObj(type=EventTypes.Message, depth=3),
  170. "C": DictObj(type=EventTypes.Name, state_key="", depth=3),
  171. "D": DictObj(type=EventTypes.Message, depth=4),
  172. },
  173. edges={"A": ["START"], "B": ["A"], "C": ["A"], "D": ["B", "C"]},
  174. )
  175. self.dummy_store.register_events(graph.walk())
  176. context_store: dict[str, EventContext] = {}
  177. for event in graph.walk():
  178. context = yield defer.ensureDeferred(
  179. self.state.compute_event_context(event)
  180. )
  181. self.dummy_store.register_event_context(event, context)
  182. context_store[event.event_id] = context
  183. ctx_c = context_store["C"]
  184. ctx_d = context_store["D"]
  185. prev_state_ids = yield defer.ensureDeferred(ctx_d.get_prev_state_ids())
  186. self.assertEqual(2, len(prev_state_ids))
  187. self.assertEqual(ctx_c.state_group, ctx_d.state_group_before_event)
  188. self.assertEqual(ctx_d.state_group_before_event, ctx_d.state_group)
  189. @defer.inlineCallbacks
  190. def test_branch_basic_conflict(self):
  191. graph = Graph(
  192. nodes={
  193. "START": DictObj(
  194. type=EventTypes.Create,
  195. state_key="",
  196. content={"creator": "@user_id:example.com"},
  197. depth=1,
  198. ),
  199. "A": DictObj(
  200. type=EventTypes.Member,
  201. state_key="@user_id:example.com",
  202. content={"membership": Membership.JOIN},
  203. membership=Membership.JOIN,
  204. depth=2,
  205. ),
  206. "B": DictObj(type=EventTypes.Name, state_key="", depth=3),
  207. "C": DictObj(type=EventTypes.Name, state_key="", depth=4),
  208. "D": DictObj(type=EventTypes.Message, depth=5),
  209. },
  210. edges={"A": ["START"], "B": ["A"], "C": ["A"], "D": ["B", "C"]},
  211. )
  212. self.dummy_store.register_events(graph.walk())
  213. context_store = {}
  214. for event in graph.walk():
  215. context = yield defer.ensureDeferred(
  216. self.state.compute_event_context(event)
  217. )
  218. self.dummy_store.register_event_context(event, context)
  219. context_store[event.event_id] = context
  220. # C ends up winning the resolution between B and C
  221. ctx_c = context_store["C"]
  222. ctx_d = context_store["D"]
  223. prev_state_ids = yield defer.ensureDeferred(ctx_d.get_prev_state_ids())
  224. self.assertSetEqual({"START", "A", "C"}, set(prev_state_ids.values()))
  225. self.assertEqual(ctx_c.state_group, ctx_d.state_group_before_event)
  226. self.assertEqual(ctx_d.state_group_before_event, ctx_d.state_group)
  227. @defer.inlineCallbacks
  228. def test_branch_have_banned_conflict(self):
  229. graph = Graph(
  230. nodes={
  231. "START": DictObj(
  232. type=EventTypes.Create,
  233. state_key="",
  234. content={"creator": "@user_id:example.com"},
  235. depth=1,
  236. ),
  237. "A": DictObj(
  238. type=EventTypes.Member,
  239. state_key="@user_id:example.com",
  240. content={"membership": Membership.JOIN},
  241. membership=Membership.JOIN,
  242. depth=2,
  243. ),
  244. "B": DictObj(type=EventTypes.Name, state_key="", depth=3),
  245. "C": DictObj(
  246. type=EventTypes.Member,
  247. state_key="@user_id_2:example.com",
  248. content={"membership": Membership.BAN},
  249. membership=Membership.BAN,
  250. depth=4,
  251. ),
  252. "D": DictObj(
  253. type=EventTypes.Name,
  254. state_key="",
  255. depth=4,
  256. sender="@user_id_2:example.com",
  257. ),
  258. "E": DictObj(type=EventTypes.Message, depth=5),
  259. },
  260. edges={"A": ["START"], "B": ["A"], "C": ["B"], "D": ["B"], "E": ["C", "D"]},
  261. )
  262. self.dummy_store.register_events(graph.walk())
  263. context_store = {}
  264. for event in graph.walk():
  265. context = yield defer.ensureDeferred(
  266. self.state.compute_event_context(event)
  267. )
  268. self.dummy_store.register_event_context(event, context)
  269. context_store[event.event_id] = context
  270. # C ends up winning the resolution between C and D because bans win over other
  271. # changes
  272. ctx_c = context_store["C"]
  273. ctx_e = context_store["E"]
  274. prev_state_ids = yield defer.ensureDeferred(ctx_e.get_prev_state_ids())
  275. self.assertSetEqual({"START", "A", "B", "C"}, set(prev_state_ids.values()))
  276. self.assertEqual(ctx_c.state_group, ctx_e.state_group_before_event)
  277. self.assertEqual(ctx_e.state_group_before_event, ctx_e.state_group)
  278. @defer.inlineCallbacks
  279. def test_branch_have_perms_conflict(self):
  280. userid1 = "@user_id:example.com"
  281. userid2 = "@user_id2:example.com"
  282. nodes = {
  283. "A1": DictObj(
  284. type=EventTypes.Create,
  285. state_key="",
  286. content={"creator": userid1},
  287. depth=1,
  288. ),
  289. "A2": DictObj(
  290. type=EventTypes.Member,
  291. state_key=userid1,
  292. content={"membership": Membership.JOIN},
  293. membership=Membership.JOIN,
  294. ),
  295. "A3": DictObj(
  296. type=EventTypes.Member,
  297. state_key=userid2,
  298. content={"membership": Membership.JOIN},
  299. membership=Membership.JOIN,
  300. ),
  301. "A4": DictObj(
  302. type=EventTypes.PowerLevels,
  303. state_key="",
  304. content={
  305. "events": {"m.room.name": 50},
  306. "users": {userid1: 100, userid2: 60},
  307. },
  308. ),
  309. "A5": DictObj(type=EventTypes.Name, state_key=""),
  310. "B": DictObj(
  311. type=EventTypes.PowerLevels,
  312. state_key="",
  313. content={"events": {"m.room.name": 50}, "users": {userid2: 30}},
  314. ),
  315. "C": DictObj(type=EventTypes.Name, state_key="", sender=userid2),
  316. "D": DictObj(type=EventTypes.Message),
  317. }
  318. edges = {
  319. "A2": ["A1"],
  320. "A3": ["A2"],
  321. "A4": ["A3"],
  322. "A5": ["A4"],
  323. "B": ["A5"],
  324. "C": ["A5"],
  325. "D": ["B", "C"],
  326. }
  327. self._add_depths(nodes, edges)
  328. graph = Graph(nodes, edges)
  329. self.dummy_store.register_events(graph.walk())
  330. context_store = {}
  331. for event in graph.walk():
  332. context = yield defer.ensureDeferred(
  333. self.state.compute_event_context(event)
  334. )
  335. self.dummy_store.register_event_context(event, context)
  336. context_store[event.event_id] = context
  337. # B ends up winning the resolution between B and C because power levels
  338. # win over other changes.
  339. ctx_b = context_store["B"]
  340. ctx_d = context_store["D"]
  341. prev_state_ids = yield defer.ensureDeferred(ctx_d.get_prev_state_ids())
  342. self.assertSetEqual({"A1", "A2", "A3", "A5", "B"}, set(prev_state_ids.values()))
  343. self.assertEqual(ctx_b.state_group, ctx_d.state_group_before_event)
  344. self.assertEqual(ctx_d.state_group_before_event, ctx_d.state_group)
  345. def _add_depths(self, nodes, edges):
  346. def _get_depth(ev):
  347. node = nodes[ev]
  348. if "depth" not in node:
  349. prevs = edges[ev]
  350. depth = max(_get_depth(prev) for prev in prevs) + 1
  351. node["depth"] = depth
  352. return node["depth"]
  353. for n in nodes:
  354. _get_depth(n)
  355. @defer.inlineCallbacks
  356. def test_annotate_with_old_message(self):
  357. event = create_event(type="test_message", name="event")
  358. old_state = [
  359. create_event(type="test1", state_key="1"),
  360. create_event(type="test1", state_key="2"),
  361. create_event(type="test2", state_key=""),
  362. ]
  363. context = yield defer.ensureDeferred(
  364. self.state.compute_event_context(
  365. event,
  366. state_ids_before_event={
  367. (e.type, e.state_key): e.event_id for e in old_state
  368. },
  369. )
  370. )
  371. prev_state_ids = yield defer.ensureDeferred(context.get_prev_state_ids())
  372. self.assertCountEqual((e.event_id for e in old_state), prev_state_ids.values())
  373. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  374. self.assertCountEqual(
  375. (e.event_id for e in old_state), current_state_ids.values()
  376. )
  377. self.assertIsNotNone(context.state_group_before_event)
  378. self.assertEqual(context.state_group_before_event, context.state_group)
  379. @defer.inlineCallbacks
  380. def test_annotate_with_old_state(self):
  381. event = create_event(type="state", state_key="", name="event")
  382. old_state = [
  383. create_event(type="test1", state_key="1"),
  384. create_event(type="test1", state_key="2"),
  385. create_event(type="test2", state_key=""),
  386. ]
  387. context = yield defer.ensureDeferred(
  388. self.state.compute_event_context(
  389. event,
  390. state_ids_before_event={
  391. (e.type, e.state_key): e.event_id for e in old_state
  392. },
  393. )
  394. )
  395. prev_state_ids = yield defer.ensureDeferred(context.get_prev_state_ids())
  396. self.assertCountEqual((e.event_id for e in old_state), prev_state_ids.values())
  397. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  398. self.assertCountEqual(
  399. (e.event_id for e in old_state + [event]), current_state_ids.values()
  400. )
  401. self.assertIsNotNone(context.state_group_before_event)
  402. self.assertNotEqual(context.state_group_before_event, context.state_group)
  403. self.assertEqual(context.state_group_before_event, context.prev_group)
  404. self.assertEqual({("state", ""): event.event_id}, context.delta_ids)
  405. @defer.inlineCallbacks
  406. def test_trivial_annotate_message(self):
  407. prev_event_id = "prev_event_id"
  408. event = create_event(
  409. type="test_message", name="event2", prev_events=[(prev_event_id, {})]
  410. )
  411. old_state = [
  412. create_event(type="test1", state_key="1"),
  413. create_event(type="test1", state_key="2"),
  414. create_event(type="test2", state_key=""),
  415. ]
  416. group_name = yield defer.ensureDeferred(
  417. self.dummy_store.store_state_group(
  418. prev_event_id,
  419. event.room_id,
  420. None,
  421. None,
  422. {(e.type, e.state_key): e.event_id for e in old_state},
  423. )
  424. )
  425. self.dummy_store.register_event_id_state_group(prev_event_id, group_name)
  426. context = yield defer.ensureDeferred(self.state.compute_event_context(event))
  427. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  428. self.assertEqual(
  429. {e.event_id for e in old_state}, set(current_state_ids.values())
  430. )
  431. self.assertEqual(group_name, context.state_group)
  432. @defer.inlineCallbacks
  433. def test_trivial_annotate_state(self):
  434. prev_event_id = "prev_event_id"
  435. event = create_event(
  436. type="state", state_key="", name="event2", prev_events=[(prev_event_id, {})]
  437. )
  438. old_state = [
  439. create_event(type="test1", state_key="1"),
  440. create_event(type="test1", state_key="2"),
  441. create_event(type="test2", state_key=""),
  442. ]
  443. group_name = yield defer.ensureDeferred(
  444. self.dummy_store.store_state_group(
  445. prev_event_id,
  446. event.room_id,
  447. None,
  448. None,
  449. {(e.type, e.state_key): e.event_id for e in old_state},
  450. )
  451. )
  452. self.dummy_store.register_event_id_state_group(prev_event_id, group_name)
  453. context = yield defer.ensureDeferred(self.state.compute_event_context(event))
  454. prev_state_ids = yield defer.ensureDeferred(context.get_prev_state_ids())
  455. self.assertEqual({e.event_id for e in old_state}, set(prev_state_ids.values()))
  456. self.assertIsNotNone(context.state_group)
  457. @defer.inlineCallbacks
  458. def test_resolve_message_conflict(self):
  459. prev_event_id1 = "event_id1"
  460. prev_event_id2 = "event_id2"
  461. event = create_event(
  462. type="test_message",
  463. name="event3",
  464. prev_events=[(prev_event_id1, {}), (prev_event_id2, {})],
  465. )
  466. creation = create_event(type=EventTypes.Create, state_key="")
  467. old_state_1 = [
  468. creation,
  469. create_event(type="test1", state_key="1"),
  470. create_event(type="test1", state_key="2"),
  471. create_event(type="test2", state_key=""),
  472. ]
  473. old_state_2 = [
  474. creation,
  475. create_event(type="test1", state_key="1"),
  476. create_event(type="test3", state_key="2"),
  477. create_event(type="test4", state_key=""),
  478. ]
  479. self.dummy_store.register_events(old_state_1)
  480. self.dummy_store.register_events(old_state_2)
  481. context = yield self._get_context(
  482. event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
  483. )
  484. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  485. self.assertEqual(len(current_state_ids), 6)
  486. self.assertIsNotNone(context.state_group)
  487. @defer.inlineCallbacks
  488. def test_resolve_state_conflict(self):
  489. prev_event_id1 = "event_id1"
  490. prev_event_id2 = "event_id2"
  491. event = create_event(
  492. type="test4",
  493. state_key="",
  494. name="event",
  495. prev_events=[(prev_event_id1, {}), (prev_event_id2, {})],
  496. )
  497. creation = create_event(type=EventTypes.Create, state_key="")
  498. old_state_1 = [
  499. creation,
  500. create_event(type="test1", state_key="1"),
  501. create_event(type="test1", state_key="2"),
  502. create_event(type="test2", state_key=""),
  503. ]
  504. old_state_2 = [
  505. creation,
  506. create_event(type="test1", state_key="1"),
  507. create_event(type="test3", state_key="2"),
  508. create_event(type="test4", state_key=""),
  509. ]
  510. store = _DummyStore()
  511. store.register_events(old_state_1)
  512. store.register_events(old_state_2)
  513. self.dummy_store.get_events = store.get_events
  514. context = yield self._get_context(
  515. event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
  516. )
  517. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  518. self.assertEqual(len(current_state_ids), 6)
  519. self.assertIsNotNone(context.state_group)
  520. @defer.inlineCallbacks
  521. def test_standard_depth_conflict(self):
  522. prev_event_id1 = "event_id1"
  523. prev_event_id2 = "event_id2"
  524. event = create_event(
  525. type="test4",
  526. name="event",
  527. prev_events=[(prev_event_id1, {}), (prev_event_id2, {})],
  528. )
  529. member_event = create_event(
  530. type=EventTypes.Member,
  531. state_key="@user_id:example.com",
  532. content={"membership": Membership.JOIN},
  533. )
  534. power_levels = create_event(
  535. type=EventTypes.PowerLevels,
  536. state_key="",
  537. content={"users": {"@foo:bar": "100", "@user_id:example.com": "100"}},
  538. )
  539. creation = create_event(
  540. type=EventTypes.Create, state_key="", content={"creator": "@foo:bar"}
  541. )
  542. old_state_1 = [
  543. creation,
  544. power_levels,
  545. member_event,
  546. create_event(type="test1", state_key="1", depth=1),
  547. ]
  548. old_state_2 = [
  549. creation,
  550. power_levels,
  551. member_event,
  552. create_event(type="test1", state_key="1", depth=2),
  553. ]
  554. store = _DummyStore()
  555. store.register_events(old_state_1)
  556. store.register_events(old_state_2)
  557. self.dummy_store.get_events = store.get_events
  558. context = yield self._get_context(
  559. event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
  560. )
  561. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  562. self.assertEqual(old_state_2[3].event_id, current_state_ids[("test1", "1")])
  563. # Reverse the depth to make sure we are actually using the depths
  564. # during state resolution.
  565. old_state_1 = [
  566. creation,
  567. power_levels,
  568. member_event,
  569. create_event(type="test1", state_key="1", depth=2),
  570. ]
  571. old_state_2 = [
  572. creation,
  573. power_levels,
  574. member_event,
  575. create_event(type="test1", state_key="1", depth=1),
  576. ]
  577. store.register_events(old_state_1)
  578. store.register_events(old_state_2)
  579. context = yield self._get_context(
  580. event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
  581. )
  582. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  583. self.assertEqual(old_state_1[3].event_id, current_state_ids[("test1", "1")])
  584. @defer.inlineCallbacks
  585. def _get_context(
  586. self, event, prev_event_id_1, old_state_1, prev_event_id_2, old_state_2
  587. ):
  588. sg1 = yield defer.ensureDeferred(
  589. self.dummy_store.store_state_group(
  590. prev_event_id_1,
  591. event.room_id,
  592. None,
  593. None,
  594. {(e.type, e.state_key): e.event_id for e in old_state_1},
  595. )
  596. )
  597. self.dummy_store.register_event_id_state_group(prev_event_id_1, sg1)
  598. sg2 = yield defer.ensureDeferred(
  599. self.dummy_store.store_state_group(
  600. prev_event_id_2,
  601. event.room_id,
  602. None,
  603. None,
  604. {(e.type, e.state_key): e.event_id for e in old_state_2},
  605. )
  606. )
  607. self.dummy_store.register_event_id_state_group(prev_event_id_2, sg2)
  608. result = yield defer.ensureDeferred(self.state.compute_event_context(event))
  609. return result