-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathSignalSocketHelperTest.php
124 lines (89 loc) · 3.38 KB
/
SignalSocketHelperTest.php
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace Enqueue\AmqpTools\Tests;
use Enqueue\AmqpTools\SignalSocketHelper;
use PHPUnit\Framework\TestCase;
class SignalSocketHelperTest extends TestCase
{
/**
* @var SignalSocketHelper
*/
private $signalHelper;
private $backupSigTermHandler;
private $backupSigIntHandler;
public function setUp()
{
parent::setUp();
if (false == function_exists('pcntl_signal_get_handler')) {
$this->markTestSkipped('PHP 7.1 and higher');
}
$this->backupSigTermHandler = pcntl_signal_get_handler(SIGTERM);
$this->backupSigIntHandler = pcntl_signal_get_handler(SIGINT);
pcntl_signal(SIGTERM, SIG_DFL);
pcntl_signal(SIGINT, SIG_DFL);
$this->signalHelper = new SignalSocketHelper();
}
public function tearDown()
{
parent::tearDown();
if ($this->signalHelper) {
$this->signalHelper->afterSocket();
}
if ($this->backupSigTermHandler) {
pcntl_signal(SIGTERM, $this->backupSigTermHandler);
}
if ($this->backupSigIntHandler) {
pcntl_signal(SIGINT, $this->backupSigIntHandler);
}
}
public function testShouldReturnFalseByDefault()
{
$this->assertFalse($this->signalHelper->wasThereSignal());
}
public function testShouldRegisterHandlerOnBeforeSocket()
{
$this->signalHelper->beforeSocket();
$this->assertAttributeSame(false, 'wasThereSignal', $this->signalHelper);
$this->assertAttributeSame([], 'handlers', $this->signalHelper);
}
public function testShouldRegisterHandlerOnBeforeSocketAndBackupCurrentOne()
{
$handler = function () {};
pcntl_signal(SIGTERM, $handler);
$this->signalHelper->beforeSocket();
$this->assertAttributeSame(false, 'wasThereSignal', $this->signalHelper);
$handlers = $this->readAttribute($this->signalHelper, 'handlers');
$this->assertInternalType('array', $handlers);
$this->assertArrayHasKey(SIGTERM, $handlers);
$this->assertSame($handler, $handlers[SIGTERM]);
}
public function testRestoreDefaultPropertiesOnAfterSocket()
{
$this->signalHelper->beforeSocket();
$this->signalHelper->afterSocket();
$this->assertAttributeSame(null, 'wasThereSignal', $this->signalHelper);
$this->assertAttributeSame([], 'handlers', $this->signalHelper);
}
public function testRestorePreviousHandlerOnAfterSocket()
{
$handler = function () {};
pcntl_signal(SIGTERM, $handler);
$this->signalHelper->beforeSocket();
$this->signalHelper->afterSocket();
$this->assertSame($handler, pcntl_signal_get_handler(SIGTERM));
}
public function testThrowsIfBeforeSocketCalledSecondTime()
{
$this->signalHelper->beforeSocket();
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The wasThereSignal property should be null but it is not. The afterSocket method might not have been called.');
$this->signalHelper->beforeSocket();
}
public function testShouldReturnTrueOnWasThereSignal()
{
$this->signalHelper->beforeSocket();
posix_kill(getmypid(), SIGINT);
pcntl_signal_dispatch();
$this->assertTrue($this->signalHelper->wasThereSignal());
$this->signalHelper->afterSocket();
}
}