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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
<?php
namespace PhpAmqpLib\Tests\Functional\Channel;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPSocketConnection;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exception\AMQPNoDataException;
use PhpAmqpLib\Exception\AMQPTimeoutException;
use PhpAmqpLib\Message\AMQPMessage;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
/**
* @group connection
*/
class ChannelWaitTest extends TestCase
{
/**
* @small
* @group signals
* @param callable $factory
*/
#[RequiresPhpunit('< 11')]
#[Test]
public function should_wait_until_signal_by_default($factory)
{
$this->deferSignal(0.5);
/** @var AMQPChannel $channel */
$channel = $factory();
$result = false;
try {
$result = $channel->wait();
} catch (\Exception $exception) {
$this->fail($exception->getMessage());
}
$this->closeChannel($channel);
$this->assertNull($result);
}
/**
* @group signals
* @covers AMQPIOReader::wait()
*/
#[Test]
public function should_wait_until_timeout_after_signal(): void
{
$factory = $this->channelFactory(true, 30, 15);
$channel = $factory();
$exception = null;
$started = microtime(true);
$this->deferSignal(1);
$this->deferSignal(2);
try {
$result = $channel->wait(null, false, 3);
} catch (\Throwable $exception) {
}
$took = microtime(true) - $started;
self::assertGreaterThan(2, $took);
self::assertLessThan(4, $took);
self::assertInstanceOf(AMQPTimeoutException::class, $exception);
$this->closeChannel($channel);
$this->assertNull($result);
}
/**
* @small
* @param callable $factory
*/
#[RequiresPhpunit('< 11')]
#[Test]
public function should_throw_timeout_exception($factory)
{
$this->expectException(AMQPTimeoutException::class);
$channel = $factory();
$channel->wait(null, false, 0.01);
$this->closeChannel($channel);
}
/**
* @small
* @param callable $factory
*/
#[RequiresPhpunit('< 11')]
#[Test]
public function should_return_instantly_non_blocking($factory)
{
$channel = $factory();
$start = microtime(true);
$channel->wait(null, true);
$took = microtime(true) - $start;
$this->assertLessThan(0.1, $took);
$this->closeChannel($channel);
}
/**
* @small
*/
#[Test]
public function should_call_handler_on_ack()
{
$receivedAck = false;
$handler = function ($message) use (&$receivedAck) {
$this->assertFalse($receivedAck);
$this->assertInstanceOf(AMQPMessage::class, $message);
$receivedAck = true;
};
$factory = $this->channelFactory();
/** @var AMQPChannel $channel */
$channel = $factory();
$channel->set_ack_handler($handler);
$channel->confirm_select();
$channel->basic_publish(new AMQPMessage('test'), 'basic_get_test');
$channel->wait_for_pending_acks(1);
$this->assertTrue($receivedAck);
}
public function provide_channels()
{
if (!defined('HOST')) {
$this->markTestSkipped('Unkown RabbitMQ host');
}
return [
[$this->channelFactory(true, 0.1, 0)],
[$this->channelFactory(false, 0.1, 0)],
[$this->channelFactory(true, 3, 1)],
[$this->channelFactory(false, 3, 1)],
];
}
protected function channelFactory($stream = true, $connectionTimeout = 1, $heartBeat = 0)
{
$factory = function () use ($stream, $connectionTimeout, $heartBeat) {
try {
if ($stream) {
$connection = new AMQPStreamConnection(
HOST,
PORT,
USER,
PASS,
VHOST,
false,
'AMQPLAIN',
null,
'en_us',
$connectionTimeout,
$connectionTimeout,
null,
false,
$heartBeat
);
} else {
$connection = new AMQPSocketConnection(
HOST,
PORT,
USER,
PASS,
VHOST,
false,
'AMQPLAIN',
null,
'en_US',
$connectionTimeout,
false,
$connectionTimeout,
$heartBeat
);
}
} catch (\ErrorException $exception) {
$this->markTestSkipped('Cannot connect to RabbitMQ: ' . $exception->getMessage());
}
$channel = $connection->channel();
$channel->queue_declare($queue = 'basic_get_queue', false, true, false, false);
$channel->exchange_declare($exchange = 'basic_get_test', 'fanout', false, true, false);
$channel->queue_bind($queue, $exchange);
return $channel;
};
return $factory;
}
public static function deferSignal($delay = 1)
{
if (!extension_loaded('pcntl')) {
self::markTestSkipped('pcntl extension is not available');
}
pcntl_signal(SIGTERM, function () {
});
$pid = getmypid();
exec('php -r "usleep(' . $delay * 1e6 . ');posix_kill(' . $pid . ', SIGTERM);" > /dev/null 2>/dev/null &');
}
protected function closeChannel(AMQPChannel $channel)
{
$connection = $channel->getConnection();
$channel->close();
$connection->close();
}
}
|