|
| 1 | +# Copyright 2023 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 | +import logging |
| 15 | + |
| 16 | +import synapse |
| 17 | +from synapse.replication.tcp.streams._base import _STREAM_UPDATE_TARGET_ROW_COUNT |
| 18 | +from synapse.types import JsonDict |
| 19 | + |
| 20 | +from tests.replication._base import BaseStreamTestCase |
| 21 | + |
| 22 | +logger = logging.getLogger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +class ToDeviceStreamTestCase(BaseStreamTestCase): |
| 26 | + servlets = [ |
| 27 | + synapse.rest.admin.register_servlets, |
| 28 | + synapse.rest.client.login.register_servlets, |
| 29 | + ] |
| 30 | + |
| 31 | + def test_to_device_stream(self) -> None: |
| 32 | + store = self.hs.get_datastores().main |
| 33 | + |
| 34 | + user1 = self.register_user("user1", "pass") |
| 35 | + self.login("user1", "pass", "device") |
| 36 | + user2 = self.register_user("user2", "pass") |
| 37 | + self.login("user2", "pass", "device") |
| 38 | + |
| 39 | + # connect to pull the updates related to users creation/login |
| 40 | + self.reconnect() |
| 41 | + self.replicate() |
| 42 | + self.test_handler.received_rdata_rows.clear() |
| 43 | + # disconnect so we can accumulate the updates without pulling them |
| 44 | + self.disconnect() |
| 45 | + |
| 46 | + msg: JsonDict = {} |
| 47 | + msg["sender"] = "@sender:example.org" |
| 48 | + msg["type"] = "m.new_device" |
| 49 | + |
| 50 | + # add messages to the device inbox for user1 up until the |
| 51 | + # limit defined for a stream update batch |
| 52 | + for i in range(0, _STREAM_UPDATE_TARGET_ROW_COUNT): |
| 53 | + msg["content"] = {"device": {}} |
| 54 | + messages = {user1: {"device": msg}} |
| 55 | + |
| 56 | + self.get_success( |
| 57 | + store.add_messages_from_remote_to_device_inbox( |
| 58 | + "example.org", |
| 59 | + f"{i}", |
| 60 | + messages, |
| 61 | + ) |
| 62 | + ) |
| 63 | + |
| 64 | + # add one more message, for user2 this time |
| 65 | + # this message would be dropped before fixing #15335 |
| 66 | + msg["content"] = {"device": {}} |
| 67 | + messages = {user2: {"device": msg}} |
| 68 | + |
| 69 | + self.get_success( |
| 70 | + store.add_messages_from_remote_to_device_inbox( |
| 71 | + "example.org", |
| 72 | + f"{_STREAM_UPDATE_TARGET_ROW_COUNT}", |
| 73 | + messages, |
| 74 | + ) |
| 75 | + ) |
| 76 | + |
| 77 | + # replication is disconnected so we shouldn't get any updates yet |
| 78 | + self.assertEqual([], self.test_handler.received_rdata_rows) |
| 79 | + |
| 80 | + # now reconnect to pull the updates |
| 81 | + self.reconnect() |
| 82 | + self.replicate() |
| 83 | + |
| 84 | + # we should receive the fact that we have to_device updates |
| 85 | + # for user1 and user2 |
| 86 | + received_rows = self.test_handler.received_rdata_rows |
| 87 | + self.assertEqual(len(received_rows), 2) |
| 88 | + self.assertEqual(received_rows[0][2].entity, user1) |
| 89 | + self.assertEqual(received_rows[1][2].entity, user2) |
0 commit comments