File: PCNTLHeartbeatSenderTest.php

package info (click to toggle)
php-amqplib 3.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,060 kB
  • sloc: php: 13,145; makefile: 77; sh: 27
file content (163 lines) | stat: -rw-r--r-- 5,016 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php

namespace PhpAmqpLib\Tests\Functional\Connection\Heartbeat;

use PhpAmqpLib\Connection\AbstractConnection;
use PhpAmqpLib\Connection\Heartbeat\PCNTLHeartbeatSender;
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
 * @requires extension pcntl
 */
#[RequiresPhp('7.1')]
class PCNTLHeartbeatSenderTest extends AbstractConnectionTestCase
{
    /** @var AbstractConnection */
    protected $connection;

    /** @var PCNTLHeartbeatSender */
    protected $sender;

    /** @var int */
    protected $heartbeatTimeout = 4;

    protected function setUpCompat()
    {
        $this->connection = $this->connection_create(
            'stream',
            HOST,
            PORT,
            ['timeout' => 3, 'heartbeat' => $this->heartbeatTimeout]
        );
        try {
            $this->sender = new PCNTLHeartbeatSender($this->connection);
        } catch (\Exception $exception) {
            $this->markTestSkipped($exception->getMessage());
        }
    }

    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(SIGALRM));
    }

    #[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);
    }

    #[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->exactly(3))->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 PCNTLHeartbeatSender($connection);
        $sender->register();

        $timeLeft = $this->heartbeatTimeout + 1;
        while ($timeLeft > 0) {
            $timeLeft = sleep($timeLeft);
        }

        $sender->unregister();
    }

    /**
     * @covers \PhpAmqpLib\Connection\Heartbeat\AbstractSignalHeartbeatSender::handleSignal
     */
    #[Test]
    public function signal_handler_should_ignore_inactive_lazy_connections()
    {
        $connection = $this->getMockBuilder(AbstractConnection::class)
            ->onlyMethods(['isConnected', 'getHeartbeat', 'isWriting', 'getLastActivity', 'checkHeartBeat'])
            ->disableOriginalConstructor()
            ->getMock();
        $connection
            ->expects(self::exactly(4))
            ->method('isConnected')
            ->willReturnOnConsecutiveCalls(false, true, true, false);
        $connection
            ->expects(self::exactly(1))
            ->method('isWriting')
            ->willReturn(false);


        $sender = new PCNTLHeartbeatSender($connection);

        $reflection = new \ReflectionClass($sender);
        $wasActive = $reflection->getProperty('wasActive');
        $wasActive->setAccessible(true);
        $conn = $reflection->getProperty('connection');
        $conn->setAccessible(true);
        $method = $reflection->getMethod('handleSignal');
        $method->setAccessible(true);


        $method->invoke($sender, 10);
        self::assertFalse($wasActive->getValue($sender));

        $method->invoke($sender, 10);
        self::assertTrue($wasActive->getValue($sender));
        self::assertNotNull($conn->getValue($sender));

        $method->invoke($sender, 10);
        self::assertTrue($wasActive->getValue($sender));
        self::assertNull($conn->getValue($sender));

        $sender->unregister();
    }
}