File: Bug256Test.php

package info (click to toggle)
php-amqplib 3.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,044 kB
  • sloc: php: 13,145; makefile: 77; sh: 27
file content (103 lines) | stat: -rw-r--r-- 2,780 bytes parent folder | download | duplicates (2)
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
<?php

namespace PhpAmqpLib\Tests\Functional\Bug;

use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Tests\Functional\AbstractConnectionTestCase;
use PhpAmqpLib\Wire\AMQPTable;
use PHPUnit\Framework\Attributes\Test;

/**
 * @group connection
 */
class Bug256Test extends AbstractConnectionTestCase
{
    protected $exchangeName = 'test_exchange';

    protected $queueName = null;

    protected $messageCount = 100;

    protected $consumedCount = 0;

    protected $connection;

    protected $connection2;

    protected $channel;

    protected $channel2;

    protected function setUpCompat()
    {
        $this->connection = $this->connection_create('socket');
        $this->channel = $this->connection->channel();

        $this->channel->exchange_declare($this->exchangeName, 'direct', false, true, false);

        $this->connection2 = $this->connection_create('stream');
        $this->channel2 = $this->connection->channel();

        list($this->queueName, ,) = $this->channel2->queue_declare();
        $this->channel2->queue_bind($this->queueName, $this->exchangeName, $this->queueName);
    }

    protected function tearDownCompat()
    {
        if ($this->channel) {
            $this->channel->exchange_delete($this->exchangeName);
            $this->channel->close();
            $this->channel = null;
        }
        if ($this->connection) {
            $this->connection->close();
            $this->connection = null;
        }
        if ($this->channel2) {
            $this->channel2->close();
            $this->channel2 = null;
        }
        if ($this->connection2) {
            $this->connection2->close();
            $this->connection2 = null;
        }
    }

    #[Test]
    public function frame_order()
    {
        $msg = new AMQPMessage('');
        $hdrs = new AMQPTable(['x-foo' => 'bar']);
        $msg->set('application_headers', $hdrs);

        for ($i = 0; $i < $this->messageCount; $i++) {
            $this->channel->basic_publish($msg, $this->exchangeName, $this->queueName);
        }

        $this->channel2->basic_consume(
            $this->queueName,
            '',
            false,
            true,
            false,
            false,
            [$this, 'processMessage']
        );

        while (count($this->channel2->callbacks)) {
            $this->channel2->wait();
        }
    }

    public function processMessage(AMQPMessage $message)
    {
        $this->consumedCount++;

        $this->assertEquals(['x-foo' => 'bar'], $message->get('application_headers')->getNativeData());

        if ($this->consumedCount >= $this->messageCount) {
            $delivery_info = $message->delivery_info;
            $delivery_info['channel']->basic_cancel($delivery_info['consumer_tag']);
        }
    }
}