Skip to content

Commit 392b44b

Browse files
authored
[1.3] Adds Signal (#6)
1 parent b94510c commit 392b44b

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎src/Contract/SignalInterface.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Engine\Contract;
13+
14+
interface SignalInterface
15+
{
16+
public static function wait(int $signo, float $timeout = -1): bool;
17+
}

‎src/Signal.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Engine;
13+
14+
use Hyperf\Engine\Contract\SignalInterface;
15+
use Swoole\Coroutine\System;
16+
17+
class Signal implements SignalInterface
18+
{
19+
public static function wait(int $signo, float $timeout = -1): bool
20+
{
21+
return System::waitSignal($signo, $timeout);
22+
}
23+
}

‎tests/Cases/SignalTest.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Cases;
13+
14+
use Hyperf\Engine\Signal;
15+
16+
/**
17+
* @internal
18+
* @coversNothing
19+
*/
20+
class SignalTest extends AbstractTestCase
21+
{
22+
public function testSignal()
23+
{
24+
$this->runInCoroutine(function () {
25+
$res = Signal::wait(SIGUSR1, 1);
26+
$this->assertFalse($res);
27+
28+
go(static function () {
29+
sleep(1);
30+
posix_kill(getmypid(), SIGUSR1);
31+
});
32+
33+
$res = Signal::wait(SIGUSR1, 2);
34+
$this->assertTrue($res);
35+
});
36+
}
37+
}

0 commit comments

Comments
 (0)