test_roommember.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2019 The Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from unittest.mock import Mock
  17. from synapse.api.constants import EventTypes, Membership
  18. from synapse.api.room_versions import RoomVersions
  19. from synapse.rest.admin import register_servlets_for_client_rest_resource
  20. from synapse.rest.client.v1 import login, room
  21. from synapse.types import Requester, UserID
  22. from tests import unittest
  23. class RoomMemberStoreTestCase(unittest.HomeserverTestCase):
  24. servlets = [
  25. login.register_servlets,
  26. register_servlets_for_client_rest_resource,
  27. room.register_servlets,
  28. ]
  29. def make_homeserver(self, reactor, clock):
  30. hs = self.setup_test_homeserver(
  31. resource_for_federation=Mock(), http_client=None
  32. )
  33. return hs
  34. def prepare(self, reactor, clock, hs):
  35. # We can't test the RoomMemberStore on its own without the other event
  36. # storage logic
  37. self.store = hs.get_datastore()
  38. self.event_builder_factory = hs.get_event_builder_factory()
  39. self.event_creation_handler = hs.get_event_creation_handler()
  40. self.u_alice = self.register_user("alice", "pass")
  41. self.t_alice = self.login("alice", "pass")
  42. self.u_bob = self.register_user("bob", "pass")
  43. # User elsewhere on another host
  44. self.u_charlie = UserID.from_string("@charlie:elsewhere")
  45. def inject_room_member(self, room, user, membership, replaces_state=None):
  46. builder = self.event_builder_factory.for_room_version(
  47. RoomVersions.V1,
  48. {
  49. "type": EventTypes.Member,
  50. "sender": user,
  51. "state_key": user,
  52. "room_id": room,
  53. "content": {"membership": membership},
  54. },
  55. )
  56. event, context = self.get_success(
  57. self.event_creation_handler.create_new_client_event(builder)
  58. )
  59. self.get_success(self.store.persist_event(event, context))
  60. return event
  61. def test_one_member(self):
  62. # Alice creates the room, and is automatically joined
  63. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  64. rooms_for_user = self.get_success(
  65. self.store.get_rooms_for_user_where_membership_is(
  66. self.u_alice, [Membership.JOIN]
  67. )
  68. )
  69. self.assertEquals([self.room], [m.room_id for m in rooms_for_user])
  70. def test_count_known_servers(self):
  71. """
  72. _count_known_servers will calculate how many servers are in a room.
  73. """
  74. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  75. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  76. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  77. servers = self.get_success(self.store._count_known_servers())
  78. self.assertEqual(servers, 2)
  79. def test_count_known_servers_stat_counter_disabled(self):
  80. """
  81. If enabled, the metrics for how many servers are known will be counted.
  82. """
  83. self.assertTrue("_known_servers_count" not in self.store.__dict__.keys())
  84. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  85. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  86. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  87. self.pump(20)
  88. self.assertTrue("_known_servers_count" not in self.store.__dict__.keys())
  89. @unittest.override_config(
  90. {"enable_metrics": True, "metrics_flags": {"known_servers": True}}
  91. )
  92. def test_count_known_servers_stat_counter_enabled(self):
  93. """
  94. If enabled, the metrics for how many servers are known will be counted.
  95. """
  96. # Initialises to 1 -- itself
  97. self.assertEqual(self.store._known_servers_count, 1)
  98. self.pump(20)
  99. # No rooms have been joined, so technically the SQL returns 0, but it
  100. # will still say it knows about itself.
  101. self.assertEqual(self.store._known_servers_count, 1)
  102. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  103. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  104. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  105. self.pump(20)
  106. # It now knows about Charlie's server.
  107. self.assertEqual(self.store._known_servers_count, 2)
  108. class CurrentStateMembershipUpdateTestCase(unittest.HomeserverTestCase):
  109. def prepare(self, reactor, clock, homeserver):
  110. self.store = homeserver.get_datastore()
  111. self.room_creator = homeserver.get_room_creation_handler()
  112. def test_can_rerun_update(self):
  113. # First make sure we have completed all updates.
  114. while not self.get_success(self.store.has_completed_background_updates()):
  115. self.get_success(self.store.do_next_background_update(100), by=0.1)
  116. # Now let's create a room, which will insert a membership
  117. user = UserID("alice", "test")
  118. requester = Requester(user, None, False, None, None)
  119. self.get_success(self.room_creator.create_room(requester, {}))
  120. # Register the background update to run again.
  121. self.get_success(
  122. self.store._simple_insert(
  123. table="background_updates",
  124. values={
  125. "update_name": "current_state_events_membership",
  126. "progress_json": "{}",
  127. "depends_on": None,
  128. },
  129. )
  130. )
  131. # ... and tell the DataStore that it hasn't finished all updates yet
  132. self.store._all_done = False
  133. # Now let's actually drive the updates to completion
  134. while not self.get_success(self.store.has_completed_background_updates()):
  135. self.get_success(self.store.do_next_background_update(100), by=0.1)