|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2020 The Matrix.org Foundation C.I.C. |
| 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 | + |
| 16 | +from synapse.replication.tcp.streams._base import ( |
| 17 | + _STREAM_UPDATE_TARGET_ROW_COUNT, |
| 18 | + AccountDataStream, |
| 19 | +) |
| 20 | + |
| 21 | +from tests.replication._base import BaseStreamTestCase |
| 22 | + |
| 23 | + |
| 24 | +class AccountDataStreamTestCase(BaseStreamTestCase): |
| 25 | + def test_update_function_room_account_data_limit(self): |
| 26 | + """Test replication with many room account data updates |
| 27 | + """ |
| 28 | + store = self.hs.get_datastore() |
| 29 | + |
| 30 | + # generate lots of account data updates |
| 31 | + updates = [] |
| 32 | + for i in range(_STREAM_UPDATE_TARGET_ROW_COUNT + 5): |
| 33 | + update = "m.test_type.%i" % (i,) |
| 34 | + self.get_success( |
| 35 | + store.add_account_data_to_room("test_user", "test_room", update, {}) |
| 36 | + ) |
| 37 | + updates.append(update) |
| 38 | + |
| 39 | + # also one global update |
| 40 | + self.get_success(store.add_account_data_for_user("test_user", "m.global", {})) |
| 41 | + |
| 42 | + # tell the notifier to catch up to avoid duplicate rows. |
| 43 | + # workaround for https://github.com/matrix-org/synapse/issues/7360 |
| 44 | + # FIXME remove this when the above is fixed |
| 45 | + self.replicate() |
| 46 | + |
| 47 | + # check we're testing what we think we are: no rows should yet have been |
| 48 | + # received |
| 49 | + self.assertEqual([], self.test_handler.received_rdata_rows) |
| 50 | + |
| 51 | + # now reconnect to pull the updates |
| 52 | + self.reconnect() |
| 53 | + self.replicate() |
| 54 | + |
| 55 | + # we should have received all the expected rows in the right order |
| 56 | + received_rows = self.test_handler.received_rdata_rows |
| 57 | + |
| 58 | + for t in updates: |
| 59 | + (stream_name, token, row) = received_rows.pop(0) |
| 60 | + self.assertEqual(stream_name, AccountDataStream.NAME) |
| 61 | + self.assertIsInstance(row, AccountDataStream.AccountDataStreamRow) |
| 62 | + self.assertEqual(row.data_type, t) |
| 63 | + self.assertEqual(row.room_id, "test_room") |
| 64 | + |
| 65 | + (stream_name, token, row) = received_rows.pop(0) |
| 66 | + self.assertIsInstance(row, AccountDataStream.AccountDataStreamRow) |
| 67 | + self.assertEqual(row.data_type, "m.global") |
| 68 | + self.assertIsNone(row.room_id) |
| 69 | + |
| 70 | + self.assertEqual([], received_rows) |
| 71 | + |
| 72 | + def test_update_function_global_account_data_limit(self): |
| 73 | + """Test replication with many global account data updates |
| 74 | + """ |
| 75 | + store = self.hs.get_datastore() |
| 76 | + |
| 77 | + # generate lots of account data updates |
| 78 | + updates = [] |
| 79 | + for i in range(_STREAM_UPDATE_TARGET_ROW_COUNT + 5): |
| 80 | + update = "m.test_type.%i" % (i,) |
| 81 | + self.get_success(store.add_account_data_for_user("test_user", update, {})) |
| 82 | + updates.append(update) |
| 83 | + |
| 84 | + # also one per-room update |
| 85 | + self.get_success( |
| 86 | + store.add_account_data_to_room("test_user", "test_room", "m.per_room", {}) |
| 87 | + ) |
| 88 | + |
| 89 | + # tell the notifier to catch up to avoid duplicate rows. |
| 90 | + # workaround for https://github.com/matrix-org/synapse/issues/7360 |
| 91 | + # FIXME remove this when the above is fixed |
| 92 | + self.replicate() |
| 93 | + |
| 94 | + # check we're testing what we think we are: no rows should yet have been |
| 95 | + # received |
| 96 | + self.assertEqual([], self.test_handler.received_rdata_rows) |
| 97 | + |
| 98 | + # now reconnect to pull the updates |
| 99 | + self.reconnect() |
| 100 | + self.replicate() |
| 101 | + |
| 102 | + # we should have received all the expected rows in the right order |
| 103 | + received_rows = self.test_handler.received_rdata_rows |
| 104 | + |
| 105 | + for t in updates: |
| 106 | + (stream_name, token, row) = received_rows.pop(0) |
| 107 | + self.assertEqual(stream_name, AccountDataStream.NAME) |
| 108 | + self.assertIsInstance(row, AccountDataStream.AccountDataStreamRow) |
| 109 | + self.assertEqual(row.data_type, t) |
| 110 | + self.assertIsNone(row.room_id) |
| 111 | + |
| 112 | + (stream_name, token, row) = received_rows.pop(0) |
| 113 | + self.assertIsInstance(row, AccountDataStream.AccountDataStreamRow) |
| 114 | + self.assertEqual(row.data_type, "m.per_room") |
| 115 | + self.assertEqual(row.room_id, "test_room") |
| 116 | + |
| 117 | + self.assertEqual([], received_rows) |
0 commit comments