-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignalsTest.php
More file actions
75 lines (61 loc) · 1.9 KB
/
SignalsTest.php
File metadata and controls
75 lines (61 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types = 1);
namespace Tests\Innmind\OperatingSystem\CurrentProcess;
use Innmind\OperatingSystem\CurrentProcess\Signals;
use Innmind\Signals\{
Handler,
Signal
};
use Innmind\BlackBox\PHPUnit\Framework\TestCase;
class SignalsTest extends TestCase
{
public function testListen()
{
$signals = Signals::of(Handler::main());
$order = [];
$count = 0;
$this->fork();
$this->assertNull($signals->listen(Signal::child, function($signal) use (&$order, &$count): void {
$this->assertSame(Signal::child, $signal);
$order[] = 'first';
++$count;
}));
$signals->listen(Signal::child, function($signal) use (&$order, &$count): void {
$this->assertSame(Signal::child, $signal);
$order[] = 'second';
++$count;
});
\sleep(2); // wait for child to stop
$this->assertSame(2, $count);
$this->assertSame(['first', 'second'], $order);
}
public function testRemoveSignal()
{
$signals = Signals::of(Handler::main());
$order = [];
$count = 0;
$this->fork();
$first = function($signal) use (&$order, &$count): void {
$this->assertSame(Signal::child, $signal);
$order[] = 'first';
++$count;
};
$signals->listen(Signal::child, $first);
$signals->listen(Signal::child, function($signal) use (&$order, &$count): void {
$this->assertSame(Signal::child, $signal);
$order[] = 'second';
++$count;
});
$this->assertNull($signals->remove($first));
\sleep(2); // wait for child to stop
$this->assertSame(1, $count);
$this->assertSame(['second'], $order);
}
private function fork()
{
if (\pcntl_fork() === 0) {
\sleep(1);
exit;
}
}
}