1
0

test_federation.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. # TODO: make it so we don't need both of these
  22. config["send_federation"] = False
  23. config["worker_app"] = "synapse.app.federation_sender"
  24. return config
  25. def test_catchup(self):
  26. """Basic test of catchup on reconnect
  27. Makes sure that updates sent while we are offline are received later.
  28. """
  29. fed_sender = self.hs.get_federation_sender()
  30. received_rows = self.test_handler.received_rdata_rows
  31. fed_sender.build_and_send_edu("testdest", "m.test_edu", {"a": "b"})
  32. self.reconnect()
  33. self.reactor.advance(0)
  34. # check we're testing what we think we are: no rows should yet have been
  35. # received
  36. self.assertEqual(received_rows, [])
  37. # We should now see an attempt to connect to the master
  38. request = self.handle_http_replication_attempt()
  39. self.assert_request_is_get_repl_stream_updates(request, "federation")
  40. # we should have received an update row
  41. stream_name, token, row = received_rows.pop()
  42. self.assertEqual(stream_name, "federation")
  43. self.assertIsInstance(row, FederationStream.FederationStreamRow)
  44. self.assertEqual(row.type, EduRow.TypeId)
  45. edurow = EduRow.from_data(row.data)
  46. self.assertEqual(edurow.edu.edu_type, "m.test_edu")
  47. self.assertEqual(edurow.edu.origin, self.hs.hostname)
  48. self.assertEqual(edurow.edu.destination, "testdest")
  49. self.assertEqual(edurow.edu.content, {"a": "b"})
  50. self.assertEqual(received_rows, [])
  51. # additional updates should be transferred without an HTTP hit
  52. fed_sender.build_and_send_edu("testdest", "m.test1", {"c": "d"})
  53. self.reactor.advance(0)
  54. # there should be no http hit
  55. self.assertEqual(len(self.reactor.tcpClients), 0)
  56. # ... but we should have a row
  57. self.assertEqual(len(received_rows), 1)
  58. stream_name, token, row = received_rows.pop()
  59. self.assertEqual(stream_name, "federation")
  60. self.assertIsInstance(row, FederationStream.FederationStreamRow)
  61. self.assertEqual(row.type, EduRow.TypeId)
  62. edurow = EduRow.from_data(row.data)
  63. self.assertEqual(edurow.edu.edu_type, "m.test1")
  64. self.assertEqual(edurow.edu.origin, self.hs.hostname)
  65. self.assertEqual(edurow.edu.destination, "testdest")
  66. self.assertEqual(edurow.edu.content, {"c": "d"})