1
0

test_federation_ack.py 3.3 KB

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