test_federation.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.federation.send_queue import EduRow
  16. from synapse.replication.tcp.streams.federation import FederationStream
  17. from tests.replication._base import BaseStreamTestCase
  18. class FederationStreamTestCase(BaseStreamTestCase):
  19. def _get_worker_hs_config(self) -> dict:
  20. # enable federation sending on the worker
  21. config = super()._get_worker_hs_config()
  22. # TODO: make it so we don't need both of these
  23. config["send_federation"] = True
  24. config["worker_app"] = "synapse.app.federation_sender"
  25. return config
  26. def test_catchup(self):
  27. """Basic test of catchup on reconnect
  28. Makes sure that updates sent while we are offline are received later.
  29. """
  30. fed_sender = self.hs.get_federation_sender()
  31. received_rows = self.test_handler.received_rdata_rows
  32. fed_sender.build_and_send_edu("testdest", "m.test_edu", {"a": "b"})
  33. self.reconnect()
  34. self.reactor.advance(0)
  35. # check we're testing what we think we are: no rows should yet have been
  36. # received
  37. self.assertEqual(received_rows, [])
  38. # We should now see an attempt to connect to the master
  39. request = self.handle_http_replication_attempt()
  40. self.assert_request_is_get_repl_stream_updates(request, "federation")
  41. # we should have received an update row
  42. stream_name, token, row = received_rows.pop()
  43. self.assertEqual(stream_name, "federation")
  44. self.assertIsInstance(row, FederationStream.FederationStreamRow)
  45. self.assertEqual(row.type, EduRow.TypeId)
  46. edurow = EduRow.from_data(row.data)
  47. self.assertEqual(edurow.edu.edu_type, "m.test_edu")
  48. self.assertEqual(edurow.edu.origin, self.hs.hostname)
  49. self.assertEqual(edurow.edu.destination, "testdest")
  50. self.assertEqual(edurow.edu.content, {"a": "b"})
  51. self.assertEqual(received_rows, [])
  52. # additional updates should be transferred without an HTTP hit
  53. fed_sender.build_and_send_edu("testdest", "m.test1", {"c": "d"})
  54. self.reactor.advance(0)
  55. # there should be no http hit
  56. self.assertEqual(len(self.reactor.tcpClients), 0)
  57. # ... but we should have a row
  58. self.assertEqual(len(received_rows), 1)
  59. stream_name, token, row = received_rows.pop()
  60. self.assertEqual(stream_name, "federation")
  61. self.assertIsInstance(row, FederationStream.FederationStreamRow)
  62. self.assertEqual(row.type, EduRow.TypeId)
  63. edurow = EduRow.from_data(row.data)
  64. self.assertEqual(edurow.edu.edu_type, "m.test1")
  65. self.assertEqual(edurow.edu.origin, self.hs.hostname)
  66. self.assertEqual(edurow.edu.destination, "testdest")
  67. self.assertEqual(edurow.edu.content, {"c": "d"})