test_federation_sender.py 4.9 KB

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