2020-10-10 16:06:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2020-12-27 23:31:45 +01:00
|
|
|
use Amp\SignalTrap;
|
2020-10-10 16:06:49 +02:00
|
|
|
use function Amp\await;
|
2020-12-27 23:31:45 +01:00
|
|
|
use function Amp\trap;
|
2020-10-10 16:06:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @requires ext-pcntl
|
|
|
|
*/
|
2020-12-27 23:31:45 +01:00
|
|
|
class SignalTrapTest extends AsyncTestCase
|
2020-10-10 16:06:49 +02:00
|
|
|
{
|
|
|
|
public function testDelayed(): void
|
|
|
|
{
|
|
|
|
$this->setMinimumRuntime(20);
|
|
|
|
|
2020-12-27 23:31:45 +01:00
|
|
|
$promise = new SignalTrap(\SIGUSR1, \SIGUSR2);
|
2020-10-10 16:06:49 +02:00
|
|
|
|
|
|
|
Loop::delay(10, fn() => \posix_kill(\getmypid(), \SIGUSR1));
|
|
|
|
|
|
|
|
$this->assertSame(\SIGUSR1, await($promise));
|
|
|
|
|
2020-12-27 23:31:45 +01:00
|
|
|
$promise = new SignalTrap(\SIGUSR1, \SIGUSR2);
|
2020-10-10 16:06:49 +02:00
|
|
|
|
|
|
|
Loop::delay(10, fn() => \posix_kill(\getmypid(), \SIGUSR2));
|
|
|
|
|
|
|
|
$this->assertSame(\SIGUSR2, await($promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReference(): void
|
|
|
|
{
|
|
|
|
$this->setMinimumRuntime(10);
|
|
|
|
|
|
|
|
Loop::delay(10, fn() => \posix_kill(\getmypid(), \SIGUSR1));
|
|
|
|
|
2020-12-27 23:31:45 +01:00
|
|
|
$promise = new SignalTrap(\SIGUSR1, \SIGUSR2);
|
2020-10-10 16:06:49 +02:00
|
|
|
$promise->unreference();
|
|
|
|
$promise->reference();
|
|
|
|
|
|
|
|
await($promise);
|
|
|
|
}
|
2020-12-27 23:31:45 +01:00
|
|
|
|
|
|
|
public function testTrapFunction(): void
|
|
|
|
{
|
|
|
|
$this->setMinimumRuntime(10);
|
|
|
|
|
|
|
|
Loop::delay(10, fn() => \posix_kill(\getmypid(), \SIGUSR1));
|
|
|
|
|
|
|
|
trap(\SIGUSR1, \SIGUSR2);
|
|
|
|
}
|
2020-10-10 16:06:49 +02:00
|
|
|
}
|