test_federation_sender.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 mock import Mock
  16. from twisted.internet import defer
  17. from synapse.types import ReadReceipt
  18. from tests.unittest import HomeserverTestCase, override_config
  19. class FederationSenderTestCases(HomeserverTestCase):
  20. def make_homeserver(self, reactor, clock):
  21. return super(FederationSenderTestCases, self).setup_test_homeserver(
  22. state_handler=Mock(spec=["get_current_hosts_in_room"]),
  23. federation_transport_client=Mock(spec=["send_transaction"]),
  24. )
  25. @override_config({"send_federation": True})
  26. def test_send_receipts(self):
  27. mock_state_handler = self.hs.get_state_handler()
  28. mock_state_handler.get_current_hosts_in_room.return_value = ["test", "host2"]
  29. mock_send_transaction = (
  30. self.hs.get_federation_transport_client().send_transaction
  31. )
  32. mock_send_transaction.return_value = defer.succeed({})
  33. sender = self.hs.get_federation_sender()
  34. receipt = ReadReceipt(
  35. "room_id", "m.read", "user_id", ["event_id"], {"ts": 1234}
  36. )
  37. self.successResultOf(sender.send_read_receipt(receipt))
  38. self.pump()
  39. # expect a call to send_transaction
  40. mock_send_transaction.assert_called_once()
  41. json_cb = mock_send_transaction.call_args[0][1]
  42. data = json_cb()
  43. self.assertEqual(
  44. data["edus"],
  45. [
  46. {
  47. "edu_type": "m.receipt",
  48. "content": {
  49. "room_id": {
  50. "m.read": {
  51. "user_id": {
  52. "event_ids": ["event_id"],
  53. "data": {"ts": 1234},
  54. }
  55. }
  56. }
  57. },
  58. }
  59. ],
  60. )
  61. @override_config({"send_federation": True})
  62. def test_send_receipts_with_backoff(self):
  63. """Send two receipts in quick succession; the second should be flushed, but
  64. only after 20ms"""
  65. mock_state_handler = self.hs.get_state_handler()
  66. mock_state_handler.get_current_hosts_in_room.return_value = ["test", "host2"]
  67. mock_send_transaction = (
  68. self.hs.get_federation_transport_client().send_transaction
  69. )
  70. mock_send_transaction.return_value = defer.succeed({})
  71. sender = self.hs.get_federation_sender()
  72. receipt = ReadReceipt(
  73. "room_id", "m.read", "user_id", ["event_id"], {"ts": 1234}
  74. )
  75. self.successResultOf(sender.send_read_receipt(receipt))
  76. self.pump()
  77. # expect a call to send_transaction
  78. mock_send_transaction.assert_called_once()
  79. json_cb = mock_send_transaction.call_args[0][1]
  80. data = json_cb()
  81. self.assertEqual(
  82. data["edus"],
  83. [
  84. {
  85. "edu_type": "m.receipt",
  86. "content": {
  87. "room_id": {
  88. "m.read": {
  89. "user_id": {
  90. "event_ids": ["event_id"],
  91. "data": {"ts": 1234},
  92. }
  93. }
  94. }
  95. },
  96. }
  97. ],
  98. )
  99. mock_send_transaction.reset_mock()
  100. # send the second RR
  101. receipt = ReadReceipt(
  102. "room_id", "m.read", "user_id", ["other_id"], {"ts": 1234}
  103. )
  104. self.successResultOf(sender.send_read_receipt(receipt))
  105. self.pump()
  106. mock_send_transaction.assert_not_called()
  107. self.reactor.advance(19)
  108. mock_send_transaction.assert_not_called()
  109. self.reactor.advance(10)
  110. mock_send_transaction.assert_called_once()
  111. json_cb = mock_send_transaction.call_args[0][1]
  112. data = json_cb()
  113. self.assertEqual(
  114. data["edus"],
  115. [
  116. {
  117. "edu_type": "m.receipt",
  118. "content": {
  119. "room_id": {
  120. "m.read": {
  121. "user_id": {
  122. "event_ids": ["other_id"],
  123. "data": {"ts": 1234},
  124. }
  125. }
  126. }
  127. },
  128. }
  129. ],
  130. )