File: ReconnectConnectionTest.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 (144 lines) | stat: -rw-r--r-- 3,951 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
<?php

namespace PhpAmqpLib\Tests\Functional;

use PhpAmqpLib\Connection\AMQPLazyConnection;
use PhpAmqpLib\Connection\AMQPLazySocketConnection;
use PhpAmqpLib\Connection\AMQPSocketConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Tests\TestCaseCompat;
use PHPUnit\Framework\Attributes\Test;

/**
 * @group connection
 */
class ReconnectConnectionTest extends TestCaseCompat
{
    protected $connection = null;

    protected $channel = null;

    protected $exchange = 'reconnect_exchange';

    protected $queue = 'reconnect_queue';

    protected $msgBody = 'foo bar baz äëïöü';

    protected function tearDownCompat()
    {
        if ($this->channel) {
            $this->channel->exchange_delete($this->exchange);
            $this->channel->queue_delete($this->queue);
            $this->channel->close();
            $this->channel = null;
        }
        if ($this->connection) {
            $this->connection->close();
            $this->connection = null;
        }
    }

    #[Test]
    public function lazy_connection_reconnect()
    {
        $this->connection = $this->getLazyConnection();

        $this->doTest();
    }

    #[Test]
    public function lazy_connection_close_reconnect()
    {
        $this->connection = $this->getLazyConnection();
        $this->setupChannel();
        $this->connection->close();
        $this->connection->reconnect();
        $this->setupChannel();

        $this->assertEquals($this->msgBody, $this->publishGet()->body);
    }

    #[Test]
    public function socket_connection_reconnect()
    {
        $this->connection = $this->getSocketConnection();

        $this->doTest();
    }

    #[Test]
    public function socket_connection_close_reconnect()
    {
        $this->connection = $this->getSocketConnection();
        $this->connection->close();
        $this->connection->reconnect();
        $this->setupChannel();

        $this->assertEquals($this->msgBody, $this->publishGet()->body);
    }

    #[Test]
    public function lazy_socket_connection_close_reconnect()
    {
        $this->connection = $this->getLazySocketConnection();
        $this->connection->close();
        $this->connection->reconnect();
        $this->setupChannel();

        $this->assertEquals($this->msgBody, $this->publishGet()->body);
    }

    #[Test]
    public function lazy_connection_socket_reconnect()
    {
        $this->connection = $this->getLazySocketConnection();

        $this->doTest();
    }

    protected function getSocketConnection()
    {
        return new AMQPSocketConnection(HOST, PORT, USER, PASS, VHOST);
    }

    protected function getLazyConnection()
    {
        return new AMQPLazyConnection(HOST, PORT, USER, PASS, VHOST);
    }

    protected function getLazySocketConnection()
    {
        return new AMQPLazySocketConnection(HOST, PORT, USER, PASS, VHOST);
    }

    protected function doTest()
    {
        $this->setupChannel();
        $this->assertEquals($this->msgBody, $this->publishGet()->body);
        $this->connection->reconnect();
        $this->setupChannel();

        $this->assertEquals($this->msgBody, $this->publishGet()->body);
    }

    protected function setupChannel()
    {
        $this->channel = $this->connection->channel();
        $this->channel->exchange_declare($this->exchange, 'direct', false, false, false);
        $this->channel->queue_declare($this->queue);
        $this->channel->queue_bind($this->queue, $this->exchange, $this->queue);
    }

    protected function publishGet()
    {
        $msg = new AMQPMessage($this->msgBody, [
            'content_type' => 'text/plain',
            'delivery_mode' => AMQPMessage::DELIVERY_MODE_NON_PERSISTENT,
            'correlation_id' => 'my_correlation_id',
            'reply_to' => 'my_reply_to'
        ]);
        $this->channel->basic_publish($msg, $this->exchange, $this->queue);

        return $this->channel->basic_get($this->queue);
    }
}