test_event_federation.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. from twisted.internet import defer
  16. import tests.unittest
  17. import tests.utils
  18. class EventFederationWorkerStoreTestCase(tests.unittest.TestCase):
  19. @defer.inlineCallbacks
  20. def setUp(self):
  21. hs = yield tests.utils.setup_test_homeserver(self.addCleanup)
  22. self.store = hs.get_datastore()
  23. @defer.inlineCallbacks
  24. def test_get_prev_events_for_room(self):
  25. room_id = "@ROOM:local"
  26. # add a bunch of events and hashes to act as forward extremities
  27. def insert_event(txn, i):
  28. event_id = "$event_%i:local" % i
  29. txn.execute(
  30. (
  31. "INSERT INTO events ("
  32. " room_id, event_id, type, depth, topological_ordering,"
  33. " content, processed, outlier, stream_ordering) "
  34. "VALUES (?, ?, 'm.test', ?, ?, 'test', ?, ?, ?)"
  35. ),
  36. (room_id, event_id, i, i, True, False, i),
  37. )
  38. txn.execute(
  39. (
  40. "INSERT INTO event_forward_extremities (room_id, event_id) "
  41. "VALUES (?, ?)"
  42. ),
  43. (room_id, event_id),
  44. )
  45. txn.execute(
  46. (
  47. "INSERT INTO event_reference_hashes "
  48. "(event_id, algorithm, hash) "
  49. "VALUES (?, 'sha256', ?)"
  50. ),
  51. (event_id, bytearray(b"ffff")),
  52. )
  53. for i in range(0, 11):
  54. yield self.store.runInteraction("insert", insert_event, i)
  55. # this should get the last five and five others
  56. r = yield self.store.get_prev_events_for_room(room_id)
  57. self.assertEqual(10, len(r))
  58. for i in range(0, 5):
  59. el = r[i]
  60. depth = el[2]
  61. self.assertEqual(10 - i, depth)
  62. for i in range(5, 5):
  63. el = r[i]
  64. depth = el[2]
  65. self.assertLessEqual(5, depth)
  66. @defer.inlineCallbacks
  67. def test_get_rooms_with_many_extremities(self):
  68. room1 = "#room1"
  69. room2 = "#room2"
  70. room3 = "#room3"
  71. def insert_event(txn, i, room_id):
  72. event_id = "$event_%i:local" % i
  73. txn.execute(
  74. (
  75. "INSERT INTO event_forward_extremities (room_id, event_id) "
  76. "VALUES (?, ?)"
  77. ),
  78. (room_id, event_id),
  79. )
  80. for i in range(0, 20):
  81. yield self.store.runInteraction("insert", insert_event, i, room1)
  82. yield self.store.runInteraction("insert", insert_event, i, room2)
  83. yield self.store.runInteraction("insert", insert_event, i, room3)
  84. # Test simple case
  85. r = yield self.store.get_rooms_with_many_extremities(5, 5, [])
  86. self.assertEqual(len(r), 3)
  87. # Does filter work?
  88. r = yield self.store.get_rooms_with_many_extremities(5, 5, [room1])
  89. self.assertTrue(room2 in r)
  90. self.assertTrue(room3 in r)
  91. self.assertEqual(len(r), 2)
  92. r = yield self.store.get_rooms_with_many_extremities(5, 5, [room1, room2])
  93. self.assertEqual(r, [room3])
  94. # Does filter and limit work?
  95. r = yield self.store.get_rooms_with_many_extremities(5, 1, [room1])
  96. self.assertTrue(r == [room2] or r == [room3])