1
0

test_federation_ack.py 3.0 KB

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