You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

157 lines
5.7 KiB

  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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 unittest.mock import Mock
  15. from synapse.handlers.typing import RoomMember, TypingWriterHandler
  16. from synapse.replication.tcp.streams import TypingStream
  17. from synapse.util.caches.stream_change_cache import StreamChangeCache
  18. from tests.replication._base import BaseStreamTestCase
  19. USER_ID = "@feeling:blue"
  20. USER_ID_2 = "@da-ba-dee:blue"
  21. ROOM_ID = "!bar:blue"
  22. ROOM_ID_2 = "!foo:blue"
  23. class TypingStreamTestCase(BaseStreamTestCase):
  24. def _build_replication_data_handler(self) -> Mock:
  25. self.mock_handler = Mock(wraps=super()._build_replication_data_handler())
  26. return self.mock_handler
  27. def test_typing(self) -> None:
  28. typing = self.hs.get_typing_handler()
  29. assert isinstance(typing, TypingWriterHandler)
  30. self.reconnect()
  31. typing._push_update(member=RoomMember(ROOM_ID, USER_ID), typing=True)
  32. self.reactor.advance(0)
  33. # We should now see an attempt to connect to the master
  34. request = self.handle_http_replication_attempt()
  35. self.assert_request_is_get_repl_stream_updates(request, "typing")
  36. self.mock_handler.on_rdata.assert_called_once()
  37. stream_name, _, token, rdata_rows = self.mock_handler.on_rdata.call_args[0]
  38. self.assertEqual(stream_name, "typing")
  39. self.assertEqual(1, len(rdata_rows))
  40. row: TypingStream.TypingStreamRow = rdata_rows[0]
  41. self.assertEqual(ROOM_ID, row.room_id)
  42. self.assertEqual([USER_ID], row.user_ids)
  43. # Now let's disconnect and insert some data.
  44. self.disconnect()
  45. self.mock_handler.on_rdata.reset_mock()
  46. typing._push_update(member=RoomMember(ROOM_ID, USER_ID), typing=False)
  47. self.mock_handler.on_rdata.assert_not_called()
  48. self.reconnect()
  49. self.pump(0.1)
  50. # We should now see an attempt to connect to the master
  51. request = self.handle_http_replication_attempt()
  52. self.assert_request_is_get_repl_stream_updates(request, "typing")
  53. # The from token should be the token from the last RDATA we got.
  54. assert request.args is not None
  55. self.assertEqual(int(request.args[b"from_token"][0]), token)
  56. self.mock_handler.on_rdata.assert_called_once()
  57. stream_name, _, token, rdata_rows = self.mock_handler.on_rdata.call_args[0]
  58. self.assertEqual(stream_name, "typing")
  59. self.assertEqual(1, len(rdata_rows))
  60. row = rdata_rows[0]
  61. self.assertEqual(ROOM_ID, row.room_id)
  62. self.assertEqual([], row.user_ids)
  63. def test_reset(self) -> None:
  64. """
  65. Test what happens when a typing stream resets.
  66. This is emulated by jumping the stream ahead, then reconnecting (which
  67. sends the proper position and RDATA).
  68. """
  69. typing = self.hs.get_typing_handler()
  70. assert isinstance(typing, TypingWriterHandler)
  71. self.reconnect()
  72. typing._push_update(member=RoomMember(ROOM_ID, USER_ID), typing=True)
  73. self.reactor.advance(0)
  74. # We should now see an attempt to connect to the master
  75. request = self.handle_http_replication_attempt()
  76. self.assert_request_is_get_repl_stream_updates(request, "typing")
  77. self.mock_handler.on_rdata.assert_called_once()
  78. stream_name, _, token, rdata_rows = self.mock_handler.on_rdata.call_args[0]
  79. self.assertEqual(stream_name, "typing")
  80. self.assertEqual(1, len(rdata_rows))
  81. row: TypingStream.TypingStreamRow = rdata_rows[0]
  82. self.assertEqual(ROOM_ID, row.room_id)
  83. self.assertEqual([USER_ID], row.user_ids)
  84. # Push the stream forward a bunch so it can be reset.
  85. for i in range(100):
  86. typing._push_update(
  87. member=RoomMember(ROOM_ID, "@test%s:blue" % i), typing=True
  88. )
  89. self.reactor.advance(0)
  90. # Disconnect.
  91. self.disconnect()
  92. # Reset the typing handler
  93. self.hs.get_replication_streams()["typing"].last_token = 0
  94. self.hs.get_replication_command_handler()._streams["typing"].last_token = 0
  95. typing._latest_room_serial = 0
  96. typing._typing_stream_change_cache = StreamChangeCache(
  97. "TypingStreamChangeCache", typing._latest_room_serial
  98. )
  99. typing._reset()
  100. # Reconnect.
  101. self.reconnect()
  102. self.pump(0.1)
  103. # We should now see an attempt to connect to the master
  104. request = self.handle_http_replication_attempt()
  105. self.assert_request_is_get_repl_stream_updates(request, "typing")
  106. # Reset the test code.
  107. self.mock_handler.on_rdata.reset_mock()
  108. self.mock_handler.on_rdata.assert_not_called()
  109. # Push additional data.
  110. typing._push_update(member=RoomMember(ROOM_ID_2, USER_ID_2), typing=False)
  111. self.reactor.advance(0)
  112. self.mock_handler.on_rdata.assert_called_once()
  113. stream_name, _, token, rdata_rows = self.mock_handler.on_rdata.call_args[0]
  114. self.assertEqual(stream_name, "typing")
  115. self.assertEqual(1, len(rdata_rows))
  116. row = rdata_rows[0]
  117. self.assertEqual(ROOM_ID_2, row.room_id)
  118. self.assertEqual([], row.user_ids)
  119. # The token should have been reset.
  120. self.assertEqual(token, 1)