test_pusher_shard.py 6.3 KB

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