mirror of
https://github.com/danog/amp.git
synced 2024-12-02 17:37:50 +01:00
ae93b4cf21
Also renamed Amp\signal() to trap(), a nice analog to bash’s trap.
55 lines
1.2 KiB
PHP
55 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace Amp\Test;
|
|
|
|
use Amp\Loop;
|
|
use Amp\PHPUnit\AsyncTestCase;
|
|
use Amp\SignalTrap;
|
|
use function Amp\await;
|
|
use function Amp\trap;
|
|
|
|
/**
|
|
* @requires ext-pcntl
|
|
*/
|
|
class SignalTrapTest extends AsyncTestCase
|
|
{
|
|
public function testDelayed(): void
|
|
{
|
|
$this->setMinimumRuntime(20);
|
|
|
|
$promise = new SignalTrap(\SIGUSR1, \SIGUSR2);
|
|
|
|
Loop::delay(10, fn() => \posix_kill(\getmypid(), \SIGUSR1));
|
|
|
|
$this->assertSame(\SIGUSR1, await($promise));
|
|
|
|
$promise = new SignalTrap(\SIGUSR1, \SIGUSR2);
|
|
|
|
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));
|
|
|
|
$promise = new SignalTrap(\SIGUSR1, \SIGUSR2);
|
|
$promise->unreference();
|
|
$promise->reference();
|
|
|
|
await($promise);
|
|
}
|
|
|
|
public function testTrapFunction(): void
|
|
{
|
|
$this->setMinimumRuntime(10);
|
|
|
|
Loop::delay(10, fn() => \posix_kill(\getmypid(), \SIGUSR1));
|
|
|
|
trap(\SIGUSR1, \SIGUSR2);
|
|
}
|
|
}
|