2020-07-16 07:06:28 -06:00
|
|
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
import logging
|
2021-04-09 11:44:38 -06:00
|
|
|
from unittest.mock import Mock
|
2020-07-16 07:06:28 -06:00
|
|
|
|
|
|
|
from twisted.internet import defer
|
2023-02-06 07:55:00 -07:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
2020-07-16 07:06:28 -06:00
|
|
|
|
|
|
|
from synapse.rest import admin
|
2021-08-17 05:57:58 -06:00
|
|
|
from synapse.rest.client import login, room
|
2023-02-06 07:55:00 -07:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.util import Clock
|
2020-07-16 07:06:28 -06:00
|
|
|
|
|
|
|
from tests.replication._base import BaseMultiWorkerStreamTestCase
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class PusherShardTestCase(BaseMultiWorkerStreamTestCase):
|
|
|
|
"""Checks pusher sharding works"""
|
|
|
|
|
|
|
|
servlets = [
|
|
|
|
admin.register_servlets_for_client_rest_resource,
|
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
2023-02-06 07:55:00 -07:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2020-07-16 07:06:28 -06:00
|
|
|
# Register a user who sends a message that we'll get notified about
|
|
|
|
self.other_user_id = self.register_user("otheruser", "pass")
|
|
|
|
self.other_access_token = self.login("otheruser", "pass")
|
|
|
|
|
2023-02-06 07:55:00 -07:00
|
|
|
def _create_pusher_and_send_msg(self, localpart: str) -> str:
|
2020-07-16 07:06:28 -06:00
|
|
|
# Create a user that will get push notifications
|
|
|
|
user_id = self.register_user(localpart, "pass")
|
|
|
|
access_token = self.login(localpart, "pass")
|
|
|
|
|
|
|
|
# Register a pusher
|
|
|
|
user_dict = self.get_success(
|
2022-02-23 04:04:02 -07:00
|
|
|
self.hs.get_datastores().main.get_user_by_access_token(access_token)
|
2020-07-16 07:06:28 -06:00
|
|
|
)
|
2023-02-14 12:03:35 -07:00
|
|
|
assert user_dict is not None
|
2023-03-24 09:09:39 -06:00
|
|
|
device_id = user_dict.device_id
|
2020-07-16 07:06:28 -06:00
|
|
|
|
|
|
|
self.get_success(
|
2022-09-21 08:39:01 -06:00
|
|
|
self.hs.get_pusherpool().add_or_update_pusher(
|
2020-07-16 07:06:28 -06:00
|
|
|
user_id=user_id,
|
2023-03-24 09:09:39 -06:00
|
|
|
device_id=device_id,
|
2020-07-16 07:06:28 -06:00
|
|
|
kind="http",
|
|
|
|
app_id="m.http",
|
|
|
|
app_display_name="HTTP Push Notifications",
|
|
|
|
device_display_name="pushy push",
|
|
|
|
pushkey="a@example.com",
|
|
|
|
lang=None,
|
2020-12-04 08:51:56 -07:00
|
|
|
data={"url": "https://push.example.com/_matrix/push/v1/notify"},
|
2020-07-16 07:06:28 -06:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# Create a room
|
|
|
|
room = self.helper.create_room_as(user_id, tok=access_token)
|
|
|
|
|
|
|
|
# The other user joins
|
|
|
|
self.helper.join(
|
|
|
|
room=room, user=self.other_user_id, tok=self.other_access_token
|
|
|
|
)
|
|
|
|
|
|
|
|
# The other user sends some messages
|
|
|
|
response = self.helper.send(room, body="Hi!", tok=self.other_access_token)
|
|
|
|
event_id = response["event_id"]
|
|
|
|
|
|
|
|
return event_id
|
|
|
|
|
2023-02-06 07:55:00 -07:00
|
|
|
def test_send_push_single_worker(self) -> None:
|
2020-07-16 07:06:28 -06:00
|
|
|
"""Test that registration works when using a pusher worker."""
|
|
|
|
http_client_mock = Mock(spec_set=["post_json_get_json"])
|
|
|
|
http_client_mock.post_json_get_json.side_effect = (
|
|
|
|
lambda *_, **__: defer.succeed({})
|
|
|
|
)
|
|
|
|
|
|
|
|
self.make_worker_hs(
|
2022-12-01 05:38:27 -07:00
|
|
|
"synapse.app.generic_worker",
|
|
|
|
{"worker_name": "pusher1", "pusher_instances": ["pusher1"]},
|
2023-05-03 06:09:20 -06:00
|
|
|
proxied_blacklisted_http_client=http_client_mock,
|
2020-07-16 07:06:28 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
event_id = self._create_pusher_and_send_msg("user")
|
|
|
|
|
|
|
|
# Advance time a bit, so the pusher will register something has happened
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
http_client_mock.post_json_get_json.assert_called_once()
|
|
|
|
self.assertEqual(
|
|
|
|
http_client_mock.post_json_get_json.call_args[0][0],
|
2020-12-04 08:51:56 -07:00
|
|
|
"https://push.example.com/_matrix/push/v1/notify",
|
2020-07-16 07:06:28 -06:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
event_id,
|
|
|
|
http_client_mock.post_json_get_json.call_args[0][1]["notification"][
|
|
|
|
"event_id"
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2023-02-06 07:55:00 -07:00
|
|
|
def test_send_push_multiple_workers(self) -> None:
|
2020-07-16 07:06:28 -06:00
|
|
|
"""Test that registration works when using sharded pusher workers."""
|
|
|
|
http_client_mock1 = Mock(spec_set=["post_json_get_json"])
|
|
|
|
http_client_mock1.post_json_get_json.side_effect = (
|
|
|
|
lambda *_, **__: defer.succeed({})
|
|
|
|
)
|
|
|
|
|
|
|
|
self.make_worker_hs(
|
2022-12-01 05:38:27 -07:00
|
|
|
"synapse.app.generic_worker",
|
2020-07-16 07:06:28 -06:00
|
|
|
{
|
|
|
|
"worker_name": "pusher1",
|
|
|
|
"pusher_instances": ["pusher1", "pusher2"],
|
|
|
|
},
|
2023-05-03 06:09:20 -06:00
|
|
|
proxied_blacklisted_http_client=http_client_mock1,
|
2020-07-16 07:06:28 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
http_client_mock2 = Mock(spec_set=["post_json_get_json"])
|
|
|
|
http_client_mock2.post_json_get_json.side_effect = (
|
|
|
|
lambda *_, **__: defer.succeed({})
|
|
|
|
)
|
|
|
|
|
|
|
|
self.make_worker_hs(
|
2022-12-01 05:38:27 -07:00
|
|
|
"synapse.app.generic_worker",
|
2020-07-16 07:06:28 -06:00
|
|
|
{
|
|
|
|
"worker_name": "pusher2",
|
|
|
|
"pusher_instances": ["pusher1", "pusher2"],
|
|
|
|
},
|
2023-05-03 06:09:20 -06:00
|
|
|
proxied_blacklisted_http_client=http_client_mock2,
|
2020-07-16 07:06:28 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
# We choose a user name that we know should go to pusher1.
|
|
|
|
event_id = self._create_pusher_and_send_msg("user2")
|
|
|
|
|
|
|
|
# Advance time a bit, so the pusher will register something has happened
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
http_client_mock1.post_json_get_json.assert_called_once()
|
|
|
|
http_client_mock2.post_json_get_json.assert_not_called()
|
|
|
|
self.assertEqual(
|
|
|
|
http_client_mock1.post_json_get_json.call_args[0][0],
|
2020-12-04 08:51:56 -07:00
|
|
|
"https://push.example.com/_matrix/push/v1/notify",
|
2020-07-16 07:06:28 -06:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
event_id,
|
|
|
|
http_client_mock1.post_json_get_json.call_args[0][1]["notification"][
|
|
|
|
"event_id"
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
http_client_mock1.post_json_get_json.reset_mock()
|
|
|
|
http_client_mock2.post_json_get_json.reset_mock()
|
|
|
|
|
|
|
|
# Now we choose a user name that we know should go to pusher2.
|
|
|
|
event_id = self._create_pusher_and_send_msg("user4")
|
|
|
|
|
|
|
|
# Advance time a bit, so the pusher will register something has happened
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
http_client_mock1.post_json_get_json.assert_not_called()
|
|
|
|
http_client_mock2.post_json_get_json.assert_called_once()
|
|
|
|
self.assertEqual(
|
|
|
|
http_client_mock2.post_json_get_json.call_args[0][0],
|
2020-12-04 08:51:56 -07:00
|
|
|
"https://push.example.com/_matrix/push/v1/notify",
|
2020-07-16 07:06:28 -06:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
event_id,
|
|
|
|
http_client_mock2.post_json_get_json.call_args[0][1]["notification"][
|
|
|
|
"event_id"
|
|
|
|
],
|
|
|
|
)
|