2020-07-10 20:26:36 +03:00
|
|
|
#
|
2023-11-21 23:29:58 +03:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 14:26:48 +03:00
|
|
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
2023-11-21 23:29:58 +03:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2020-07-10 20:26:36 +03:00
|
|
|
#
|
|
|
|
#
|
|
|
|
import logging
|
2023-08-25 02:38:46 +03:00
|
|
|
from unittest.mock import AsyncMock, Mock
|
2020-07-10 20:26:36 +03:00
|
|
|
|
2023-07-18 11:49:21 +03:00
|
|
|
from netaddr import IPSet
|
|
|
|
|
2020-07-10 20:26:36 +03:00
|
|
|
from synapse.api.constants import EventTypes, Membership
|
|
|
|
from synapse.events.builder import EventBuilderFactory
|
2023-02-14 22:03:35 +03:00
|
|
|
from synapse.handlers.typing import TypingWriterHandler
|
2023-07-18 11:49:21 +03:00
|
|
|
from synapse.http.federation.matrix_federation_agent import MatrixFederationAgent
|
2020-07-10 20:26:36 +03:00
|
|
|
from synapse.rest.admin import register_servlets_for_client_rest_resource
|
2021-08-17 14:57:58 +03:00
|
|
|
from synapse.rest.client import login, room
|
2020-08-26 19:05:36 +03:00
|
|
|
from synapse.types import UserID, create_requester
|
2020-07-10 20:26:36 +03:00
|
|
|
|
2020-07-15 17:27:35 +03:00
|
|
|
from tests.replication._base import BaseMultiWorkerStreamTestCase
|
2023-07-18 11:49:21 +03:00
|
|
|
from tests.server import get_clock
|
2020-07-10 20:26:36 +03:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-07-15 17:27:35 +03:00
|
|
|
class FederationSenderTestCase(BaseMultiWorkerStreamTestCase):
|
2022-12-01 15:38:27 +03:00
|
|
|
"""
|
|
|
|
Various tests for federation sending on workers.
|
|
|
|
|
|
|
|
Federation sending is disabled by default, it will be enabled in each test by
|
|
|
|
updating 'federation_sender_instances'.
|
|
|
|
"""
|
|
|
|
|
2020-07-10 20:26:36 +03:00
|
|
|
servlets = [
|
2020-07-15 17:27:35 +03:00
|
|
|
login.register_servlets,
|
|
|
|
register_servlets_for_client_rest_resource,
|
|
|
|
room.register_servlets,
|
2020-07-10 20:26:36 +03:00
|
|
|
]
|
|
|
|
|
2023-07-18 11:49:21 +03:00
|
|
|
def setUp(self) -> None:
|
|
|
|
super().setUp()
|
|
|
|
|
|
|
|
reactor, _ = get_clock()
|
|
|
|
self.matrix_federation_agent = MatrixFederationAgent(
|
|
|
|
reactor,
|
|
|
|
tls_client_options_factory=None,
|
|
|
|
user_agent=b"SynapseInTrialTest/0.0.0",
|
|
|
|
ip_allowlist=None,
|
|
|
|
ip_blocklist=IPSet(),
|
|
|
|
)
|
|
|
|
|
2023-02-06 17:55:00 +03:00
|
|
|
def test_send_event_single_sender(self) -> None:
|
2020-07-10 20:26:36 +03:00
|
|
|
"""Test that using a single federation sender worker correctly sends a
|
|
|
|
new event.
|
|
|
|
"""
|
2020-07-15 17:27:35 +03:00
|
|
|
mock_client = Mock(spec=["put_json"])
|
2023-08-25 02:38:46 +03:00
|
|
|
mock_client.put_json = AsyncMock(return_value={})
|
2023-07-18 11:49:21 +03:00
|
|
|
mock_client.agent = self.matrix_federation_agent
|
2020-07-15 17:27:35 +03:00
|
|
|
self.make_worker_hs(
|
2022-12-01 15:38:27 +03:00
|
|
|
"synapse.app.generic_worker",
|
|
|
|
{
|
|
|
|
"worker_name": "federation_sender1",
|
|
|
|
"federation_sender_instances": ["federation_sender1"],
|
|
|
|
},
|
2020-12-02 19:09:24 +03:00
|
|
|
federation_http_client=mock_client,
|
2020-07-15 17:27:35 +03:00
|
|
|
)
|
2020-07-10 20:26:36 +03:00
|
|
|
|
|
|
|
user = self.register_user("user", "pass")
|
|
|
|
token = self.login("user", "pass")
|
|
|
|
|
|
|
|
room = self.create_room_with_remote_server(user, token)
|
|
|
|
|
|
|
|
mock_client.put_json.reset_mock()
|
|
|
|
|
|
|
|
self.create_and_send_event(room, UserID.from_string(user))
|
|
|
|
self.replicate()
|
|
|
|
|
|
|
|
# Assert that the event was sent out over federation.
|
|
|
|
mock_client.put_json.assert_called()
|
|
|
|
self.assertEqual(mock_client.put_json.call_args[0][0], "other_server")
|
|
|
|
self.assertTrue(mock_client.put_json.call_args[1]["data"].get("pdus"))
|
|
|
|
|
2023-02-06 17:55:00 +03:00
|
|
|
def test_send_event_sharded(self) -> None:
|
2020-07-10 20:26:36 +03:00
|
|
|
"""Test that using two federation sender workers correctly sends
|
|
|
|
new events.
|
|
|
|
"""
|
2020-07-15 17:27:35 +03:00
|
|
|
mock_client1 = Mock(spec=["put_json"])
|
2023-08-25 02:38:46 +03:00
|
|
|
mock_client1.put_json = AsyncMock(return_value={})
|
2023-07-18 11:49:21 +03:00
|
|
|
mock_client1.agent = self.matrix_federation_agent
|
2020-07-15 17:27:35 +03:00
|
|
|
self.make_worker_hs(
|
2022-12-01 15:38:27 +03:00
|
|
|
"synapse.app.generic_worker",
|
2020-07-10 20:26:36 +03:00
|
|
|
{
|
2022-12-01 15:38:27 +03:00
|
|
|
"worker_name": "federation_sender1",
|
|
|
|
"federation_sender_instances": [
|
|
|
|
"federation_sender1",
|
|
|
|
"federation_sender2",
|
|
|
|
],
|
2020-07-15 17:27:35 +03:00
|
|
|
},
|
2020-12-02 19:09:24 +03:00
|
|
|
federation_http_client=mock_client1,
|
2020-07-10 20:26:36 +03:00
|
|
|
)
|
|
|
|
|
2020-07-15 17:27:35 +03:00
|
|
|
mock_client2 = Mock(spec=["put_json"])
|
2023-08-25 02:38:46 +03:00
|
|
|
mock_client2.put_json = AsyncMock(return_value={})
|
2023-07-18 11:49:21 +03:00
|
|
|
mock_client2.agent = self.matrix_federation_agent
|
2020-07-15 17:27:35 +03:00
|
|
|
self.make_worker_hs(
|
2022-12-01 15:38:27 +03:00
|
|
|
"synapse.app.generic_worker",
|
2020-07-10 20:26:36 +03:00
|
|
|
{
|
2022-12-01 15:38:27 +03:00
|
|
|
"worker_name": "federation_sender2",
|
|
|
|
"federation_sender_instances": [
|
|
|
|
"federation_sender1",
|
|
|
|
"federation_sender2",
|
|
|
|
],
|
2020-07-15 17:27:35 +03:00
|
|
|
},
|
2020-12-02 19:09:24 +03:00
|
|
|
federation_http_client=mock_client2,
|
2020-07-10 20:26:36 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
user = self.register_user("user2", "pass")
|
|
|
|
token = self.login("user2", "pass")
|
|
|
|
|
|
|
|
sent_on_1 = False
|
|
|
|
sent_on_2 = False
|
|
|
|
for i in range(20):
|
|
|
|
server_name = "other_server_%d" % (i,)
|
|
|
|
room = self.create_room_with_remote_server(user, token, server_name)
|
2022-04-27 16:03:44 +03:00
|
|
|
mock_client1.reset_mock()
|
|
|
|
mock_client2.reset_mock()
|
2020-07-10 20:26:36 +03:00
|
|
|
|
|
|
|
self.create_and_send_event(room, UserID.from_string(user))
|
|
|
|
self.replicate()
|
|
|
|
|
|
|
|
if mock_client1.put_json.called:
|
|
|
|
sent_on_1 = True
|
|
|
|
mock_client2.put_json.assert_not_called()
|
|
|
|
self.assertEqual(mock_client1.put_json.call_args[0][0], server_name)
|
|
|
|
self.assertTrue(mock_client1.put_json.call_args[1]["data"].get("pdus"))
|
|
|
|
elif mock_client2.put_json.called:
|
|
|
|
sent_on_2 = True
|
|
|
|
mock_client1.put_json.assert_not_called()
|
|
|
|
self.assertEqual(mock_client2.put_json.call_args[0][0], server_name)
|
|
|
|
self.assertTrue(mock_client2.put_json.call_args[1]["data"].get("pdus"))
|
|
|
|
else:
|
|
|
|
raise AssertionError(
|
|
|
|
"Expected send transaction from one or the other sender"
|
|
|
|
)
|
|
|
|
|
|
|
|
if sent_on_1 and sent_on_2:
|
|
|
|
break
|
|
|
|
|
|
|
|
self.assertTrue(sent_on_1)
|
|
|
|
self.assertTrue(sent_on_2)
|
|
|
|
|
2023-02-06 17:55:00 +03:00
|
|
|
def test_send_typing_sharded(self) -> None:
|
2020-07-10 20:26:36 +03:00
|
|
|
"""Test that using two federation sender workers correctly sends
|
|
|
|
new typing EDUs.
|
|
|
|
"""
|
2020-07-15 17:27:35 +03:00
|
|
|
mock_client1 = Mock(spec=["put_json"])
|
2023-08-25 02:38:46 +03:00
|
|
|
mock_client1.put_json = AsyncMock(return_value={})
|
2023-07-18 11:49:21 +03:00
|
|
|
mock_client1.agent = self.matrix_federation_agent
|
2020-07-15 17:27:35 +03:00
|
|
|
self.make_worker_hs(
|
2022-12-01 15:38:27 +03:00
|
|
|
"synapse.app.generic_worker",
|
2020-07-10 20:26:36 +03:00
|
|
|
{
|
2022-12-01 15:38:27 +03:00
|
|
|
"worker_name": "federation_sender1",
|
|
|
|
"federation_sender_instances": [
|
|
|
|
"federation_sender1",
|
|
|
|
"federation_sender2",
|
|
|
|
],
|
2020-07-15 17:27:35 +03:00
|
|
|
},
|
2020-12-02 19:09:24 +03:00
|
|
|
federation_http_client=mock_client1,
|
2020-07-10 20:26:36 +03:00
|
|
|
)
|
|
|
|
|
2020-07-15 17:27:35 +03:00
|
|
|
mock_client2 = Mock(spec=["put_json"])
|
2023-08-25 02:38:46 +03:00
|
|
|
mock_client2.put_json = AsyncMock(return_value={})
|
2023-07-18 11:49:21 +03:00
|
|
|
mock_client2.agent = self.matrix_federation_agent
|
2020-07-15 17:27:35 +03:00
|
|
|
self.make_worker_hs(
|
2022-12-01 15:38:27 +03:00
|
|
|
"synapse.app.generic_worker",
|
2020-07-10 20:26:36 +03:00
|
|
|
{
|
2022-12-01 15:38:27 +03:00
|
|
|
"worker_name": "federation_sender2",
|
|
|
|
"federation_sender_instances": [
|
|
|
|
"federation_sender1",
|
|
|
|
"federation_sender2",
|
|
|
|
],
|
2020-07-15 17:27:35 +03:00
|
|
|
},
|
2020-12-02 19:09:24 +03:00
|
|
|
federation_http_client=mock_client2,
|
2020-07-10 20:26:36 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
user = self.register_user("user3", "pass")
|
|
|
|
token = self.login("user3", "pass")
|
|
|
|
|
|
|
|
typing_handler = self.hs.get_typing_handler()
|
2023-02-14 22:03:35 +03:00
|
|
|
assert isinstance(typing_handler, TypingWriterHandler)
|
2020-07-10 20:26:36 +03:00
|
|
|
|
|
|
|
sent_on_1 = False
|
|
|
|
sent_on_2 = False
|
|
|
|
for i in range(20):
|
|
|
|
server_name = "other_server_%d" % (i,)
|
|
|
|
room = self.create_room_with_remote_server(user, token, server_name)
|
2022-04-27 16:03:44 +03:00
|
|
|
mock_client1.reset_mock()
|
|
|
|
mock_client2.reset_mock()
|
2020-07-10 20:26:36 +03:00
|
|
|
|
|
|
|
self.get_success(
|
|
|
|
typing_handler.started_typing(
|
|
|
|
target_user=UserID.from_string(user),
|
2020-08-26 19:05:36 +03:00
|
|
|
requester=create_requester(user),
|
2020-07-10 20:26:36 +03:00
|
|
|
room_id=room,
|
|
|
|
timeout=20000,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.replicate()
|
|
|
|
|
|
|
|
if mock_client1.put_json.called:
|
|
|
|
sent_on_1 = True
|
|
|
|
mock_client2.put_json.assert_not_called()
|
|
|
|
self.assertEqual(mock_client1.put_json.call_args[0][0], server_name)
|
|
|
|
self.assertTrue(mock_client1.put_json.call_args[1]["data"].get("edus"))
|
|
|
|
elif mock_client2.put_json.called:
|
|
|
|
sent_on_2 = True
|
|
|
|
mock_client1.put_json.assert_not_called()
|
|
|
|
self.assertEqual(mock_client2.put_json.call_args[0][0], server_name)
|
|
|
|
self.assertTrue(mock_client2.put_json.call_args[1]["data"].get("edus"))
|
|
|
|
else:
|
|
|
|
raise AssertionError(
|
|
|
|
"Expected send transaction from one or the other sender"
|
|
|
|
)
|
|
|
|
|
|
|
|
if sent_on_1 and sent_on_2:
|
|
|
|
break
|
|
|
|
|
|
|
|
self.assertTrue(sent_on_1)
|
|
|
|
self.assertTrue(sent_on_2)
|
2020-07-15 17:27:35 +03:00
|
|
|
|
2023-02-06 17:55:00 +03:00
|
|
|
def create_room_with_remote_server(
|
|
|
|
self, user: str, token: str, remote_server: str = "other_server"
|
|
|
|
) -> str:
|
2020-07-15 17:27:35 +03:00
|
|
|
room = self.helper.create_room_as(user, tok=token)
|
2022-02-23 14:04:02 +03:00
|
|
|
store = self.hs.get_datastores().main
|
2021-08-26 23:41:44 +03:00
|
|
|
federation = self.hs.get_federation_event_handler()
|
2020-07-15 17:27:35 +03:00
|
|
|
|
|
|
|
prev_event_ids = self.get_success(store.get_latest_event_ids_in_room(room))
|
|
|
|
room_version = self.get_success(store.get_room_version(room))
|
|
|
|
|
|
|
|
factory = EventBuilderFactory(self.hs)
|
|
|
|
factory.hostname = remote_server
|
|
|
|
|
|
|
|
user_id = UserID("user", remote_server).to_string()
|
|
|
|
|
|
|
|
event_dict = {
|
|
|
|
"type": EventTypes.Member,
|
|
|
|
"state_key": user_id,
|
|
|
|
"content": {"membership": Membership.JOIN},
|
|
|
|
"sender": user_id,
|
|
|
|
"room_id": room,
|
|
|
|
}
|
|
|
|
|
|
|
|
builder = factory.for_room_version(room_version, event_dict)
|
2021-06-22 12:02:53 +03:00
|
|
|
join_event = self.get_success(
|
2023-09-18 16:29:05 +03:00
|
|
|
builder.build(prev_event_ids=list(prev_event_ids), auth_event_ids=None)
|
2021-06-22 12:02:53 +03:00
|
|
|
)
|
2020-07-15 17:27:35 +03:00
|
|
|
|
2021-06-24 17:30:49 +03:00
|
|
|
self.get_success(federation.on_send_membership_event(remote_server, join_event))
|
2020-07-15 17:27:35 +03:00
|
|
|
self.replicate()
|
|
|
|
|
|
|
|
return room
|