test_federation.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright 2019 New Vector Ltd
  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 synapse.federation.send_queue import EduRow
  15. from synapse.replication.tcp.streams.federation import FederationStream
  16. from tests.replication._base import BaseStreamTestCase
  17. class FederationStreamTestCase(BaseStreamTestCase):
  18. def _get_worker_hs_config(self) -> dict:
  19. # enable federation sending on the worker
  20. config = super()._get_worker_hs_config()
  21. config["worker_name"] = "federation_sender1"
  22. config["federation_sender_instances"] = ["federation_sender1"]
  23. return config
  24. def test_catchup(self) -> None:
  25. """Basic test of catchup on reconnect
  26. Makes sure that updates sent while we are offline are received later.
  27. """
  28. fed_sender = self.hs.get_federation_sender()
  29. received_rows = self.test_handler.received_rdata_rows
  30. fed_sender.build_and_send_edu("testdest", "m.test_edu", {"a": "b"})
  31. self.reconnect()
  32. self.reactor.advance(0)
  33. # check we're testing what we think we are: no rows should yet have been
  34. # received
  35. self.assertEqual(received_rows, [])
  36. # We should now see an attempt to connect to the master
  37. request = self.handle_http_replication_attempt()
  38. self.assert_request_is_get_repl_stream_updates(request, "federation")
  39. # we should have received an update row
  40. stream_name, token, row = received_rows.pop()
  41. self.assertEqual(stream_name, "federation")
  42. self.assertIsInstance(row, FederationStream.FederationStreamRow)
  43. self.assertEqual(row.type, EduRow.TypeId)
  44. edurow = EduRow.from_data(row.data)
  45. self.assertEqual(edurow.edu.edu_type, "m.test_edu")
  46. self.assertEqual(edurow.edu.origin, self.hs.hostname)
  47. self.assertEqual(edurow.edu.destination, "testdest")
  48. self.assertEqual(edurow.edu.content, {"a": "b"})
  49. self.assertEqual(received_rows, [])
  50. # additional updates should be transferred without an HTTP hit
  51. fed_sender.build_and_send_edu("testdest", "m.test1", {"c": "d"})
  52. self.reactor.advance(0)
  53. # there should be no http hit
  54. self.assertEqual(len(self.reactor.tcpClients), 0)
  55. # ... but we should have a row
  56. self.assertEqual(len(received_rows), 1)
  57. stream_name, token, row = received_rows.pop()
  58. self.assertEqual(stream_name, "federation")
  59. self.assertIsInstance(row, FederationStream.FederationStreamRow)
  60. self.assertEqual(row.type, EduRow.TypeId)
  61. edurow = EduRow.from_data(row.data)
  62. self.assertEqual(edurow.edu.edu_type, "m.test1")
  63. self.assertEqual(edurow.edu.origin, self.hs.hostname)
  64. self.assertEqual(edurow.edu.destination, "testdest")
  65. self.assertEqual(edurow.edu.content, {"c": "d"})