test_pusher_shard.py 6.4 KB

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