test_state.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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):
  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(event, old_state=old_state)
  365. )
  366. prev_state_ids = yield defer.ensureDeferred(context.get_prev_state_ids())
  367. self.assertCountEqual((e.event_id for e in old_state), prev_state_ids.values())
  368. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  369. self.assertCountEqual(
  370. (e.event_id for e in old_state), current_state_ids.values()
  371. )
  372. self.assertIsNotNone(context.state_group_before_event)
  373. self.assertEqual(context.state_group_before_event, context.state_group)
  374. @defer.inlineCallbacks
  375. def test_annotate_with_old_state(self):
  376. event = create_event(type="state", state_key="", name="event")
  377. old_state = [
  378. create_event(type="test1", state_key="1"),
  379. create_event(type="test1", state_key="2"),
  380. create_event(type="test2", state_key=""),
  381. ]
  382. context = yield defer.ensureDeferred(
  383. self.state.compute_event_context(event, old_state=old_state)
  384. )
  385. prev_state_ids = yield defer.ensureDeferred(context.get_prev_state_ids())
  386. self.assertCountEqual((e.event_id for e in old_state), prev_state_ids.values())
  387. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  388. self.assertCountEqual(
  389. (e.event_id for e in old_state + [event]), current_state_ids.values()
  390. )
  391. self.assertIsNotNone(context.state_group_before_event)
  392. self.assertNotEqual(context.state_group_before_event, context.state_group)
  393. self.assertEqual(context.state_group_before_event, context.prev_group)
  394. self.assertEqual({("state", ""): event.event_id}, context.delta_ids)
  395. @defer.inlineCallbacks
  396. def test_trivial_annotate_message(self):
  397. prev_event_id = "prev_event_id"
  398. event = create_event(
  399. type="test_message", name="event2", prev_events=[(prev_event_id, {})]
  400. )
  401. old_state = [
  402. create_event(type="test1", state_key="1"),
  403. create_event(type="test1", state_key="2"),
  404. create_event(type="test2", state_key=""),
  405. ]
  406. group_name = yield defer.ensureDeferred(
  407. self.dummy_store.store_state_group(
  408. prev_event_id,
  409. event.room_id,
  410. None,
  411. None,
  412. {(e.type, e.state_key): e.event_id for e in old_state},
  413. )
  414. )
  415. self.dummy_store.register_event_id_state_group(prev_event_id, group_name)
  416. context = yield defer.ensureDeferred(self.state.compute_event_context(event))
  417. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  418. self.assertEqual(
  419. {e.event_id for e in old_state}, set(current_state_ids.values())
  420. )
  421. self.assertEqual(group_name, context.state_group)
  422. @defer.inlineCallbacks
  423. def test_trivial_annotate_state(self):
  424. prev_event_id = "prev_event_id"
  425. event = create_event(
  426. type="state", state_key="", name="event2", prev_events=[(prev_event_id, {})]
  427. )
  428. old_state = [
  429. create_event(type="test1", state_key="1"),
  430. create_event(type="test1", state_key="2"),
  431. create_event(type="test2", state_key=""),
  432. ]
  433. group_name = yield defer.ensureDeferred(
  434. self.dummy_store.store_state_group(
  435. prev_event_id,
  436. event.room_id,
  437. None,
  438. None,
  439. {(e.type, e.state_key): e.event_id for e in old_state},
  440. )
  441. )
  442. self.dummy_store.register_event_id_state_group(prev_event_id, group_name)
  443. context = yield defer.ensureDeferred(self.state.compute_event_context(event))
  444. prev_state_ids = yield defer.ensureDeferred(context.get_prev_state_ids())
  445. self.assertEqual({e.event_id for e in old_state}, set(prev_state_ids.values()))
  446. self.assertIsNotNone(context.state_group)
  447. @defer.inlineCallbacks
  448. def test_resolve_message_conflict(self):
  449. prev_event_id1 = "event_id1"
  450. prev_event_id2 = "event_id2"
  451. event = create_event(
  452. type="test_message",
  453. name="event3",
  454. prev_events=[(prev_event_id1, {}), (prev_event_id2, {})],
  455. )
  456. creation = create_event(type=EventTypes.Create, state_key="")
  457. old_state_1 = [
  458. creation,
  459. create_event(type="test1", state_key="1"),
  460. create_event(type="test1", state_key="2"),
  461. create_event(type="test2", state_key=""),
  462. ]
  463. old_state_2 = [
  464. creation,
  465. create_event(type="test1", state_key="1"),
  466. create_event(type="test3", state_key="2"),
  467. create_event(type="test4", state_key=""),
  468. ]
  469. self.dummy_store.register_events(old_state_1)
  470. self.dummy_store.register_events(old_state_2)
  471. context = yield self._get_context(
  472. event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
  473. )
  474. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  475. self.assertEqual(len(current_state_ids), 6)
  476. self.assertIsNotNone(context.state_group)
  477. @defer.inlineCallbacks
  478. def test_resolve_state_conflict(self):
  479. prev_event_id1 = "event_id1"
  480. prev_event_id2 = "event_id2"
  481. event = create_event(
  482. type="test4",
  483. state_key="",
  484. name="event",
  485. prev_events=[(prev_event_id1, {}), (prev_event_id2, {})],
  486. )
  487. creation = create_event(type=EventTypes.Create, state_key="")
  488. old_state_1 = [
  489. creation,
  490. create_event(type="test1", state_key="1"),
  491. create_event(type="test1", state_key="2"),
  492. create_event(type="test2", state_key=""),
  493. ]
  494. old_state_2 = [
  495. creation,
  496. create_event(type="test1", state_key="1"),
  497. create_event(type="test3", state_key="2"),
  498. create_event(type="test4", state_key=""),
  499. ]
  500. store = _DummyStore()
  501. store.register_events(old_state_1)
  502. store.register_events(old_state_2)
  503. self.dummy_store.get_events = store.get_events
  504. context = yield self._get_context(
  505. event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
  506. )
  507. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  508. self.assertEqual(len(current_state_ids), 6)
  509. self.assertIsNotNone(context.state_group)
  510. @defer.inlineCallbacks
  511. def test_standard_depth_conflict(self):
  512. prev_event_id1 = "event_id1"
  513. prev_event_id2 = "event_id2"
  514. event = create_event(
  515. type="test4",
  516. name="event",
  517. prev_events=[(prev_event_id1, {}), (prev_event_id2, {})],
  518. )
  519. member_event = create_event(
  520. type=EventTypes.Member,
  521. state_key="@user_id:example.com",
  522. content={"membership": Membership.JOIN},
  523. )
  524. power_levels = create_event(
  525. type=EventTypes.PowerLevels,
  526. state_key="",
  527. content={"users": {"@foo:bar": "100", "@user_id:example.com": "100"}},
  528. )
  529. creation = create_event(
  530. type=EventTypes.Create, state_key="", content={"creator": "@foo:bar"}
  531. )
  532. old_state_1 = [
  533. creation,
  534. power_levels,
  535. member_event,
  536. create_event(type="test1", state_key="1", depth=1),
  537. ]
  538. old_state_2 = [
  539. creation,
  540. power_levels,
  541. member_event,
  542. create_event(type="test1", state_key="1", depth=2),
  543. ]
  544. store = _DummyStore()
  545. store.register_events(old_state_1)
  546. store.register_events(old_state_2)
  547. self.dummy_store.get_events = store.get_events
  548. context = yield self._get_context(
  549. event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
  550. )
  551. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  552. self.assertEqual(old_state_2[3].event_id, current_state_ids[("test1", "1")])
  553. # Reverse the depth to make sure we are actually using the depths
  554. # during state resolution.
  555. old_state_1 = [
  556. creation,
  557. power_levels,
  558. member_event,
  559. create_event(type="test1", state_key="1", depth=2),
  560. ]
  561. old_state_2 = [
  562. creation,
  563. power_levels,
  564. member_event,
  565. create_event(type="test1", state_key="1", depth=1),
  566. ]
  567. store.register_events(old_state_1)
  568. store.register_events(old_state_2)
  569. context = yield self._get_context(
  570. event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
  571. )
  572. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  573. self.assertEqual(old_state_1[3].event_id, current_state_ids[("test1", "1")])
  574. @defer.inlineCallbacks
  575. def _get_context(
  576. self, event, prev_event_id_1, old_state_1, prev_event_id_2, old_state_2
  577. ):
  578. sg1 = yield defer.ensureDeferred(
  579. self.dummy_store.store_state_group(
  580. prev_event_id_1,
  581. event.room_id,
  582. None,
  583. None,
  584. {(e.type, e.state_key): e.event_id for e in old_state_1},
  585. )
  586. )
  587. self.dummy_store.register_event_id_state_group(prev_event_id_1, sg1)
  588. sg2 = yield defer.ensureDeferred(
  589. self.dummy_store.store_state_group(
  590. prev_event_id_2,
  591. event.room_id,
  592. None,
  593. None,
  594. {(e.type, e.state_key): e.event_id for e in old_state_2},
  595. )
  596. )
  597. self.dummy_store.register_event_id_state_group(prev_event_id_2, sg2)
  598. result = yield defer.ensureDeferred(self.state.compute_event_context(event))
  599. return result