1
0

test_event_federation.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 tests.unittest
  16. import tests.utils
  17. class EventFederationWorkerStoreTestCase(tests.unittest.HomeserverTestCase):
  18. def prepare(self, reactor, clock, hs):
  19. self.store = hs.get_datastore()
  20. def test_get_prev_events_for_room(self):
  21. room_id = "@ROOM:local"
  22. # add a bunch of events and hashes to act as forward extremities
  23. def insert_event(txn, i):
  24. event_id = "$event_%i:local" % i
  25. txn.execute(
  26. (
  27. "INSERT INTO events ("
  28. " room_id, event_id, type, depth, topological_ordering,"
  29. " content, processed, outlier, stream_ordering) "
  30. "VALUES (?, ?, 'm.test', ?, ?, 'test', ?, ?, ?)"
  31. ),
  32. (room_id, event_id, i, i, True, False, i),
  33. )
  34. txn.execute(
  35. (
  36. "INSERT INTO event_forward_extremities (room_id, event_id) "
  37. "VALUES (?, ?)"
  38. ),
  39. (room_id, event_id),
  40. )
  41. txn.execute(
  42. (
  43. "INSERT INTO event_reference_hashes "
  44. "(event_id, algorithm, hash) "
  45. "VALUES (?, 'sha256', ?)"
  46. ),
  47. (event_id, bytearray(b"ffff")),
  48. )
  49. for i in range(0, 20):
  50. self.get_success(self.store.db.runInteraction("insert", insert_event, i))
  51. # this should get the last ten
  52. r = self.get_success(self.store.get_prev_events_for_room(room_id))
  53. self.assertEqual(10, len(r))
  54. for i in range(0, 10):
  55. self.assertEqual("$event_%i:local" % (19 - i), r[i])
  56. def test_get_rooms_with_many_extremities(self):
  57. room1 = "#room1"
  58. room2 = "#room2"
  59. room3 = "#room3"
  60. def insert_event(txn, i, room_id):
  61. event_id = "$event_%i:local" % i
  62. txn.execute(
  63. (
  64. "INSERT INTO event_forward_extremities (room_id, event_id) "
  65. "VALUES (?, ?)"
  66. ),
  67. (room_id, event_id),
  68. )
  69. for i in range(0, 20):
  70. self.get_success(
  71. self.store.db.runInteraction("insert", insert_event, i, room1)
  72. )
  73. self.get_success(
  74. self.store.db.runInteraction("insert", insert_event, i, room2)
  75. )
  76. self.get_success(
  77. self.store.db.runInteraction("insert", insert_event, i, room3)
  78. )
  79. # Test simple case
  80. r = self.get_success(self.store.get_rooms_with_many_extremities(5, 5, []))
  81. self.assertEqual(len(r), 3)
  82. # Does filter work?
  83. r = self.get_success(self.store.get_rooms_with_many_extremities(5, 5, [room1]))
  84. self.assertTrue(room2 in r)
  85. self.assertTrue(room3 in r)
  86. self.assertEqual(len(r), 2)
  87. r = self.get_success(
  88. self.store.get_rooms_with_many_extremities(5, 5, [room1, room2])
  89. )
  90. self.assertEqual(r, [room3])
  91. # Does filter and limit work?
  92. r = self.get_success(self.store.get_rooms_with_many_extremities(5, 1, [room1]))
  93. self.assertTrue(r == [room2] or r == [room3])
  94. def test_auth_difference(self):
  95. room_id = "@ROOM:local"
  96. # The silly auth graph we use to test the auth difference algorithm,
  97. # where the top are the most recent events.
  98. #
  99. # A B
  100. # \ /
  101. # D E
  102. # \ |
  103. # ` F C
  104. # | /|
  105. # G ´ |
  106. # | \ |
  107. # H I
  108. # | |
  109. # K J
  110. auth_graph = {
  111. "a": ["e"],
  112. "b": ["e"],
  113. "c": ["g", "i"],
  114. "d": ["f"],
  115. "e": ["f"],
  116. "f": ["g"],
  117. "g": ["h", "i"],
  118. "h": ["k"],
  119. "i": ["j"],
  120. "k": [],
  121. "j": [],
  122. }
  123. depth_map = {
  124. "a": 7,
  125. "b": 7,
  126. "c": 4,
  127. "d": 6,
  128. "e": 6,
  129. "f": 5,
  130. "g": 3,
  131. "h": 2,
  132. "i": 2,
  133. "k": 1,
  134. "j": 1,
  135. }
  136. # We rudely fiddle with the appropriate tables directly, as that's much
  137. # easier than constructing events properly.
  138. def insert_event(txn, event_id, stream_ordering):
  139. depth = depth_map[event_id]
  140. self.store.db.simple_insert_txn(
  141. txn,
  142. table="events",
  143. values={
  144. "event_id": event_id,
  145. "room_id": room_id,
  146. "depth": depth,
  147. "topological_ordering": depth,
  148. "type": "m.test",
  149. "processed": True,
  150. "outlier": False,
  151. "stream_ordering": stream_ordering,
  152. },
  153. )
  154. self.store.db.simple_insert_many_txn(
  155. txn,
  156. table="event_auth",
  157. values=[
  158. {"event_id": event_id, "room_id": room_id, "auth_id": a}
  159. for a in auth_graph[event_id]
  160. ],
  161. )
  162. next_stream_ordering = 0
  163. for event_id in auth_graph:
  164. next_stream_ordering += 1
  165. self.get_success(
  166. self.store.db.runInteraction(
  167. "insert", insert_event, event_id, next_stream_ordering
  168. )
  169. )
  170. # Now actually test that various combinations give the right result:
  171. difference = self.get_success(
  172. self.store.get_auth_chain_difference([{"a"}, {"b"}])
  173. )
  174. self.assertSetEqual(difference, {"a", "b"})
  175. difference = self.get_success(
  176. self.store.get_auth_chain_difference([{"a"}, {"b"}, {"c"}])
  177. )
  178. self.assertSetEqual(difference, {"a", "b", "c", "e", "f"})
  179. difference = self.get_success(
  180. self.store.get_auth_chain_difference([{"a", "c"}, {"b"}])
  181. )
  182. self.assertSetEqual(difference, {"a", "b", "c"})
  183. difference = self.get_success(
  184. self.store.get_auth_chain_difference([{"a"}, {"b"}, {"d"}])
  185. )
  186. self.assertSetEqual(difference, {"a", "b", "d", "e"})
  187. difference = self.get_success(
  188. self.store.get_auth_chain_difference([{"a"}, {"b"}, {"c"}, {"d"}])
  189. )
  190. self.assertSetEqual(difference, {"a", "b", "c", "d", "e", "f"})
  191. difference = self.get_success(
  192. self.store.get_auth_chain_difference([{"a"}, {"b"}, {"e"}])
  193. )
  194. self.assertSetEqual(difference, {"a", "b"})
  195. difference = self.get_success(self.store.get_auth_chain_difference([{"a"}]))
  196. self.assertSetEqual(difference, set())