2020-04-28 18:42:03 +02:00
|
|
|
#
|
2023-11-21 21:29:58 +01:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2020-04-28 18:42:03 +02:00
|
|
|
#
|
|
|
|
#
|
2021-04-09 19:44:38 +02:00
|
|
|
from unittest.mock import Mock
|
2020-04-29 13:30:36 +02:00
|
|
|
|
2023-02-14 20:03:35 +01:00
|
|
|
from synapse.handlers.typing import RoomMember, TypingWriterHandler
|
2020-04-28 18:42:03 +02:00
|
|
|
from synapse.replication.tcp.streams import TypingStream
|
2020-06-15 14:44:54 +02:00
|
|
|
from synapse.util.caches.stream_change_cache import StreamChangeCache
|
2020-04-28 18:42:03 +02:00
|
|
|
|
2020-05-13 17:01:47 +02:00
|
|
|
from tests.replication._base import BaseStreamTestCase
|
2020-04-28 18:42:03 +02:00
|
|
|
|
|
|
|
USER_ID = "@feeling:blue"
|
2020-06-15 14:44:54 +02:00
|
|
|
USER_ID_2 = "@da-ba-dee:blue"
|
|
|
|
|
|
|
|
ROOM_ID = "!bar:blue"
|
|
|
|
ROOM_ID_2 = "!foo:blue"
|
2020-04-28 18:42:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TypingStreamTestCase(BaseStreamTestCase):
|
2023-02-06 15:55:00 +01:00
|
|
|
def _build_replication_data_handler(self) -> Mock:
|
|
|
|
self.mock_handler = Mock(wraps=super()._build_replication_data_handler())
|
|
|
|
return self.mock_handler
|
2020-04-29 13:30:36 +02:00
|
|
|
|
2023-02-06 15:55:00 +01:00
|
|
|
def test_typing(self) -> None:
|
2020-04-28 18:42:03 +02:00
|
|
|
typing = self.hs.get_typing_handler()
|
2023-02-14 20:03:35 +01:00
|
|
|
assert isinstance(typing, TypingWriterHandler)
|
2020-04-28 18:42:03 +02:00
|
|
|
|
2023-11-16 14:05:09 +01:00
|
|
|
# Create a typing update before we reconnect so that there is a missing
|
|
|
|
# update to fetch.
|
|
|
|
typing._push_update(member=RoomMember(ROOM_ID, USER_ID), typing=True)
|
|
|
|
|
2020-04-28 18:42:03 +02:00
|
|
|
self.reconnect()
|
|
|
|
|
2020-06-15 14:44:54 +02:00
|
|
|
typing._push_update(member=RoomMember(ROOM_ID, USER_ID), typing=True)
|
2020-04-28 18:42:03 +02:00
|
|
|
|
|
|
|
self.reactor.advance(0)
|
|
|
|
|
|
|
|
# We should now see an attempt to connect to the master
|
|
|
|
request = self.handle_http_replication_attempt()
|
|
|
|
self.assert_request_is_get_repl_stream_updates(request, "typing")
|
|
|
|
|
2023-02-06 15:55:00 +01:00
|
|
|
self.mock_handler.on_rdata.assert_called_once()
|
|
|
|
stream_name, _, token, rdata_rows = self.mock_handler.on_rdata.call_args[0]
|
2020-04-28 18:42:03 +02:00
|
|
|
self.assertEqual(stream_name, "typing")
|
|
|
|
self.assertEqual(1, len(rdata_rows))
|
2021-07-13 12:52:58 +02:00
|
|
|
row: TypingStream.TypingStreamRow = rdata_rows[0]
|
2020-06-15 14:44:54 +02:00
|
|
|
self.assertEqual(ROOM_ID, row.room_id)
|
2020-04-28 18:42:03 +02:00
|
|
|
self.assertEqual([USER_ID], row.user_ids)
|
|
|
|
|
|
|
|
# Now let's disconnect and insert some data.
|
|
|
|
self.disconnect()
|
|
|
|
|
2023-02-06 15:55:00 +01:00
|
|
|
self.mock_handler.on_rdata.reset_mock()
|
2020-04-28 18:42:03 +02:00
|
|
|
|
2020-06-15 14:44:54 +02:00
|
|
|
typing._push_update(member=RoomMember(ROOM_ID, USER_ID), typing=False)
|
2020-04-28 18:42:03 +02:00
|
|
|
|
2023-02-06 15:55:00 +01:00
|
|
|
self.mock_handler.on_rdata.assert_not_called()
|
2020-04-28 18:42:03 +02:00
|
|
|
|
|
|
|
self.reconnect()
|
|
|
|
self.pump(0.1)
|
|
|
|
|
|
|
|
# We should now see an attempt to connect to the master
|
|
|
|
request = self.handle_http_replication_attempt()
|
|
|
|
self.assert_request_is_get_repl_stream_updates(request, "typing")
|
|
|
|
|
|
|
|
# The from token should be the token from the last RDATA we got.
|
2021-03-26 17:49:46 +01:00
|
|
|
assert request.args is not None
|
2020-04-28 18:42:03 +02:00
|
|
|
self.assertEqual(int(request.args[b"from_token"][0]), token)
|
|
|
|
|
2023-02-06 15:55:00 +01:00
|
|
|
self.mock_handler.on_rdata.assert_called_once()
|
|
|
|
stream_name, _, token, rdata_rows = self.mock_handler.on_rdata.call_args[0]
|
2020-04-28 18:42:03 +02:00
|
|
|
self.assertEqual(stream_name, "typing")
|
|
|
|
self.assertEqual(1, len(rdata_rows))
|
2020-04-29 13:30:36 +02:00
|
|
|
row = rdata_rows[0]
|
2020-06-15 14:44:54 +02:00
|
|
|
self.assertEqual(ROOM_ID, row.room_id)
|
|
|
|
self.assertEqual([], row.user_ids)
|
|
|
|
|
2023-02-06 15:55:00 +01:00
|
|
|
def test_reset(self) -> None:
|
2020-06-15 14:44:54 +02:00
|
|
|
"""
|
|
|
|
Test what happens when a typing stream resets.
|
|
|
|
|
|
|
|
This is emulated by jumping the stream ahead, then reconnecting (which
|
|
|
|
sends the proper position and RDATA).
|
|
|
|
"""
|
|
|
|
typing = self.hs.get_typing_handler()
|
2023-02-14 20:03:35 +01:00
|
|
|
assert isinstance(typing, TypingWriterHandler)
|
2020-06-15 14:44:54 +02:00
|
|
|
|
2023-11-16 14:05:09 +01:00
|
|
|
# Create a typing update before we reconnect so that there is a missing
|
|
|
|
# update to fetch.
|
|
|
|
typing._push_update(member=RoomMember(ROOM_ID, USER_ID), typing=True)
|
|
|
|
|
2020-06-15 14:44:54 +02:00
|
|
|
self.reconnect()
|
|
|
|
|
|
|
|
typing._push_update(member=RoomMember(ROOM_ID, USER_ID), typing=True)
|
|
|
|
|
|
|
|
self.reactor.advance(0)
|
|
|
|
|
|
|
|
# We should now see an attempt to connect to the master
|
|
|
|
request = self.handle_http_replication_attempt()
|
|
|
|
self.assert_request_is_get_repl_stream_updates(request, "typing")
|
|
|
|
|
2023-02-06 15:55:00 +01:00
|
|
|
self.mock_handler.on_rdata.assert_called_once()
|
|
|
|
stream_name, _, token, rdata_rows = self.mock_handler.on_rdata.call_args[0]
|
2020-06-15 14:44:54 +02:00
|
|
|
self.assertEqual(stream_name, "typing")
|
|
|
|
self.assertEqual(1, len(rdata_rows))
|
2021-07-13 12:52:58 +02:00
|
|
|
row: TypingStream.TypingStreamRow = rdata_rows[0]
|
2020-06-15 14:44:54 +02:00
|
|
|
self.assertEqual(ROOM_ID, row.room_id)
|
|
|
|
self.assertEqual([USER_ID], row.user_ids)
|
|
|
|
|
|
|
|
# Push the stream forward a bunch so it can be reset.
|
|
|
|
for i in range(100):
|
|
|
|
typing._push_update(
|
|
|
|
member=RoomMember(ROOM_ID, "@test%s:blue" % i), typing=True
|
|
|
|
)
|
|
|
|
self.reactor.advance(0)
|
|
|
|
|
|
|
|
# Disconnect.
|
|
|
|
self.disconnect()
|
|
|
|
|
|
|
|
# Reset the typing handler
|
|
|
|
self.hs.get_replication_streams()["typing"].last_token = 0
|
2022-03-10 14:01:56 +01:00
|
|
|
self.hs.get_replication_command_handler()._streams["typing"].last_token = 0
|
2020-06-15 14:44:54 +02:00
|
|
|
typing._latest_room_serial = 0
|
|
|
|
typing._typing_stream_change_cache = StreamChangeCache(
|
|
|
|
"TypingStreamChangeCache", typing._latest_room_serial
|
|
|
|
)
|
|
|
|
typing._reset()
|
|
|
|
|
|
|
|
# Reconnect.
|
|
|
|
self.reconnect()
|
|
|
|
self.pump(0.1)
|
|
|
|
|
|
|
|
# We should now see an attempt to connect to the master
|
|
|
|
request = self.handle_http_replication_attempt()
|
|
|
|
self.assert_request_is_get_repl_stream_updates(request, "typing")
|
|
|
|
|
|
|
|
# Reset the test code.
|
2023-02-06 15:55:00 +01:00
|
|
|
self.mock_handler.on_rdata.reset_mock()
|
|
|
|
self.mock_handler.on_rdata.assert_not_called()
|
2020-06-15 14:44:54 +02:00
|
|
|
|
|
|
|
# Push additional data.
|
|
|
|
typing._push_update(member=RoomMember(ROOM_ID_2, USER_ID_2), typing=False)
|
|
|
|
self.reactor.advance(0)
|
|
|
|
|
2023-02-06 15:55:00 +01:00
|
|
|
self.mock_handler.on_rdata.assert_called_once()
|
|
|
|
stream_name, _, token, rdata_rows = self.mock_handler.on_rdata.call_args[0]
|
2020-06-15 14:44:54 +02:00
|
|
|
self.assertEqual(stream_name, "typing")
|
|
|
|
self.assertEqual(1, len(rdata_rows))
|
|
|
|
row = rdata_rows[0]
|
|
|
|
self.assertEqual(ROOM_ID_2, row.room_id)
|
2020-04-28 18:42:03 +02:00
|
|
|
self.assertEqual([], row.user_ids)
|
2020-06-15 14:44:54 +02:00
|
|
|
|
|
|
|
# The token should have been reset.
|
|
|
|
self.assertEqual(token, 1)
|