test_federation_ack.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. from unittest import mock
  15. from synapse.app.generic_worker import GenericWorkerServer
  16. from synapse.replication.tcp.commands import FederationAckCommand
  17. from synapse.replication.tcp.protocol import IReplicationConnection
  18. from synapse.replication.tcp.streams.federation import FederationStream
  19. from tests.unittest import HomeserverTestCase
  20. class FederationAckTestCase(HomeserverTestCase):
  21. def default_config(self) -> dict:
  22. config = super().default_config()
  23. config["worker_app"] = "synapse.app.generic_worker"
  24. config["worker_name"] = "federation_sender1"
  25. config["federation_sender_instances"] = ["federation_sender1"]
  26. return config
  27. def make_homeserver(self, reactor, clock):
  28. hs = self.setup_test_homeserver(homeserver_to_use=GenericWorkerServer)
  29. return hs
  30. def test_federation_ack_sent(self):
  31. """A FEDERATION_ACK should be sent back after each RDATA federation
  32. This test checks that the federation sender is correctly sending back
  33. FEDERATION_ACK messages. The test works by spinning up a federation_sender
  34. worker server, and then fishing out its ReplicationCommandHandler. We wire
  35. the RCH up to a mock connection (so that we can observe the command being sent)
  36. and then poke in an RDATA row.
  37. XXX: it might be nice to do this by pretending to be a synapse master worker
  38. (or a redis server), and having the worker connect to us via a mocked-up TCP
  39. transport, rather than assuming that the implementation has a
  40. ReplicationCommandHandler.
  41. """
  42. rch = self.hs.get_replication_command_handler()
  43. # wire up the ReplicationCommandHandler to a mock connection, which needs
  44. # to implement IReplicationConnection. (Note that Mock doesn't understand
  45. # interfaces, but casing an interface to a list gives the attributes.)
  46. mock_connection = mock.Mock(spec=list(IReplicationConnection))
  47. rch.new_connection(mock_connection)
  48. # tell it it received an RDATA row
  49. self.get_success(
  50. rch.on_rdata(
  51. "federation",
  52. "master",
  53. token=10,
  54. rows=[
  55. FederationStream.FederationStreamRow(
  56. type="x", data={"test": [1, 2, 3]}
  57. )
  58. ],
  59. )
  60. )
  61. # now check that the FEDERATION_ACK was sent
  62. mock_connection.send_command.assert_called_once()
  63. cmd = mock_connection.send_command.call_args[0][0]
  64. assert isinstance(cmd, FederationAckCommand)
  65. self.assertEqual(cmd.token, 10)