test_pusher_shard.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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 mock import Mock
  17. from twisted.internet import defer
  18. from synapse.rest import admin
  19. from synapse.rest.client.v1 import login, room
  20. from tests.replication._base import BaseMultiWorkerStreamTestCase
  21. logger = logging.getLogger(__name__)
  22. class PusherShardTestCase(BaseMultiWorkerStreamTestCase):
  23. """Checks pusher sharding works
  24. """
  25. servlets = [
  26. admin.register_servlets_for_client_rest_resource,
  27. room.register_servlets,
  28. login.register_servlets,
  29. ]
  30. def prepare(self, reactor, clock, hs):
  31. # Register a user who sends a message that we'll get notified about
  32. self.other_user_id = self.register_user("otheruser", "pass")
  33. self.other_access_token = self.login("otheruser", "pass")
  34. def default_config(self):
  35. conf = super().default_config()
  36. conf["start_pushers"] = False
  37. return conf
  38. def _create_pusher_and_send_msg(self, localpart):
  39. # Create a user that will get push notifications
  40. user_id = self.register_user(localpart, "pass")
  41. access_token = self.login(localpart, "pass")
  42. # Register a pusher
  43. user_dict = self.get_success(
  44. self.hs.get_datastore().get_user_by_access_token(access_token)
  45. )
  46. token_id = user_dict.token_id
  47. self.get_success(
  48. self.hs.get_pusherpool().add_pusher(
  49. user_id=user_id,
  50. access_token=token_id,
  51. kind="http",
  52. app_id="m.http",
  53. app_display_name="HTTP Push Notifications",
  54. device_display_name="pushy push",
  55. pushkey="a@example.com",
  56. lang=None,
  57. data={"url": "https://push.example.com/push"},
  58. )
  59. )
  60. self.pump()
  61. # Create a room
  62. room = self.helper.create_room_as(user_id, tok=access_token)
  63. # The other user joins
  64. self.helper.join(
  65. room=room, user=self.other_user_id, tok=self.other_access_token
  66. )
  67. # The other user sends some messages
  68. response = self.helper.send(room, body="Hi!", tok=self.other_access_token)
  69. event_id = response["event_id"]
  70. return event_id
  71. def test_send_push_single_worker(self):
  72. """Test that registration works when using a pusher worker.
  73. """
  74. http_client_mock = Mock(spec_set=["post_json_get_json"])
  75. http_client_mock.post_json_get_json.side_effect = lambda *_, **__: defer.succeed(
  76. {}
  77. )
  78. self.make_worker_hs(
  79. "synapse.app.pusher",
  80. {"start_pushers": True},
  81. proxied_http_client=http_client_mock,
  82. )
  83. event_id = self._create_pusher_and_send_msg("user")
  84. # Advance time a bit, so the pusher will register something has happened
  85. self.pump()
  86. http_client_mock.post_json_get_json.assert_called_once()
  87. self.assertEqual(
  88. http_client_mock.post_json_get_json.call_args[0][0],
  89. "https://push.example.com/push",
  90. )
  91. self.assertEqual(
  92. event_id,
  93. http_client_mock.post_json_get_json.call_args[0][1]["notification"][
  94. "event_id"
  95. ],
  96. )
  97. def test_send_push_multiple_workers(self):
  98. """Test that registration works when using sharded pusher workers.
  99. """
  100. http_client_mock1 = Mock(spec_set=["post_json_get_json"])
  101. http_client_mock1.post_json_get_json.side_effect = lambda *_, **__: defer.succeed(
  102. {}
  103. )
  104. self.make_worker_hs(
  105. "synapse.app.pusher",
  106. {
  107. "start_pushers": True,
  108. "worker_name": "pusher1",
  109. "pusher_instances": ["pusher1", "pusher2"],
  110. },
  111. proxied_http_client=http_client_mock1,
  112. )
  113. http_client_mock2 = Mock(spec_set=["post_json_get_json"])
  114. http_client_mock2.post_json_get_json.side_effect = lambda *_, **__: defer.succeed(
  115. {}
  116. )
  117. self.make_worker_hs(
  118. "synapse.app.pusher",
  119. {
  120. "start_pushers": True,
  121. "worker_name": "pusher2",
  122. "pusher_instances": ["pusher1", "pusher2"],
  123. },
  124. proxied_http_client=http_client_mock2,
  125. )
  126. # We choose a user name that we know should go to pusher1.
  127. event_id = self._create_pusher_and_send_msg("user2")
  128. # Advance time a bit, so the pusher will register something has happened
  129. self.pump()
  130. http_client_mock1.post_json_get_json.assert_called_once()
  131. http_client_mock2.post_json_get_json.assert_not_called()
  132. self.assertEqual(
  133. http_client_mock1.post_json_get_json.call_args[0][0],
  134. "https://push.example.com/push",
  135. )
  136. self.assertEqual(
  137. event_id,
  138. http_client_mock1.post_json_get_json.call_args[0][1]["notification"][
  139. "event_id"
  140. ],
  141. )
  142. http_client_mock1.post_json_get_json.reset_mock()
  143. http_client_mock2.post_json_get_json.reset_mock()
  144. # Now we choose a user name that we know should go to pusher2.
  145. event_id = self._create_pusher_and_send_msg("user4")
  146. # Advance time a bit, so the pusher will register something has happened
  147. self.pump()
  148. http_client_mock1.post_json_get_json.assert_not_called()
  149. http_client_mock2.post_json_get_json.assert_called_once()
  150. self.assertEqual(
  151. http_client_mock2.post_json_get_json.call_args[0][0],
  152. "https://push.example.com/push",
  153. )
  154. self.assertEqual(
  155. event_id,
  156. http_client_mock2.post_json_get_json.call_args[0][1]["notification"][
  157. "event_id"
  158. ],
  159. )