|
18 | 18 | # [This file includes modifications made by New Vector Limited] |
19 | 19 | # |
20 | 20 | # |
| 21 | +from typing import Any |
| 22 | +from unittest import mock |
21 | 23 |
|
22 | 24 | from twisted.internet import defer |
| 25 | +from twisted.internet.testing import MemoryReactor |
23 | 26 |
|
24 | | -from synapse.replication.tcp.commands import PositionCommand |
| 27 | +from synapse.replication.tcp.commands import PositionCommand, ServerCommand |
| 28 | +from synapse.server import HomeServer |
| 29 | +from synapse.util.clock import Clock |
25 | 30 |
|
26 | 31 | from tests.replication._base import BaseMultiWorkerStreamTestCase |
27 | 32 |
|
@@ -209,3 +214,52 @@ def test_wait_for_stream_position_rdata(self) -> None: |
209 | 214 | # Master should get told about `next_token2`, so the deferred should |
210 | 215 | # resolve. |
211 | 216 | self.assertTrue(d.called) |
| 217 | + |
| 218 | + |
| 219 | +class RestartBehaviorTestCase(BaseMultiWorkerStreamTestCase): |
| 220 | + def prepare( |
| 221 | + self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer |
| 222 | + ) -> None: |
| 223 | + super().prepare(reactor, clock, homeserver) |
| 224 | + self.repl_comm_handler = self.hs.get_replication_command_handler() |
| 225 | + |
| 226 | + self.server_name = self.hs.config.server.server_name |
| 227 | + |
| 228 | + def default_config(self) -> dict[str, Any]: |
| 229 | + config = super().default_config() |
| 230 | + config["force_crash_workers_after_main_restart"] = True |
| 231 | + return config |
| 232 | + |
| 233 | + def test_server_command_can_raise_from_main(self) -> None: |
| 234 | + """ |
| 235 | + Test that a server command send from the main process can raise a SIGTERM on a |
| 236 | + worker |
| 237 | + """ |
| 238 | + # self.reactor.advance(0.001) |
| 239 | + worker = self.make_worker_hs( |
| 240 | + "synapse.app.generic_worker", |
| 241 | + extra_config={ |
| 242 | + "worker_name": "worker1", |
| 243 | + "redis": {"enabled": True}, |
| 244 | + }, |
| 245 | + ) |
| 246 | + |
| 247 | + # Move forward time just a pinch, so the SERVER command has a new value |
| 248 | + self.reactor.advance(0.001) |
| 249 | + # In unit tests, we can not really restart a process. So, send the command again |
| 250 | + # to simulate that. |
| 251 | + self.repl_comm_handler.send_command( |
| 252 | + ServerCommand( |
| 253 | + self.server_name, self.hs.get_instance_name(), self.clock.time_msec() |
| 254 | + ) |
| 255 | + ) |
| 256 | + with mock.patch( |
| 257 | + "signal.raise_signal", |
| 258 | + ) as patch_ctx: |
| 259 | + # Simulate receiving the redis command by calling the handler that would |
| 260 | + # have responded to "wake up" |
| 261 | + worker.get_replication_streamer().on_notifier_poke() |
| 262 | + # Need to move forward time to get the background tasks to run |
| 263 | + self.reactor.advance(0.001) |
| 264 | + |
| 265 | + patch_ctx.assert_called_once() |
0 commit comments