|
| 1 | +# Copyright 2022 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 | + |
| 15 | +from synapse.replication.tcp.redis import RedisDirectTcpReplicationClientFactory |
| 16 | + |
| 17 | +from tests.replication._base import BaseMultiWorkerStreamTestCase |
| 18 | +from tests.unittest import HomeserverTestCase |
| 19 | + |
| 20 | +ALL_RDATA_CHANNELS = [ |
| 21 | + "RDATA/account_data", |
| 22 | + "RDATA/backfill", |
| 23 | + "RDATA/caches", |
| 24 | + "RDATA/device_lists", |
| 25 | + "RDATA/events", |
| 26 | + "RDATA/federation", |
| 27 | + "RDATA/groups", |
| 28 | + "RDATA/presence", |
| 29 | + "RDATA/presence_federation", |
| 30 | + "RDATA/push_rules", |
| 31 | + "RDATA/pushers", |
| 32 | + "RDATA/receipts", |
| 33 | + "RDATA/tag_account_data", |
| 34 | + "RDATA/to_device", |
| 35 | + "RDATA/typing", |
| 36 | + "RDATA/user_signature", |
| 37 | +] |
| 38 | + |
| 39 | + |
| 40 | +class RedisTestCase(HomeserverTestCase): |
| 41 | + def test_subscribed_to_enough_redis_channels(self) -> None: |
| 42 | + # The default main process is subscribed to USER_IP and all RDATA channels. |
| 43 | + self.assertCountEqual( |
| 44 | + RedisDirectTcpReplicationClientFactory.channels_to_subscribe_to_for_config( |
| 45 | + self.hs.config |
| 46 | + ), |
| 47 | + [ |
| 48 | + "USER_IP", |
| 49 | + ] |
| 50 | + + ALL_RDATA_CHANNELS, |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +class RedisWorkerTestCase(BaseMultiWorkerStreamTestCase): |
| 55 | + def test_background_worker_subscribed_to_user_ip(self) -> None: |
| 56 | + # The default main process is subscribed to USER_IP and all RDATA channels. |
| 57 | + worker1 = self.make_worker_hs( |
| 58 | + "synapse.app.generic_worker", |
| 59 | + extra_config={ |
| 60 | + "worker_name": "worker1", |
| 61 | + "run_background_tasks_on": "worker1", |
| 62 | + }, |
| 63 | + ) |
| 64 | + self.assertIn( |
| 65 | + "USER_IP", |
| 66 | + RedisDirectTcpReplicationClientFactory.channels_to_subscribe_to_for_config( |
| 67 | + worker1.config |
| 68 | + ), |
| 69 | + ) |
| 70 | + |
| 71 | + def test_non_background_worker_not_subscribed_to_user_ip(self) -> None: |
| 72 | + # The default main process is subscribed to USER_IP and all RDATA channels. |
| 73 | + worker2 = self.make_worker_hs( |
| 74 | + "synapse.app.generic_worker", |
| 75 | + extra_config={ |
| 76 | + "worker_name": "worker2", |
| 77 | + "run_background_tasks_on": "worker1", |
| 78 | + }, |
| 79 | + ) |
| 80 | + self.assertNotIn( |
| 81 | + "USER_IP", |
| 82 | + RedisDirectTcpReplicationClientFactory.channels_to_subscribe_to_for_config( |
| 83 | + worker2.config |
| 84 | + ), |
| 85 | + ) |
0 commit comments