test_pusher_shard.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from unittest.mock import Mock
  16. from twisted.internet import defer
  17. from synapse.rest import admin
  18. from synapse.rest.client import login, room
  19. from tests.replication._base import BaseMultiWorkerStreamTestCase
  20. logger = logging.getLogger(__name__)
  21. class PusherShardTestCase(BaseMultiWorkerStreamTestCase):
  22. """Checks pusher sharding works"""
  23. servlets = [
  24. admin.register_servlets_for_client_rest_resource,
  25. room.register_servlets,
  26. login.register_servlets,
  27. ]
  28. def prepare(self, reactor, clock, hs):
  29. # Register a user who sends a message that we'll get notified about
  30. self.other_user_id = self.register_user("otheruser", "pass")
  31. self.other_access_token = self.login("otheruser", "pass")
  32. def default_config(self):
  33. conf = super().default_config()
  34. conf["start_pushers"] = False
  35. return conf
  36. def _create_pusher_and_send_msg(self, localpart):
  37. # Create a user that will get push notifications
  38. user_id = self.register_user(localpart, "pass")
  39. access_token = self.login(localpart, "pass")
  40. # Register a pusher
  41. user_dict = self.get_success(
  42. self.hs.get_datastore().get_user_by_access_token(access_token)
  43. )
  44. token_id = user_dict.token_id
  45. self.get_success(
  46. self.hs.get_pusherpool().add_pusher(
  47. user_id=user_id,
  48. access_token=token_id,
  49. kind="http",
  50. app_id="m.http",
  51. app_display_name="HTTP Push Notifications",
  52. device_display_name="pushy push",
  53. pushkey="a@example.com",
  54. lang=None,
  55. data={"url": "https://push.example.com/_matrix/push/v1/notify"},
  56. )
  57. )
  58. self.pump()
  59. # Create a room
  60. room = self.helper.create_room_as(user_id, tok=access_token)
  61. # The other user joins
  62. self.helper.join(
  63. room=room, user=self.other_user_id, tok=self.other_access_token
  64. )
  65. # The other user sends some messages
  66. response = self.helper.send(room, body="Hi!", tok=self.other_access_token)
  67. event_id = response["event_id"]
  68. return event_id
  69. def test_send_push_single_worker(self):
  70. """Test that registration works when using a pusher worker."""
  71. http_client_mock = Mock(spec_set=["post_json_get_json"])
  72. http_client_mock.post_json_get_json.side_effect = (
  73. lambda *_, **__: defer.succeed({})
  74. )
  75. self.make_worker_hs(
  76. "synapse.app.pusher",
  77. {"start_pushers": False},
  78. proxied_blacklisted_http_client=http_client_mock,
  79. )
  80. event_id = self._create_pusher_and_send_msg("user")
  81. # Advance time a bit, so the pusher will register something has happened
  82. self.pump()
  83. http_client_mock.post_json_get_json.assert_called_once()
  84. self.assertEqual(
  85. http_client_mock.post_json_get_json.call_args[0][0],
  86. "https://push.example.com/_matrix/push/v1/notify",
  87. )
  88. self.assertEqual(
  89. event_id,
  90. http_client_mock.post_json_get_json.call_args[0][1]["notification"][
  91. "event_id"
  92. ],
  93. )
  94. def test_send_push_multiple_workers(self):
  95. """Test that registration works when using sharded pusher workers."""
  96. http_client_mock1 = Mock(spec_set=["post_json_get_json"])
  97. http_client_mock1.post_json_get_json.side_effect = (
  98. lambda *_, **__: defer.succeed({})
  99. )
  100. self.make_worker_hs(
  101. "synapse.app.pusher",
  102. {
  103. "start_pushers": True,
  104. "worker_name": "pusher1",
  105. "pusher_instances": ["pusher1", "pusher2"],
  106. },
  107. proxied_blacklisted_http_client=http_client_mock1,
  108. )
  109. http_client_mock2 = Mock(spec_set=["post_json_get_json"])
  110. http_client_mock2.post_json_get_json.side_effect = (
  111. lambda *_, **__: defer.succeed({})
  112. )
  113. self.make_worker_hs(
  114. "synapse.app.pusher",
  115. {
  116. "start_pushers": True,
  117. "worker_name": "pusher2",
  118. "pusher_instances": ["pusher1", "pusher2"],
  119. },
  120. proxied_blacklisted_http_client=http_client_mock2,
  121. )
  122. # We choose a user name that we know should go to pusher1.
  123. event_id = self._create_pusher_and_send_msg("user2")
  124. # Advance time a bit, so the pusher will register something has happened
  125. self.pump()
  126. http_client_mock1.post_json_get_json.assert_called_once()
  127. http_client_mock2.post_json_get_json.assert_not_called()
  128. self.assertEqual(
  129. http_client_mock1.post_json_get_json.call_args[0][0],
  130. "https://push.example.com/_matrix/push/v1/notify",
  131. )
  132. self.assertEqual(
  133. event_id,
  134. http_client_mock1.post_json_get_json.call_args[0][1]["notification"][
  135. "event_id"
  136. ],
  137. )
  138. http_client_mock1.post_json_get_json.reset_mock()
  139. http_client_mock2.post_json_get_json.reset_mock()
  140. # Now we choose a user name that we know should go to pusher2.
  141. event_id = self._create_pusher_and_send_msg("user4")
  142. # Advance time a bit, so the pusher will register something has happened
  143. self.pump()
  144. http_client_mock1.post_json_get_json.assert_not_called()
  145. http_client_mock2.post_json_get_json.assert_called_once()
  146. self.assertEqual(
  147. http_client_mock2.post_json_get_json.call_args[0][0],
  148. "https://push.example.com/_matrix/push/v1/notify",
  149. )
  150. self.assertEqual(
  151. event_id,
  152. http_client_mock2.post_json_get_json.call_args[0][1]["notification"][
  153. "event_id"
  154. ],
  155. )