public function testControlledExit()
{
if (!extension_loaded('pcntl')) {
$this->markTestSkipped('PCNTL extension is not loaded.');
}
$proc = new Process('exec ' . PHP_BINARY . ' ' . escapeshellarg(__DIR__ . '/console') . ' jms-job-queue:run --worker-name=test --verbose --max-runtime=999999');
$proc->start();
usleep(500000.0);
$this->assertTrue($proc->isRunning(), 'Process exited prematurely: ' . $proc->getOutput() . $proc->getErrorOutput());
$this->assertTrueWithin(3, function () use($proc) {
return false !== strpos($proc->getOutput(), 'Signal Handlers have been installed');
}, function () use($proc) {
$this->fail('Signal handlers were not installed: ' . $proc->getOutput() . $proc->getErrorOutput());
});
$proc->signal(SIGTERM);
$this->assertTrueWithin(3, function () use($proc) {
return false !== strpos($proc->getOutput(), 'Received SIGTERM');
}, function () use($proc) {
$this->fail('Signal was not received by process within 3 seconds: ' . $proc->getOutput() . $proc->getErrorOutput());
});
$this->assertTrueWithin(3, function () use($proc) {
return !$proc->isRunning();
}, function () use($proc) {
$this->fail('Process did not terminate within 3 seconds: ' . $proc->getOutput() . $proc->getErrorOutput());
});
$this->assertContains('All jobs finished, exiting.', $proc->getOutput());
}