File: ConnectionUnresponsiveTest.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 (83 lines) | stat: -rw-r--r-- 2,661 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
<?php

namespace PhpAmqpLib\Tests\Functional\Connection;

use PhpAmqpLib\Exception;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Tests\Functional\AbstractConnectionTestCase;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;

/**
 * @group connection
 */
class ConnectionUnresponsiveTest extends AbstractConnectionTestCase
{
    /**
     * Use mocked write functions to simulate completely blocked connections.
     * @small
     * @covers \PhpAmqpLib\Wire\IO\StreamIO::write()
     * @covers \PhpAmqpLib\Wire\IO\SocketIO::write()
     * @param string $type
     */
    #[Test]
    #[TestWith(["stream"])]
    #[TestWith(["socket"])]
    public function must_throw_exception_on_completely_blocked_connection($type)
    {
        self::$blocked = false;
        $connection = $this->connection_create($type);
        $channel = $connection->channel();
        $this->assertTrue($channel->is_open());
        $this->queue_bind($channel, $exchange_name = 'test_exchange_broken', $queue_name);

        self::$blocked = true;
        $message = new AMQPMessage(
            str_repeat('0', 8),
            ['delivery_mode' => AMQPMessage::DELIVERY_MODE_NON_PERSISTENT]
        );

        $exception = null;
        try {
            $channel->basic_publish($message, $exchange_name, $queue_name);
        } catch (\Exception $exception) {
        }

        self::$blocked = false;

        $this->assertInstanceOf(\Exception::class, $exception);
        $this->assertInstanceOf(Exception\AMQPTimeoutException::class, $exception);
        $this->assertEquals(1, $exception->getTimeout());

        $this->assertTrue($channel->is_open());
        $this->assertTrue($connection->isConnected());
    }

    /**
     * @group proxy
     * @covers \PhpAmqpLib\Connection\AbstractConnection::connect()
     * @param string $type
     */
    #[Test]
    #[TestWith(["stream"])]
    public function must_throw_timeout_exception_on_missing_connect_response($type)
    {
        $proxy = $this->create_proxy();
        $proxy->mode('timeout', ['timeout' => 0], 'downstream');
        $connection = null;
        $exception = null;
        try {
            $connection = $this->connection_create(
                $type,
                $proxy->getHost(),
                $proxy->getPort(),
                array('timeout' => 3, 'connectionTimeout' => .1, 'heartbeat' => 1)
            );
        } catch (\Exception $exception) {
        }

        $this->assertInstanceOf(\Exception::class, $exception);
        $this->assertInstanceOf(Exception\AMQPTimeoutException::class, $exception);
        $this->assertNull($connection);
    }
}