test_state.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. from twisted.internet import defer
  17. from synapse.api.constants import EventTypes, Membership
  18. from synapse.types import RoomID, UserID
  19. import tests.unittest
  20. import tests.utils
  21. logger = logging.getLogger(__name__)
  22. class StateStoreTestCase(tests.unittest.TestCase):
  23. def __init__(self, *args, **kwargs):
  24. super(StateStoreTestCase, self).__init__(*args, **kwargs)
  25. self.store = None # type: synapse.storage.DataStore
  26. @defer.inlineCallbacks
  27. def setUp(self):
  28. hs = yield tests.utils.setup_test_homeserver(self.addCleanup)
  29. self.store = hs.get_datastore()
  30. self.event_builder_factory = hs.get_event_builder_factory()
  31. self.event_creation_handler = hs.get_event_creation_handler()
  32. self.u_alice = UserID.from_string("@alice:test")
  33. self.u_bob = UserID.from_string("@bob:test")
  34. self.room = RoomID.from_string("!abc123:test")
  35. yield self.store.store_room(
  36. self.room.to_string(), room_creator_user_id="@creator:text", is_public=True
  37. )
  38. @defer.inlineCallbacks
  39. def inject_state_event(self, room, sender, typ, state_key, content):
  40. builder = self.event_builder_factory.new(
  41. {
  42. "type": typ,
  43. "sender": sender.to_string(),
  44. "state_key": state_key,
  45. "room_id": room.to_string(),
  46. "content": content,
  47. }
  48. )
  49. event, context = yield self.event_creation_handler.create_new_client_event(
  50. builder
  51. )
  52. yield self.store.persist_event(event, context)
  53. defer.returnValue(event)
  54. def assertStateMapEqual(self, s1, s2):
  55. for t in s1:
  56. # just compare event IDs for simplicity
  57. self.assertEqual(s1[t].event_id, s2[t].event_id)
  58. self.assertEqual(len(s1), len(s2))
  59. @defer.inlineCallbacks
  60. def test_get_state_for_event(self):
  61. # this defaults to a linear DAG as each new injection defaults to whatever
  62. # forward extremities are currently in the DB for this room.
  63. e1 = yield self.inject_state_event(
  64. self.room, self.u_alice, EventTypes.Create, '', {}
  65. )
  66. e2 = yield self.inject_state_event(
  67. self.room, self.u_alice, EventTypes.Name, '', {"name": "test room"}
  68. )
  69. e3 = yield self.inject_state_event(
  70. self.room,
  71. self.u_alice,
  72. EventTypes.Member,
  73. self.u_alice.to_string(),
  74. {"membership": Membership.JOIN},
  75. )
  76. e4 = yield self.inject_state_event(
  77. self.room,
  78. self.u_bob,
  79. EventTypes.Member,
  80. self.u_bob.to_string(),
  81. {"membership": Membership.JOIN},
  82. )
  83. e5 = yield self.inject_state_event(
  84. self.room,
  85. self.u_bob,
  86. EventTypes.Member,
  87. self.u_bob.to_string(),
  88. {"membership": Membership.LEAVE},
  89. )
  90. # check we get the full state as of the final event
  91. state = yield self.store.get_state_for_event(
  92. e5.event_id, None, filtered_types=None
  93. )
  94. self.assertIsNotNone(e4)
  95. self.assertStateMapEqual(
  96. {
  97. (e1.type, e1.state_key): e1,
  98. (e2.type, e2.state_key): e2,
  99. (e3.type, e3.state_key): e3,
  100. # e4 is overwritten by e5
  101. (e5.type, e5.state_key): e5,
  102. },
  103. state,
  104. )
  105. # check we can filter to the m.room.name event (with a '' state key)
  106. state = yield self.store.get_state_for_event(
  107. e5.event_id, [(EventTypes.Name, '')], filtered_types=None
  108. )
  109. self.assertStateMapEqual({(e2.type, e2.state_key): e2}, state)
  110. # check we can filter to the m.room.name event (with a wildcard None state key)
  111. state = yield self.store.get_state_for_event(
  112. e5.event_id, [(EventTypes.Name, None)], filtered_types=None
  113. )
  114. self.assertStateMapEqual({(e2.type, e2.state_key): e2}, state)
  115. # check we can grab the m.room.member events (with a wildcard None state key)
  116. state = yield self.store.get_state_for_event(
  117. e5.event_id, [(EventTypes.Member, None)], filtered_types=None
  118. )
  119. self.assertStateMapEqual(
  120. {(e3.type, e3.state_key): e3, (e5.type, e5.state_key): e5}, state
  121. )
  122. # check we can use filter_types to grab a specific room member
  123. # without filtering out the other event types
  124. state = yield self.store.get_state_for_event(
  125. e5.event_id,
  126. [(EventTypes.Member, self.u_alice.to_string())],
  127. filtered_types=[EventTypes.Member],
  128. )
  129. self.assertStateMapEqual(
  130. {
  131. (e1.type, e1.state_key): e1,
  132. (e2.type, e2.state_key): e2,
  133. (e3.type, e3.state_key): e3,
  134. },
  135. state,
  136. )
  137. # check that types=[], filtered_types=[EventTypes.Member]
  138. # doesn't return all members
  139. state = yield self.store.get_state_for_event(
  140. e5.event_id, [], filtered_types=[EventTypes.Member]
  141. )
  142. self.assertStateMapEqual(
  143. {(e1.type, e1.state_key): e1, (e2.type, e2.state_key): e2}, state
  144. )
  145. #######################################################
  146. # _get_some_state_from_cache tests against a full cache
  147. #######################################################
  148. room_id = self.room.to_string()
  149. group_ids = yield self.store.get_state_groups_ids(room_id, [e5.event_id])
  150. group = list(group_ids.keys())[0]
  151. # test _get_some_state_from_cache correctly filters out members with types=[]
  152. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  153. group, [], filtered_types=[EventTypes.Member]
  154. )
  155. self.assertEqual(is_all, True)
  156. self.assertDictEqual(
  157. {
  158. (e1.type, e1.state_key): e1.event_id,
  159. (e2.type, e2.state_key): e2.event_id,
  160. },
  161. state_dict,
  162. )
  163. # test _get_some_state_from_cache correctly filters in members with wildcard types
  164. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  165. group, [(EventTypes.Member, None)], filtered_types=[EventTypes.Member]
  166. )
  167. self.assertEqual(is_all, True)
  168. self.assertDictEqual(
  169. {
  170. (e1.type, e1.state_key): e1.event_id,
  171. (e2.type, e2.state_key): e2.event_id,
  172. (e3.type, e3.state_key): e3.event_id,
  173. # e4 is overwritten by e5
  174. (e5.type, e5.state_key): e5.event_id,
  175. },
  176. state_dict,
  177. )
  178. # test _get_some_state_from_cache correctly filters in members with specific types
  179. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  180. group,
  181. [(EventTypes.Member, e5.state_key)],
  182. filtered_types=[EventTypes.Member],
  183. )
  184. self.assertEqual(is_all, True)
  185. self.assertDictEqual(
  186. {
  187. (e1.type, e1.state_key): e1.event_id,
  188. (e2.type, e2.state_key): e2.event_id,
  189. (e5.type, e5.state_key): e5.event_id,
  190. },
  191. state_dict,
  192. )
  193. # test _get_some_state_from_cache correctly filters in members with specific types
  194. # and no filtered_types
  195. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  196. group, [(EventTypes.Member, e5.state_key)], filtered_types=None
  197. )
  198. self.assertEqual(is_all, True)
  199. self.assertDictEqual({(e5.type, e5.state_key): e5.event_id}, state_dict)
  200. #######################################################
  201. # deliberately remove e2 (room name) from the _state_group_cache
  202. (is_all, known_absent, state_dict_ids) = self.store._state_group_cache.get(
  203. group
  204. )
  205. self.assertEqual(is_all, True)
  206. self.assertEqual(known_absent, set())
  207. self.assertDictEqual(
  208. state_dict_ids,
  209. {
  210. (e1.type, e1.state_key): e1.event_id,
  211. (e2.type, e2.state_key): e2.event_id,
  212. (e3.type, e3.state_key): e3.event_id,
  213. # e4 is overwritten by e5
  214. (e5.type, e5.state_key): e5.event_id,
  215. },
  216. )
  217. state_dict_ids.pop((e2.type, e2.state_key))
  218. self.store._state_group_cache.invalidate(group)
  219. self.store._state_group_cache.update(
  220. sequence=self.store._state_group_cache.sequence,
  221. key=group,
  222. value=state_dict_ids,
  223. # list fetched keys so it knows it's partial
  224. fetched_keys=(
  225. (e1.type, e1.state_key),
  226. (e3.type, e3.state_key),
  227. (e5.type, e5.state_key),
  228. ),
  229. )
  230. (is_all, known_absent, state_dict_ids) = self.store._state_group_cache.get(
  231. group
  232. )
  233. self.assertEqual(is_all, False)
  234. self.assertEqual(
  235. known_absent,
  236. set(
  237. [
  238. (e1.type, e1.state_key),
  239. (e3.type, e3.state_key),
  240. (e5.type, e5.state_key),
  241. ]
  242. ),
  243. )
  244. self.assertDictEqual(
  245. state_dict_ids,
  246. {
  247. (e1.type, e1.state_key): e1.event_id,
  248. (e3.type, e3.state_key): e3.event_id,
  249. (e5.type, e5.state_key): e5.event_id,
  250. },
  251. )
  252. ############################################
  253. # test that things work with a partial cache
  254. # test _get_some_state_from_cache correctly filters out members with types=[]
  255. room_id = self.room.to_string()
  256. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  257. group, [], filtered_types=[EventTypes.Member]
  258. )
  259. self.assertEqual(is_all, False)
  260. self.assertDictEqual({(e1.type, e1.state_key): e1.event_id}, state_dict)
  261. # test _get_some_state_from_cache correctly filters in members wildcard types
  262. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  263. group, [(EventTypes.Member, None)], filtered_types=[EventTypes.Member]
  264. )
  265. self.assertEqual(is_all, False)
  266. self.assertDictEqual(
  267. {
  268. (e1.type, e1.state_key): e1.event_id,
  269. (e3.type, e3.state_key): e3.event_id,
  270. # e4 is overwritten by e5
  271. (e5.type, e5.state_key): e5.event_id,
  272. },
  273. state_dict,
  274. )
  275. # test _get_some_state_from_cache correctly filters in members with specific types
  276. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  277. group,
  278. [(EventTypes.Member, e5.state_key)],
  279. filtered_types=[EventTypes.Member],
  280. )
  281. self.assertEqual(is_all, False)
  282. self.assertDictEqual(
  283. {
  284. (e1.type, e1.state_key): e1.event_id,
  285. (e5.type, e5.state_key): e5.event_id,
  286. },
  287. state_dict,
  288. )
  289. # test _get_some_state_from_cache correctly filters in members with specific types
  290. # and no filtered_types
  291. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  292. group, [(EventTypes.Member, e5.state_key)], filtered_types=None
  293. )
  294. self.assertEqual(is_all, True)
  295. self.assertDictEqual({(e5.type, e5.state_key): e5.event_id}, state_dict)