2020-05-26 13:41:38 +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-05-26 13:41:38 +03:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2021-04-09 20:44:38 +03:00
|
|
|
from unittest import mock
|
2020-05-26 13:41:38 +03:00
|
|
|
|
2023-02-06 17:55:00 +03:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
|
2020-05-26 13:41:38 +03:00
|
|
|
from synapse.app.generic_worker import GenericWorkerServer
|
|
|
|
from synapse.replication.tcp.commands import FederationAckCommand
|
2021-03-12 19:37:57 +03:00
|
|
|
from synapse.replication.tcp.protocol import IReplicationConnection
|
2020-05-26 13:41:38 +03:00
|
|
|
from synapse.replication.tcp.streams.federation import FederationStream
|
2023-02-06 17:55:00 +03:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.util import Clock
|
2020-05-26 13:41:38 +03:00
|
|
|
|
|
|
|
from tests.unittest import HomeserverTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class FederationAckTestCase(HomeserverTestCase):
|
|
|
|
def default_config(self) -> dict:
|
|
|
|
config = super().default_config()
|
2022-12-01 15:38:27 +03:00
|
|
|
config["worker_app"] = "synapse.app.generic_worker"
|
|
|
|
config["worker_name"] = "federation_sender1"
|
|
|
|
config["federation_sender_instances"] = ["federation_sender1"]
|
2023-05-26 17:28:55 +03:00
|
|
|
config["instance_map"] = {"main": {"host": "127.0.0.1", "port": 0}}
|
2020-05-26 13:41:38 +03:00
|
|
|
return config
|
|
|
|
|
2023-02-06 17:55:00 +03:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
|
|
|
return self.setup_test_homeserver(homeserver_to_use=GenericWorkerServer)
|
2020-05-26 13:41:38 +03:00
|
|
|
|
2023-02-06 17:55:00 +03:00
|
|
|
def test_federation_ack_sent(self) -> None:
|
2020-05-26 13:41:38 +03:00
|
|
|
"""A FEDERATION_ACK should be sent back after each RDATA federation
|
|
|
|
|
|
|
|
This test checks that the federation sender is correctly sending back
|
|
|
|
FEDERATION_ACK messages. The test works by spinning up a federation_sender
|
|
|
|
worker server, and then fishing out its ReplicationCommandHandler. We wire
|
|
|
|
the RCH up to a mock connection (so that we can observe the command being sent)
|
|
|
|
and then poke in an RDATA row.
|
|
|
|
|
|
|
|
XXX: it might be nice to do this by pretending to be a synapse master worker
|
|
|
|
(or a redis server), and having the worker connect to us via a mocked-up TCP
|
|
|
|
transport, rather than assuming that the implementation has a
|
|
|
|
ReplicationCommandHandler.
|
|
|
|
"""
|
2022-03-10 16:01:56 +03:00
|
|
|
rch = self.hs.get_replication_command_handler()
|
2020-05-26 13:41:38 +03:00
|
|
|
|
2021-03-12 19:37:57 +03:00
|
|
|
# wire up the ReplicationCommandHandler to a mock connection, which needs
|
|
|
|
# to implement IReplicationConnection. (Note that Mock doesn't understand
|
|
|
|
# interfaces, but casing an interface to a list gives the attributes.)
|
|
|
|
mock_connection = mock.Mock(spec=list(IReplicationConnection))
|
2020-05-26 13:41:38 +03:00
|
|
|
rch.new_connection(mock_connection)
|
|
|
|
|
|
|
|
# tell it it received an RDATA row
|
|
|
|
self.get_success(
|
|
|
|
rch.on_rdata(
|
|
|
|
"federation",
|
|
|
|
"master",
|
|
|
|
token=10,
|
2021-12-30 21:47:12 +03:00
|
|
|
rows=[
|
|
|
|
FederationStream.FederationStreamRow(
|
|
|
|
type="x", data={"test": [1, 2, 3]}
|
|
|
|
)
|
|
|
|
],
|
2020-05-26 13:41:38 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# now check that the FEDERATION_ACK was sent
|
|
|
|
mock_connection.send_command.assert_called_once()
|
|
|
|
cmd = mock_connection.send_command.call_args[0][0]
|
|
|
|
assert isinstance(cmd, FederationAckCommand)
|
|
|
|
self.assertEqual(cmd.token, 10)
|