Skip to content

Commit 589879d

Browse files
committed
Add SIGQUIT support to the signal list
1 parent 0c2ee0d commit 589879d

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

src/Command/RunWebSocketServerCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6161
$this->loop->addSignal(\SIGTERM, $closer);
6262
}
6363

64+
if (\defined('SIGQUIT')) {
65+
$this->loop->addSignal(\SIGQUIT, $closer);
66+
}
67+
6468
$this->eventDispatcher?->dispatch(new BeforeRunServer($socketServer, $this->loop));
6569

6670
$server->run();

tests/Command/RunWebSocketServerCommandTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,56 @@ public function testCommandLaunchesWebSocketServerWithUriFromArguments(): void
9494
$commandTester = new CommandTester($command);
9595
$commandTester->execute(['uri' => $uri]);
9696
}
97+
98+
public function testCommandRegistersTheShutdownSignalsOnTheEventLoop(): void
99+
{
100+
$uri = 'tcp://127.0.0.1:8080';
101+
102+
/** @var Stub&ServerInterface $socketServer */
103+
$socketServer = self::createStub(ServerInterface::class);
104+
105+
/** @var MockObject&Server $server */
106+
$server = $this->createMock(Server::class);
107+
$server->expects(self::once())
108+
->method('run');
109+
110+
/** @var MockObject&SocketServerFactory $socketServerFactory */
111+
$socketServerFactory = $this->createMock(SocketServerFactory::class);
112+
$socketServerFactory->method('build')
113+
->with($uri)
114+
->willReturn($socketServer);
115+
116+
/** @var MockObject&ServerFactory $serverFactory */
117+
$serverFactory = $this->createMock(ServerFactory::class);
118+
$serverFactory->method('build')
119+
->with($socketServer)
120+
->willReturn($server);
121+
122+
$registeredSignals = [];
123+
124+
/** @var Stub&LoopInterface $loop */
125+
$loop = $this->createStub(LoopInterface::class);
126+
$loop->method('addSignal')
127+
->willReturnCallback(function (int $signal) use (&$registeredSignals): void {
128+
$registeredSignals[] = $signal;
129+
});
130+
131+
$command = new RunWebSocketServerCommand(null, $socketServerFactory, $serverFactory, $loop, $uri);
132+
133+
$commandTester = new CommandTester($command);
134+
$commandTester->execute([]);
135+
136+
// The command guards each registration with \defined() since the SIG* constants
137+
// require ext-pcntl, so only assert on the signals available in this environment.
138+
$expectedSignals = array_values(array_filter(
139+
[
140+
\defined('SIGINT') ? \SIGINT : null,
141+
\defined('SIGTERM') ? \SIGTERM : null,
142+
\defined('SIGQUIT') ? \SIGQUIT : null,
143+
],
144+
static fn (?int $signal): bool => null !== $signal,
145+
));
146+
147+
self::assertSame($expectedSignals, $registeredSignals);
148+
}
97149
}

0 commit comments

Comments
 (0)