test_state.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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()
  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(),
  37. room_creator_user_id="@creator:text",
  38. is_public=True
  39. )
  40. @defer.inlineCallbacks
  41. def inject_state_event(self, room, sender, typ, state_key, content):
  42. builder = self.event_builder_factory.new({
  43. "type": typ,
  44. "sender": sender.to_string(),
  45. "state_key": state_key,
  46. "room_id": room.to_string(),
  47. "content": content,
  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, '', {
  68. "name": "test room"
  69. },
  70. )
  71. e3 = yield self.inject_state_event(
  72. self.room, self.u_alice, EventTypes.Member, self.u_alice.to_string(), {
  73. "membership": Membership.JOIN
  74. },
  75. )
  76. e4 = yield self.inject_state_event(
  77. self.room, self.u_bob, EventTypes.Member, self.u_bob.to_string(), {
  78. "membership": Membership.JOIN
  79. },
  80. )
  81. e5 = yield self.inject_state_event(
  82. self.room, self.u_bob, EventTypes.Member, self.u_bob.to_string(), {
  83. "membership": Membership.LEAVE
  84. },
  85. )
  86. # check we get the full state as of the final event
  87. state = yield self.store.get_state_for_event(
  88. e5.event_id, None, filtered_types=None
  89. )
  90. self.assertIsNotNone(e4)
  91. self.assertStateMapEqual({
  92. (e1.type, e1.state_key): e1,
  93. (e2.type, e2.state_key): e2,
  94. (e3.type, e3.state_key): e3,
  95. # e4 is overwritten by e5
  96. (e5.type, e5.state_key): e5,
  97. }, state)
  98. # check we can filter to the m.room.name event (with a '' state key)
  99. state = yield self.store.get_state_for_event(
  100. e5.event_id, [(EventTypes.Name, '')], filtered_types=None
  101. )
  102. self.assertStateMapEqual({
  103. (e2.type, e2.state_key): e2,
  104. }, state)
  105. # check we can filter to the m.room.name event (with a wildcard None state key)
  106. state = yield self.store.get_state_for_event(
  107. e5.event_id, [(EventTypes.Name, None)], filtered_types=None
  108. )
  109. self.assertStateMapEqual({
  110. (e2.type, e2.state_key): e2,
  111. }, state)
  112. # check we can grab the m.room.member events (with a wildcard None state key)
  113. state = yield self.store.get_state_for_event(
  114. e5.event_id, [(EventTypes.Member, None)], filtered_types=None
  115. )
  116. self.assertStateMapEqual({
  117. (e3.type, e3.state_key): e3,
  118. (e5.type, e5.state_key): e5,
  119. }, state)
  120. # check we can use filter_types to grab a specific room member
  121. # without filtering out the other event types
  122. state = yield self.store.get_state_for_event(
  123. e5.event_id, [(EventTypes.Member, self.u_alice.to_string())],
  124. filtered_types=[EventTypes.Member],
  125. )
  126. self.assertStateMapEqual({
  127. (e1.type, e1.state_key): e1,
  128. (e2.type, e2.state_key): e2,
  129. (e3.type, e3.state_key): e3,
  130. }, state)
  131. # check that types=[], filtered_types=[EventTypes.Member]
  132. # doesn't return all members
  133. state = yield self.store.get_state_for_event(
  134. e5.event_id, [], filtered_types=[EventTypes.Member],
  135. )
  136. self.assertStateMapEqual({
  137. (e1.type, e1.state_key): e1,
  138. (e2.type, e2.state_key): e2,
  139. }, state)
  140. #######################################################
  141. # _get_some_state_from_cache tests against a full cache
  142. #######################################################
  143. room_id = self.room.to_string()
  144. group_ids = yield self.store.get_state_groups_ids(room_id, [e5.event_id])
  145. group = group_ids.keys()[0]
  146. # test _get_some_state_from_cache correctly filters out members with types=[]
  147. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  148. group, [], filtered_types=[EventTypes.Member]
  149. )
  150. self.assertEqual(is_all, True)
  151. self.assertDictEqual({
  152. (e1.type, e1.state_key): e1.event_id,
  153. (e2.type, e2.state_key): e2.event_id,
  154. }, state_dict)
  155. # test _get_some_state_from_cache correctly filters in members with wildcard types
  156. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  157. group, [(EventTypes.Member, None)], filtered_types=[EventTypes.Member]
  158. )
  159. self.assertEqual(is_all, True)
  160. self.assertDictEqual({
  161. (e1.type, e1.state_key): e1.event_id,
  162. (e2.type, e2.state_key): e2.event_id,
  163. (e3.type, e3.state_key): e3.event_id,
  164. # e4 is overwritten by e5
  165. (e5.type, e5.state_key): e5.event_id,
  166. }, state_dict)
  167. # test _get_some_state_from_cache correctly filters in members with specific types
  168. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  169. group, [(EventTypes.Member, e5.state_key)], filtered_types=[EventTypes.Member]
  170. )
  171. self.assertEqual(is_all, True)
  172. self.assertDictEqual({
  173. (e1.type, e1.state_key): e1.event_id,
  174. (e2.type, e2.state_key): e2.event_id,
  175. (e5.type, e5.state_key): e5.event_id,
  176. }, state_dict)
  177. # test _get_some_state_from_cache correctly filters in members with specific types
  178. # and no filtered_types
  179. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  180. group, [(EventTypes.Member, e5.state_key)], filtered_types=None
  181. )
  182. self.assertEqual(is_all, True)
  183. self.assertDictEqual({
  184. (e5.type, e5.state_key): e5.event_id,
  185. }, state_dict)
  186. #######################################################
  187. # deliberately remove e2 (room name) from the _state_group_cache
  188. (is_all, known_absent, state_dict_ids) = self.store._state_group_cache.get(group)
  189. self.assertEqual(is_all, True)
  190. self.assertEqual(known_absent, set())
  191. self.assertDictEqual(state_dict_ids, {
  192. (e1.type, e1.state_key): e1.event_id,
  193. (e2.type, e2.state_key): e2.event_id,
  194. (e3.type, e3.state_key): e3.event_id,
  195. # e4 is overwritten by e5
  196. (e5.type, e5.state_key): e5.event_id,
  197. })
  198. state_dict_ids.pop((e2.type, e2.state_key))
  199. self.store._state_group_cache.invalidate(group)
  200. self.store._state_group_cache.update(
  201. sequence=self.store._state_group_cache.sequence,
  202. key=group,
  203. value=state_dict_ids,
  204. # list fetched keys so it knows it's partial
  205. fetched_keys=(
  206. (e1.type, e1.state_key),
  207. (e3.type, e3.state_key),
  208. (e5.type, e5.state_key),
  209. )
  210. )
  211. (is_all, known_absent, state_dict_ids) = self.store._state_group_cache.get(group)
  212. self.assertEqual(is_all, False)
  213. self.assertEqual(known_absent, set([
  214. (e1.type, e1.state_key),
  215. (e3.type, e3.state_key),
  216. (e5.type, e5.state_key),
  217. ]))
  218. self.assertDictEqual(state_dict_ids, {
  219. (e1.type, e1.state_key): e1.event_id,
  220. (e3.type, e3.state_key): e3.event_id,
  221. (e5.type, e5.state_key): e5.event_id,
  222. })
  223. ############################################
  224. # test that things work with a partial cache
  225. # test _get_some_state_from_cache correctly filters out members with types=[]
  226. room_id = self.room.to_string()
  227. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  228. group, [], filtered_types=[EventTypes.Member]
  229. )
  230. self.assertEqual(is_all, False)
  231. self.assertDictEqual({
  232. (e1.type, e1.state_key): e1.event_id,
  233. }, state_dict)
  234. # test _get_some_state_from_cache correctly filters in members wildcard types
  235. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  236. group, [(EventTypes.Member, None)], filtered_types=[EventTypes.Member]
  237. )
  238. self.assertEqual(is_all, False)
  239. self.assertDictEqual({
  240. (e1.type, e1.state_key): e1.event_id,
  241. (e3.type, e3.state_key): e3.event_id,
  242. # e4 is overwritten by e5
  243. (e5.type, e5.state_key): e5.event_id,
  244. }, state_dict)
  245. # test _get_some_state_from_cache correctly filters in members with specific types
  246. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  247. group, [(EventTypes.Member, e5.state_key)], filtered_types=[EventTypes.Member]
  248. )
  249. self.assertEqual(is_all, False)
  250. self.assertDictEqual({
  251. (e1.type, e1.state_key): e1.event_id,
  252. (e5.type, e5.state_key): e5.event_id,
  253. }, state_dict)
  254. # test _get_some_state_from_cache correctly filters in members with specific types
  255. # and no filtered_types
  256. (state_dict, is_all) = yield self.store._get_some_state_from_cache(
  257. group, [(EventTypes.Member, e5.state_key)], filtered_types=None
  258. )
  259. self.assertEqual(is_all, True)
  260. self.assertDictEqual({
  261. (e5.type, e5.state_key): e5.event_id,
  262. }, state_dict)