1
0

test_workers.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 typing import Any, Mapping, Optional
  15. from unittest.mock import Mock
  16. from immutabledict import immutabledict
  17. from synapse.config import ConfigError
  18. from synapse.config.workers import WorkerConfig
  19. from tests.unittest import TestCase
  20. _EMPTY_IMMUTABLEDICT: Mapping[str, Any] = immutabledict()
  21. class WorkerDutyConfigTestCase(TestCase):
  22. def _make_worker_config(
  23. self,
  24. worker_app: str,
  25. worker_name: Optional[str],
  26. extras: Mapping[str, Any] = _EMPTY_IMMUTABLEDICT,
  27. ) -> WorkerConfig:
  28. root_config = Mock()
  29. root_config.worker_app = worker_app
  30. root_config.worker_name = worker_name
  31. worker_config = WorkerConfig(root_config)
  32. worker_config_dict = {
  33. "worker_name": worker_name,
  34. "worker_app": worker_app,
  35. **extras,
  36. }
  37. worker_config.read_config(worker_config_dict)
  38. return worker_config
  39. def test_old_configs_master(self) -> None:
  40. """
  41. Tests old (legacy) config options. This is for the master's config.
  42. """
  43. main_process_config = self._make_worker_config(
  44. worker_app="synapse.app.homeserver", worker_name=None
  45. )
  46. self.assertTrue(
  47. main_process_config._should_this_worker_perform_duty(
  48. {},
  49. "notify_appservices",
  50. "synapse.app.appservice",
  51. "notify_appservices_from_worker",
  52. )
  53. )
  54. self.assertTrue(
  55. main_process_config._should_this_worker_perform_duty(
  56. {
  57. "notify_appservices": True,
  58. },
  59. "notify_appservices",
  60. "synapse.app.appservice",
  61. "notify_appservices_from_worker",
  62. )
  63. )
  64. self.assertFalse(
  65. main_process_config._should_this_worker_perform_duty(
  66. {
  67. "notify_appservices": False,
  68. },
  69. "notify_appservices",
  70. "synapse.app.appservice",
  71. "notify_appservices_from_worker",
  72. )
  73. )
  74. def test_old_configs_appservice_worker(self) -> None:
  75. """
  76. Tests old (legacy) config options. This is for the worker's config.
  77. """
  78. appservice_worker_config = self._make_worker_config(
  79. worker_app="synapse.app.appservice",
  80. worker_name="worker1",
  81. extras={
  82. # Set notify_appservices to false for the initialiser's config,
  83. # so that it doesn't raise an exception here.
  84. # (This is not read by `_should_this_worker_perform_duty`.)
  85. "notify_appservices": False,
  86. "instance_map": {"main": {"host": "127.0.0.1", "port": 0}},
  87. },
  88. )
  89. with self.assertRaises(ConfigError):
  90. # This raises because you need to set notify_appservices: False
  91. # before using the synapse.app.appservice worker type
  92. self.assertFalse(
  93. appservice_worker_config._should_this_worker_perform_duty(
  94. {},
  95. "notify_appservices",
  96. "synapse.app.appservice",
  97. "notify_appservices_from_worker",
  98. )
  99. )
  100. with self.assertRaises(ConfigError):
  101. # This also raises because you need to set notify_appservices: False
  102. # before using the synapse.app.appservice worker type
  103. appservice_worker_config._should_this_worker_perform_duty(
  104. {
  105. "notify_appservices": True,
  106. },
  107. "notify_appservices",
  108. "synapse.app.appservice",
  109. "notify_appservices_from_worker",
  110. )
  111. self.assertTrue(
  112. appservice_worker_config._should_this_worker_perform_duty(
  113. {
  114. "notify_appservices": False,
  115. },
  116. "notify_appservices",
  117. "synapse.app.appservice",
  118. "notify_appservices_from_worker",
  119. )
  120. )
  121. def test_transitional_configs_master(self) -> None:
  122. """
  123. Tests transitional (legacy + new) config options. This is for the master's config.
  124. """
  125. main_process_config = self._make_worker_config(
  126. worker_app="synapse.app.homeserver",
  127. worker_name=None,
  128. extras={"instance_map": {"main": {"host": "127.0.0.1", "port": 0}}},
  129. )
  130. self.assertTrue(
  131. main_process_config._should_this_worker_perform_duty(
  132. {
  133. "notify_appservices": True,
  134. "notify_appservices_from_worker": "master",
  135. },
  136. "notify_appservices",
  137. "synapse.app.appservice",
  138. "notify_appservices_from_worker",
  139. )
  140. )
  141. self.assertFalse(
  142. main_process_config._should_this_worker_perform_duty(
  143. {
  144. "notify_appservices": False,
  145. "notify_appservices_from_worker": "worker1",
  146. },
  147. "notify_appservices",
  148. "synapse.app.appservice",
  149. "notify_appservices_from_worker",
  150. )
  151. )
  152. with self.assertRaises(ConfigError):
  153. # Contradictory because we say the master should notify appservices,
  154. # then we say worker1 is the designated worker to do that!
  155. main_process_config._should_this_worker_perform_duty(
  156. {
  157. "notify_appservices": True,
  158. "notify_appservices_from_worker": "worker1",
  159. },
  160. "notify_appservices",
  161. "synapse.app.appservice",
  162. "notify_appservices_from_worker",
  163. )
  164. with self.assertRaises(ConfigError):
  165. # Contradictory because we say the master shouldn't notify appservices,
  166. # then we say master is the designated worker to do that!
  167. main_process_config._should_this_worker_perform_duty(
  168. {
  169. "notify_appservices": False,
  170. "notify_appservices_from_worker": "master",
  171. },
  172. "notify_appservices",
  173. "synapse.app.appservice",
  174. "notify_appservices_from_worker",
  175. )
  176. def test_transitional_configs_appservice_worker(self) -> None:
  177. """
  178. Tests transitional (legacy + new) config options. This is for the worker's config.
  179. """
  180. appservice_worker_config = self._make_worker_config(
  181. worker_app="synapse.app.appservice",
  182. worker_name="worker1",
  183. extras={
  184. # Set notify_appservices to false for the initialiser's config,
  185. # so that it doesn't raise an exception here.
  186. # (This is not read by `_should_this_worker_perform_duty`.)
  187. "notify_appservices": False,
  188. "instance_map": {"main": {"host": "127.0.0.1", "port": 0}},
  189. },
  190. )
  191. self.assertTrue(
  192. appservice_worker_config._should_this_worker_perform_duty(
  193. {
  194. "notify_appservices": False,
  195. "notify_appservices_from_worker": "worker1",
  196. },
  197. "notify_appservices",
  198. "synapse.app.appservice",
  199. "notify_appservices_from_worker",
  200. )
  201. )
  202. with self.assertRaises(ConfigError):
  203. # This raises because this worker is the appservice app type, yet
  204. # another worker is the designated worker!
  205. appservice_worker_config._should_this_worker_perform_duty(
  206. {
  207. "notify_appservices": False,
  208. "notify_appservices_from_worker": "worker2",
  209. },
  210. "notify_appservices",
  211. "synapse.app.appservice",
  212. "notify_appservices_from_worker",
  213. )
  214. def test_new_configs_master(self) -> None:
  215. """
  216. Tests new config options. This is for the master's config.
  217. """
  218. main_process_config = self._make_worker_config(
  219. worker_app="synapse.app.homeserver",
  220. worker_name=None,
  221. extras={"instance_map": {"main": {"host": "127.0.0.1", "port": 0}}},
  222. )
  223. self.assertTrue(
  224. main_process_config._should_this_worker_perform_duty(
  225. {"notify_appservices_from_worker": None},
  226. "notify_appservices",
  227. "synapse.app.appservice",
  228. "notify_appservices_from_worker",
  229. )
  230. )
  231. self.assertFalse(
  232. main_process_config._should_this_worker_perform_duty(
  233. {"notify_appservices_from_worker": "worker1"},
  234. "notify_appservices",
  235. "synapse.app.appservice",
  236. "notify_appservices_from_worker",
  237. )
  238. )
  239. def test_new_configs_appservice_worker(self) -> None:
  240. """
  241. Tests new config options. This is for the worker's config.
  242. """
  243. appservice_worker_config = self._make_worker_config(
  244. worker_app="synapse.app.generic_worker",
  245. worker_name="worker1",
  246. extras={"instance_map": {"main": {"host": "127.0.0.1", "port": 0}}},
  247. )
  248. self.assertTrue(
  249. appservice_worker_config._should_this_worker_perform_duty(
  250. {
  251. "notify_appservices_from_worker": "worker1",
  252. },
  253. "notify_appservices",
  254. "synapse.app.appservice",
  255. "notify_appservices_from_worker",
  256. )
  257. )
  258. self.assertFalse(
  259. appservice_worker_config._should_this_worker_perform_duty(
  260. {
  261. "notify_appservices_from_worker": "worker2",
  262. },
  263. "notify_appservices",
  264. "synapse.app.appservice",
  265. "notify_appservices_from_worker",
  266. )
  267. )
  268. def test_worker_duty_configs(self) -> None:
  269. """
  270. Additional tests for the worker duties
  271. """
  272. worker1_config = self._make_worker_config(
  273. worker_app="synapse.app.generic_worker",
  274. worker_name="worker1",
  275. extras={
  276. "notify_appservices_from_worker": "worker2",
  277. "update_user_directory_from_worker": "worker1",
  278. "instance_map": {"main": {"host": "127.0.0.1", "port": 0}},
  279. },
  280. )
  281. self.assertFalse(worker1_config.should_notify_appservices)
  282. self.assertTrue(worker1_config.should_update_user_directory)
  283. worker2_config = self._make_worker_config(
  284. worker_app="synapse.app.generic_worker",
  285. worker_name="worker2",
  286. extras={
  287. "notify_appservices_from_worker": "worker2",
  288. "update_user_directory_from_worker": "worker1",
  289. "instance_map": {"main": {"host": "127.0.0.1", "port": 0}},
  290. },
  291. )
  292. self.assertTrue(worker2_config.should_notify_appservices)
  293. self.assertFalse(worker2_config.should_update_user_directory)