test_federation_ack.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_replication_command_handler()
  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=[
  54. FederationStream.FederationStreamRow(
  55. type="x", data={"test": [1, 2, 3]}
  56. )
  57. ],
  58. )
  59. )
  60. # now check that the FEDERATION_ACK was sent
  61. mock_connection.send_command.assert_called_once()
  62. cmd = mock_connection.send_command.call_args[0][0]
  63. assert isinstance(cmd, FederationAckCommand)
  64. self.assertEqual(cmd.token, 10)