test_handler.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. from tests.replication._base import RedisMultiWorkerStreamTestCase
  15. class ChannelsTestCase(RedisMultiWorkerStreamTestCase):
  16. def test_subscribed_to_enough_redis_channels(self) -> None:
  17. # The default main process is subscribed to the USER_IP channel.
  18. self.assertCountEqual(
  19. self.hs.get_replication_command_handler()._channels_to_subscribe_to,
  20. ["USER_IP"],
  21. )
  22. def test_background_worker_subscribed_to_user_ip(self) -> None:
  23. # The default main process is subscribed to the USER_IP channel.
  24. worker1 = self.make_worker_hs(
  25. "synapse.app.generic_worker",
  26. extra_config={
  27. "worker_name": "worker1",
  28. "run_background_tasks_on": "worker1",
  29. "redis": {"enabled": True},
  30. },
  31. )
  32. self.assertIn(
  33. "USER_IP",
  34. worker1.get_replication_command_handler()._channels_to_subscribe_to,
  35. )
  36. # Advance so the Redis subscription gets processed
  37. self.pump(0.1)
  38. # The counts are 2 because both the main process and the worker are subscribed.
  39. self.assertEqual(len(self._redis_server._subscribers_by_channel[b"test"]), 2)
  40. self.assertEqual(
  41. len(self._redis_server._subscribers_by_channel[b"test/USER_IP"]), 2
  42. )
  43. def test_non_background_worker_not_subscribed_to_user_ip(self) -> None:
  44. # The default main process is subscribed to the USER_IP channel.
  45. worker2 = self.make_worker_hs(
  46. "synapse.app.generic_worker",
  47. extra_config={
  48. "worker_name": "worker2",
  49. "run_background_tasks_on": "worker1",
  50. "redis": {"enabled": True},
  51. },
  52. )
  53. self.assertNotIn(
  54. "USER_IP",
  55. worker2.get_replication_command_handler()._channels_to_subscribe_to,
  56. )
  57. # Advance so the Redis subscription gets processed
  58. self.pump(0.1)
  59. # The count is 2 because both the main process and the worker are subscribed.
  60. self.assertEqual(len(self._redis_server._subscribers_by_channel[b"test"]), 2)
  61. # For USER_IP, the count is 1 because only the main process is subscribed.
  62. self.assertEqual(
  63. len(self._redis_server._subscribers_by_channel[b"test/USER_IP"]), 1
  64. )