test_roommember.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Membership
  18. from synapse.rest.admin import register_servlets_for_client_rest_resource
  19. from synapse.rest.client.v1 import login, room
  20. from synapse.types import Requester, UserID
  21. from tests import unittest
  22. class RoomMemberStoreTestCase(unittest.HomeserverTestCase):
  23. servlets = [
  24. login.register_servlets,
  25. register_servlets_for_client_rest_resource,
  26. room.register_servlets,
  27. ]
  28. def make_homeserver(self, reactor, clock):
  29. hs = self.setup_test_homeserver(
  30. resource_for_federation=Mock(), http_client=None
  31. )
  32. return hs
  33. def prepare(self, reactor, clock, hs):
  34. # We can't test the RoomMemberStore on its own without the other event
  35. # storage logic
  36. self.store = hs.get_datastore()
  37. self.u_alice = self.register_user("alice", "pass")
  38. self.t_alice = self.login("alice", "pass")
  39. self.u_bob = self.register_user("bob", "pass")
  40. # User elsewhere on another host
  41. self.u_charlie = UserID.from_string("@charlie:elsewhere")
  42. def test_one_member(self):
  43. # Alice creates the room, and is automatically joined
  44. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  45. rooms_for_user = self.get_success(
  46. self.store.get_rooms_for_user_where_membership_is(
  47. self.u_alice, [Membership.JOIN]
  48. )
  49. )
  50. self.assertEquals([self.room], [m.room_id for m in rooms_for_user])
  51. def test_count_known_servers(self):
  52. """
  53. _count_known_servers will calculate how many servers are in a room.
  54. """
  55. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  56. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  57. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  58. servers = self.get_success(self.store._count_known_servers())
  59. self.assertEqual(servers, 2)
  60. def test_count_known_servers_stat_counter_disabled(self):
  61. """
  62. If enabled, the metrics for how many servers are known will be counted.
  63. """
  64. self.assertTrue("_known_servers_count" not in self.store.__dict__.keys())
  65. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  66. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  67. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  68. self.pump(20)
  69. self.assertTrue("_known_servers_count" not in self.store.__dict__.keys())
  70. @unittest.override_config(
  71. {"enable_metrics": True, "metrics_flags": {"known_servers": True}}
  72. )
  73. def test_count_known_servers_stat_counter_enabled(self):
  74. """
  75. If enabled, the metrics for how many servers are known will be counted.
  76. """
  77. # Initialises to 1 -- itself
  78. self.assertEqual(self.store._known_servers_count, 1)
  79. self.pump(20)
  80. # No rooms have been joined, so technically the SQL returns 0, but it
  81. # will still say it knows about itself.
  82. self.assertEqual(self.store._known_servers_count, 1)
  83. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  84. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  85. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  86. self.pump(20)
  87. # It now knows about Charlie's server.
  88. self.assertEqual(self.store._known_servers_count, 2)
  89. class CurrentStateMembershipUpdateTestCase(unittest.HomeserverTestCase):
  90. def prepare(self, reactor, clock, homeserver):
  91. self.store = homeserver.get_datastore()
  92. self.room_creator = homeserver.get_room_creation_handler()
  93. def test_can_rerun_update(self):
  94. # First make sure we have completed all updates.
  95. while not self.get_success(self.store.has_completed_background_updates()):
  96. self.get_success(self.store.do_next_background_update(100), by=0.1)
  97. # Now let's create a room, which will insert a membership
  98. user = UserID("alice", "test")
  99. requester = Requester(user, None, False, None, None)
  100. self.get_success(self.room_creator.create_room(requester, {}))
  101. # Register the background update to run again.
  102. self.get_success(
  103. self.store._simple_insert(
  104. table="background_updates",
  105. values={
  106. "update_name": "current_state_events_membership",
  107. "progress_json": "{}",
  108. "depends_on": None,
  109. },
  110. )
  111. )
  112. # ... and tell the DataStore that it hasn't finished all updates yet
  113. self.store._all_done = False
  114. # Now let's actually drive the updates to completion
  115. while not self.get_success(self.store.has_completed_background_updates()):
  116. self.get_success(self.store.do_next_background_update(100), by=0.1)