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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
<?php
namespace PhpAmqpLib\Tests\Functional\Connection\Heartbeat;
use PhpAmqpLib\Connection\AbstractConnection;
use PhpAmqpLib\Connection\Heartbeat\SIGHeartbeatSender;
use PhpAmqpLib\Exception\AMQPRuntimeException;
use PhpAmqpLib\Tests\Functional\AbstractConnectionTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\Attributes\Test;
/**
* @group connection
* @group signals
* @group sig
* @requires extension pcntl
*/
#[RequiresPhp('7.1')]
class SIGHeartbeatSenderTest extends AbstractConnectionTestCase
{
/** @var AbstractConnection */
protected $connection;
/** @var SIGHeartbeatSender */
protected $sender;
/** @var int */
protected $heartbeatTimeout = 4;
protected $signal = SIGUSR1;
protected function setUpCompat()
{
$this->connection = $this->connection_create(
'stream',
HOST,
PORT,
['timeout' => 3, 'heartbeat' => $this->heartbeatTimeout]
);
$this->sender = new SIGHeartbeatSender($this->connection, $this->signal);
}
protected function tearDownCompat()
{
if ($this->sender) {
$this->sender->unregister();
}
if ($this->connection) {
$this->connection->close();
}
$this->sender = null;
$this->connection = null;
}
#[Test]
public function register_should_fail_after_unregister()
{
$this->expectException(AMQPRuntimeException::class);
$this->expectExceptionMessage('Unable to re-register heartbeat sender');
$this->sender->unregister();
$this->sender->register();
}
#[Test]
public function unregister_should_return_default_signal_handler()
{
$this->sender->register();
$this->sender->unregister();
self::assertEquals(SIG_IGN, pcntl_signal_get_handler($this->signal));
}
#[Test]
public function heartbeat_should_interrupt_non_blocking_action()
{
$this->sender->register();
$timeLeft = $this->heartbeatTimeout;
$continuation = 0;
while ($timeLeft > 0) {
$timeLeft = sleep($timeLeft);
$continuation++;
}
self::assertEquals(2, $continuation);
}
/**
* @runInSeparateProcess
*/
#[Test]
public function alarm_sig_should_be_registered_when_conn_is_writing()
{
$connection = $this->getMockBuilder(AbstractConnection::class)
->onlyMethods(['isConnected', 'getHeartbeat', 'isWriting', 'getLastActivity'])
->disableOriginalConstructor()
->getMock();
$connection->expects($this->atLeast(2))->method('isConnected')->willReturn(true);
$connection->expects($this->once())->method('getHeartbeat')->willReturn($this->heartbeatTimeout);
$connection->expects($this->exactly(2))
->method('isWriting')
->willReturnOnConsecutiveCalls(true, false);
$connection->expects($this->exactly(1))
->method('getLastActivity')
->willReturn(time() + 99);
$sender = new SIGHeartbeatSender($connection, $this->signal);
$sender->register();
$timeLeft = $this->heartbeatTimeout + 1;
while ($timeLeft > 0) {
$timeLeft = sleep($timeLeft);
}
$sender->unregister();
}
/**
* @runInSeparateProcess
* @outputBuffering disabled
* @covers \PhpAmqpLib\Connection\Heartbeat\SIGHeartbeatSender::unregister()
*/
#[Test]
public function child_process_must_be_terminated_after_unregister()
{
$property = new \ReflectionProperty(get_class($this->sender), 'childPid');
$property->setAccessible(true);
$this->sender->register();
$pid = $property->getValue($this->sender);
self::assertGreaterThan(0, $pid);
$this->sender->unregister();
$result = pcntl_waitpid($pid, $status, WNOHANG);
self::assertEquals(-1, $result);
self::assertEquals(0, $status);
}
}
|